I was trying to paint an image on my JPanel (which is placed on a JFrame). I have created the panel & frame with this code:
JFrame frame = new JFrame("Game");
panel = (JPanel)frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(800,600));
panel.setLayout(null);
panel.add(this);
setIgnoreRepaint(true);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
To be able to paint something on the screen I have decided to create a mehtod called getGraphics, which will return me the graphics from my JPanel. To load the images I am using this code:
Image background;
public void loadImages()
{
try {
background = ImageIO.read(new File("background.jpg"));
System.out.printf("[IMAGE] %dx%d",background.getHeight(null),background.getWidth(null));
} catch (IOException e) {
e.printStackTrace();
}
}
Just to make sure that it has worked fine, I am giving the dimension of my image out to the console after loading it.
To paint that all on the screen I am using this code:
public class Panel extends JComponent
{
@Override
public void paint(Graphics g)
{
TextureEngine te = new TextureEngine();
g.drawImage(te.background,60,60,null);
}
}
If I for example draw a line on my JPanel, everything works fine. But if I try to draw an image on it, nothing happens. What am I doing wrong here?