I am currently learning Java Animation and graphics in NetBeans.
I decided to start off with a simple ball movement in JPanel.
I am having some problem with fixing the flickering a flickering problem. I have looked at many forums but most were for AWT using Double Buffering,but I came to know that SWING components don't need Double Buffering. I tried - using repaint()
and .clearRect()
.
Out of the 2 I found that using .clearRect()
gave me better results, but not seamless flicker-free animation all the time.So I wanted to know if there is a better way to eliminate flickering.
Here is my code:
public class NewJFrame extends javax.swing.JFrame {
int x;
int y;
int xspeed = 1;
int yspeed = 1;
int width;
int height;
Graphics g;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
g = jp.getGraphics();
width = jp.getWidth();
height = jp.getHeight();
final Timer timerCHK = new Timer();
timerCHK.schedule(new TimerTask() {
public void run() {
move();
time();
}
}, 1000, 10);
}
void time() {
final Graphics g = jp.getGraphics();
final Timer timerCHK = new Timer();
timerCHK.schedule(new TimerTask() {
public void run() {
g.clearRect(0, 0, jp.getWidth() - 3, jp.getHeight() - 3);
}
}, 1000, 12);
}
void move() {
x = x + xspeed;
y = y + yspeed;
Graphics mk = jp.getGraphics();
if (x < 0) {
x = 0;
xspeed = -xspeed;
} else if (x > width - 20) {
x = width - 20;
xspeed = -xspeed;
}
if (y < 0) {
y = 0;
yspeed = -yspeed;
} else if (y == height - 20) {
y = height - 20;
yspeed = -yspeed;
}
mk.drawOval(x, y, 20, 20);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
This is not an answer (per se) but an extended comment - the comments didn't have enough room
Unfortunately, not. I did a short 3D animation course a few years ago and applied many of the basic principles from that to my programming. While many of the theories are superfluous to what we do, they are still important to understand.
The problem you will face is not only do you need to have a good understanding of the animation theories, but also a good understanding of Swing's inner workings and APIs.
There are a number of good APIs available to help you in this regards, but you will still need to have an understanding of the animation theories to put them into piratical use.
Kirill Grouchnikov has done some excellent post on his Pushing Pixels website (to support his Trident animation engine) which might be a suitable compromise between the two disciplines.
Kirill also highlight the following articals (which I have to admit, I've not read read ... yet ;))
Updated with Swing side
For the Swing side of things, you will need to have a good understanding of...
Painting in Swing - check out Painting in AWT and Swing and Performing Custom Painting.
This is REALLY, REALLY important. This is one of the most common misunderstandings that developers have when they first start trying to perform animation in Swing.
You'll also need to understand Concurrency in Java and in particular, how it applies to Swing
I had a very similar flickering problem, I used
and solved all my problems let me know please if updateUI() solved your problem