0
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class Main
{
    JFrame jf;
    Main()
    {
        jf=new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(new MyCanvas());
        jf.pack();
        jf.setVisible(true);
    }
    public static void main(String[] args)
    {
        MyCanvas.img=Toolkit.getDefaultToolkit().createImage("1.jpg");
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new Main();
            }
        });
    }
}
class MyCanvas extends JComponent
{
    static Image img;
    BufferedImage bi;
    MyCanvas()
    {
        setPreferredSize(new Dimension(200,200));
        bi=new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
        Graphics g=bi.getGraphics();
        for (int i=0;i<10;i++)
            for (int j=0;j<10;j++)
                g.drawImage(img,i*20,j*20,20,20,this);
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(bi,0,0,this);
        //g.drawImage(img,0,0,this);
    }
}

If I uncomment line g.drawImage(img,0,0,this); I get my image on window. But with this code I see empty window. It is easy to understand that g.drawImage(img,i*20,j*20,20,20,this); in loop doesn't draw but what is problem?

eXXXXXXXXXXX2
  • 1,540
  • 1
  • 18
  • 32

2 Answers2

3

edited out the unrelated stuff I did for future readers to find the answer easier. In java i've always had to use getResource or getResourceAsStream to ensure the file loaded from the right directory correctly.

class MyCanvas extends JComponent
{
    public static Image loadImage(String s)
    {
        try 
        {
            return ImageIO.read(Main.class.getResource(s));
        } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
            return null;
        }
    }
}
annirun
  • 123
  • 2
  • 7
  • `MyCanvas.img=Toolkit.getDefaultToolkit().createImage("1.jpg");` What is wrong in this line? With using `ImageIO.read()` I get desired result. – eXXXXXXXXXXX2 Feb 25 '12 at 16:21
  • will you said `g.drawImage(img,0,0,this);` worked beforehand so that rules out the only thing I had to say which was that java is picky about what folder it considers root so it sometimes looks in the wrong place. – annirun Feb 25 '12 at 16:51
  • 1
    @eXXXXXXXXXXX: `createImage()` is asynchronous; +1 for `ImageIO`. – trashgod Feb 25 '12 at 20:02
1

1) put BufferedImage as Icon to the JLabel example here

2) your paintComponent in CustomCanvas could be

class MyCanvas extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(150, 150);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bi,0,0,this);
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319