animation thread not using repaint help
hi
I have made a simple applet moving a circle across the screen.
But the animation is jerky,I use buffering but the circle still
gets sudden bigger moves though it should only move 1 pixel at each repaint.
Could this be fixed with making a new thread that updates the position of the circle instead of updating it in paint() ?
And why is it normal to use sleep() in the animation thread, isnt it possible to just paint as fast as possible? instead of using sleep(25),whynot just tell the thread to wait for the "calculation" thread to be finished,and then continue? isn't that what the sleep() is for?waiting for the other stuff to be done so the rendering doesnt take all the processor time? So it would be better to just tell the animation thread to wait until the other thread is finished. This would make the animation be as fast as possible..and faster on a fast machine.
Any ideas?
Here is the source code to my applet test. If you want to comment then please do.
import java.awt.*;
import java.applet.Applet;
public class Class1 extends Applet implements Runnable
{
private Thread anim;
private Thread calc;
private boolean running = false;
private boolean running2 = false;
int x = 0;
int y = 40;
Dimension minBufferDimension;
ImageminBufferImage;
Graphics minBufferGraphics;
public void init()
{
minBufferDimension = getSize();
minBufferImage = createImage( 300, 300 );
minBufferGraphics = minBufferImage.getGraphics();
}
public void run()
{
Thread currentThread = Thread.currentThread();
while(currentThread == anim)
{
repaint();
try
{
anim.sleep(15);
}
catch (InterruptedException e){}
}
while(currentThread == calc)
{
x++;
try
{
calc.sleep(15);
}
catch (InterruptedException e) {}
}
}
public synchronized void start()
{
if(anim == null || !anim.isAlive())
{
anim = new Thread(this);
anim.setPriority(Thread.MIN_PRIORITY + 1);
}
if(calc == null || !calc.isAlive())
{
calc = new Thread(this);
calc.setPriority(Thread.MIN_PRIORITY + 1);
}
calc.start();
anim.start();
}
public synchronized void stop()
{
running = false;
running2 = false;
}
public void update( Graphics g )
{
paint(g);
}
public void paint(Graphics g)
{
minBufferGraphics.setColor( getBackground() );
minBufferGraphics.fillRect(0 , 0 , 300, 300 );
minBufferGraphics.setColor( Color.black );
minBufferGraphics.drawOval(x,y,40,40);
g.drawImage( minBufferImage, 0, 0, this );
}
}

