painting and animating in Jpanel

Ok, very weird things happening. First of all, I’ve changed this line


repaint();

to this

paint(this.getGraphics());

And the flickering is gone.

And in the main class

public class Game {

	
	public static void main(String[] args) {
	JFrame frame = new JFrame();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setContentPane(new GamePanel());
	frame.setResizable(false);
	frame.pack();
	frame.setLocationRelativeTo(null);
	frame.setVisible(true);
    
	}

}

Changing the setResizable from a line to another determines the flickering too. Wow, I’ve found this by trial and error, am I supposed to find documentation about that?

@sirkarpfen: How do you buffer the graphic object?

calling paint(Graphics g) or paintComponent(Graphics g) directly is not recommended. Instead you should use repaint(). If you want a nice and smooth rendering you should definetely use Canvas + accelerated Graphics and double buffering (as i allready posted before^^).

Also this:


frame.setContentPane(new GamePanel());

Is useless, instead just add the GamePanel to the frame:


frame.add(new GamePanel);

It seems like you have to learn a bit more about Swing and AWT Components and the painting Mechanism so you could take a look here:
http://www.oracle.com/technetwork/java/painting-140037.html

EDIT: As Oracle confirmes JPanel has a double buffered Graphics Object by default, but it still seems that it is much slower than using the Graphics Object from a Canvas with a double buffered strategy. Is it because AWT is much more low-level? Can someone maybe explain that to us? xD

And while I’m reading Oracle, I’m submitting to you this.

[quote]Swing starts with AWT’s basic painting model and extends it further in order to maximize performance and improve extensibility
[/quote]

[/quote]
which is interesting and efficient for 2D GUIs but not for games in which you use custom painting.

Ok, after testing and studying I am at a conclusion. Correct me if I’m wrong.
You can mix awt and Swing, especially if it’s a Canvas inside a JFrame, so you won’t deal with other components like JButton and stuff. Canvas let you bypass the JVM (so you won’t be overriding paint) with the bufferstrategy inside EDT.
Once you did that, you can draw anything you like with g object.
Jpanel can bypass too the JVM with a BufferedImage to avoid the overwritten PaintComponent and thus putting everything into the EDT.

Basically you can mix as many AWT and Swing Components as you like. This was only a problem before Java 6 Update 10. It was fixed and there are no more problems when mixing components. If it’s usefull is another story :).

What you wrote about the buffered Graphics object is correct. But with Canvas you get a new Graphics object when creating a bufferstrategy. This object is for your use only and is normally not touched by drawing operations triggered by the JVM. The JPanels Graphics Object is used in every paintComponent(Graphics g) call.

Well now i am curious about why you allways talk about the EDT? Every drawing Method is normally done within the EDT, except when you draw with a graphics object inside a thread that was created by you :).