Hi
I’d like to do some simple animated graphics on a JPanel. Does anyone know of an online example that includes (normal application, not an applet) some double-buffering and some threading? All the examples I’ve found have been applets.
Thanks.
Hi
I’d like to do some simple animated graphics on a JPanel. Does anyone know of an online example that includes (normal application, not an applet) some double-buffering and some threading? All the examples I’ve found have been applets.
Thanks.
here is a fairly simple example of a red sqaure bouncing around on a black background. Double-buffering is done automatically btw. If you want smoother animation, you will need to make sure that the main loop takes as close to the same amount of time to run each time as possible. Other technologies used to get better performance or quality with java2d are the BufferStrategy class, managed images, and the fullscreen exclusive mode. There is tons written about these technologies already, so just check out google first. Heres the code:
import javax.swing.*;
import java.awt.*;
public class MyClass implements Runnable
{
private JFrame mainFrame;
private JPanel drawingPanel;
private boolean running;
private Rectangle rect;
private int xDirection;
private int yDirection;
private int xSpeed;
private int ySpeed;
public MyClass()
{
rect = new Rectangle(50, 50, 50, 50);
mainFrame = new JFrame("Test");
drawingPanel = new DrawingPanel();
drawingPanel.setPreferredSize(new Dimension(640, 480));
mainFrame.setContentPane(drawingPanel);
mainFrame.pack();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
xDirection = 1;
yDirection = 1;
xSpeed = 2;
ySpeed = 2;
Thread mainLoop = new Thread(this);
mainLoop.start();
}
public void run()
{
running = true;
while(running)
{
updatePositions();
drawingPanel.repaint();
try
{
Thread.sleep(15);
} catch(InterruptedException ex) {
ex.printStackTrace();
}
}
}
private void updatePositions()
{
rect.x += xDirection*xSpeed;
rect.y += yDirection*ySpeed;
if(rect.x < 0)
{
rect.x = 0;
xDirection = 1;
}
else if(rect.x+rect.width > drawingPanel.getWidth())
{
rect.x = drawingPanel.getWidth()-rect.width;
xDirection = -1;
}
if(rect.y < 0)
{
rect.y = 0;
yDirection = 1;
}
else if(rect.y+rect.height > drawingPanel.getHeight())
{
rect.y = drawingPanel.getHeight()-rect.height;
yDirection = -1;
}
}
public static void main(String[] args)
{
new MyClass();
}
class DrawingPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
//draw black background
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, drawingPanel.getWidth(), drawingPanel.getHeight());
//draw red rectangle
g2.setColor(Color.RED);
g2.fillRect(rect.x, rect.y, rect.width, rect.height);
}
}
}
Thank you! 
Is it possible to do double buffering with JPanel? Cause the above example uses the drawing tools from the Java Package…
as for me I am having trouble with mine… cause I am using a buffer image… the background(buffer image) does not flicker, but as for my character (buffer image too) will flicker everytime…
can someone explain to me?
Then another question is that , can we use BufferStrategy on JPanel… cause my images does not flicker on windows but on the Panel…
Cheers!
You can use BufferStrategy on a JFrame, but not directly on a JPanel. My somewhat inelegant solution is to use java.awt.Canvas as a drawing surface, since that supports setting a BufferStrategy separate of that belonging to its ancestral frame, while the rest of the application (stuff which doesn’t require constant animation) uses swing’s own double buffering system.
Also, the flickering depends on how you draw it. If you go all the way through swing (using only repaint(), for example, though that is less-than-optimal for one who wants high framerates) then it shouldn’t flicker, but we don’t know what you do so it’s a bit difficult.
Heavy weight components only draw themselfs lightweight components get drawn by there parent