How do you paint a content pane?

I’m laying the groundwork for an applet, but for now I’m testing it out as an application. Anyway, I originally had it such that I called repaint() at the standard 60 times a second. But I found that this flickered a lot and was not very good. I looked into it and found that I should be using “paintComponent()” to paint on a contentPane() instead of overriding the regular paint method.

So I have an class called “GamePanel” which extends a JPanel, and here is it’s paint component method:

@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		Rectangle rect = new Rectangle(100, 100, 100, 100);
		g2d.fill(rect);
	}

All it does is draw a black square

And here’s how I set the panel up at the start of the program:

GamePanel gamePanel = new GamePanel();
GameComponent gameComponenet = new GameComponent();
JFrame frame = new JFrame("Video Game");

gamePanel.add(gameComponenet, BorderLayout.CENTER);

frame.setContentPane(gamePanel);

Now I’m just kinda confused… How do I access the “paintComponent()” method in the GamePanel class WITHOUT putting it in the overridden paint() method? I found that when I did the following in conjunction with the 60fps repaint(), It just flickered anyway, so that can’t be the answer

@Override
	public void paint(Graphics g) {
		gamePanel.paintComponent(g);
	}

Thanks

You need to render to an off screen image, then draw that to the Graphics object passed to you in paint.

Er… How do you do that?

Most of Swing is double buffered by default.

I would use canvas and a bufferstrategy so you will have control on when everything is rendered. EG: active rendering

Here are some useful links to help you get started on properly double buffer and active render.

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418

Ah, thanks so much for those tutorials! I found the first one very useful. I now have a much smoother rendering system. Thank you