Painting something on a JPanel

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?

Can I see what TextureEngine does?

Don’t create a new TextureEngine object every time paint is called. Make it a class variable inside your Panel class.

Is loadImages ever called?

public class TextureEngine
{
	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();
		}
	}
}

[quote="ra4king,post:3,topic:38222"] Don't create a new TextureEngine object every time paint is called. Make it a class variable inside your Panel class.

Is loadImages ever called?
[/quote]
Ah yeah, right, it would be pretty stupid to create it on that way I guess. Fixed.
And yes, loadImages gets called:

public static void main(String[] args)
{
	Game g = new Game();
	TextureEngine te = new TextureEngine();
	g.running = true;
	te.loadImages();
	g.run();
	
}

(From the main class called ‘Game’)

This is my run method:

public void run()
{
	Panel p = new Panel();
	while(running)
	{
		//Render our Panel
		p.paint(getGraphics());
	}
}

No you never do call loadImages, the TextureEngine instance in your main method and the one in your Panel are completely different :wink:

You’re declaring the TextureEngine variable locally in main(), so Game and Panel don’t see it. Try reorganizing your code a little so that the te variable can be used in your Panel’s paint() method. :slight_smile:

Well if your calling everything from the main ‘static’ context then I think all the methods have to be static…at least the ones in that class.

Also, if you want to use any variables in a different class (your JPanel) they need to be declared outside of any method.


public void setStuff()
{
     int myInt =  0;
}

nothing outside of that method can use that int.


private int myInt;

public void setStuff()
{
     myInt = 0;
}

Now you can use that int outside of the method.

Wow thank you so much! I just had to change a few lines to fix everything.