A
Aaron Fude
Hi,
I'm 100% sure that this is an FAQ, but I can't seem to find answer.
Let's say I have 150 BufferedImages and I want to spit them out to a
JPanel at a rate of 15fps so I can have 10 seconds of relatively
smooth animation. I have a class VideoPanel that you can see below and
a separate Thread that calls "repaint" on the VideoPanel 15 times a
second. Now, the VideoPanel actually repaints when it wants to, so I
get about 1 frame a second and I see about every 15th frame. What's
the right way to fix this?
public class VideoPanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Image img = myImages[myCurrentFrame];
int w = img.getWidth(this);
int h = img.getHeight(this);
g.drawImage(img, 0, 0, w, h, 0, 0, w, h, this);
}
}
My Thread class is this:
public class Animate extends Thread {
public void run() {
while (true) {
myVideoPanel.repaint();
myCurrentFrame = (myCurrentFrame +1)%myImages.length;
try {
sleep(1000/15);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
I'm 100% sure that this is an FAQ, but I can't seem to find answer.
Let's say I have 150 BufferedImages and I want to spit them out to a
JPanel at a rate of 15fps so I can have 10 seconds of relatively
smooth animation. I have a class VideoPanel that you can see below and
a separate Thread that calls "repaint" on the VideoPanel 15 times a
second. Now, the VideoPanel actually repaints when it wants to, so I
get about 1 frame a second and I see about every 15th frame. What's
the right way to fix this?
public class VideoPanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Image img = myImages[myCurrentFrame];
int w = img.getWidth(this);
int h = img.getHeight(this);
g.drawImage(img, 0, 0, w, h, 0, 0, w, h, this);
}
}
My Thread class is this:
public class Animate extends Thread {
public void run() {
while (true) {
myVideoPanel.repaint();
myCurrentFrame = (myCurrentFrame +1)%myImages.length;
try {
sleep(1000/15);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}