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 );

}

}

[2872 byte] By [zuloa] at [2007-9-28 7:58:46]
# 1

so many threads for so little work.

import java.awt.*;

import java.applet.Applet;

public class Class1 extends Applet implements Runnable{

private Thread thread;

private boolean running = true;

int x = 1;

int y = 40;

int direction=1;

int changeDirection=-1;

Image minBufferImage;

Graphics minBufferGraphics;

public void init(){

}

public void run(){

while(running){

repaint();

move();

try{

thread.sleep(15);

}catch (InterruptedException e){}

}

}

public synchronized void start(){

thread = new Thread(this);

thread.start();

}

public synchronized void stop(){

running = false;

}

public void move(){

if(x>getSize().width-40 || x<0){

direction=direction*changeDirection;

}

x=x+direction;

}

public void paint(Graphics g){

g.setColor(Color.black);

g.drawOval(x,y,40,40);

}

public void update(Graphics g) {

if(minBufferImage==null){

minBufferImage=createImage(getSize().width,getSize().height);

}

minBufferGraphics=(minBufferImage.getGraphics());

minBufferGraphics.setColor(getBackground());

minBufferGraphics.fillRect(0,0,getSize().width,getSize().height);

paint(minBufferGraphics);

g.drawImage(minBufferImage,0,0,this);

}

}

the sleep method is used to slow down the animation, and to allow time for garbage collection. it doesn't need to be as high as it is, but a least a few millisecs is recommended.

outawatera at 2007-7-9 19:12:09 > top of Java-index,Other Topics,Java Game Development...
# 2

you might want to add a flag that lets your animation thread wait for paint() to finish to make shure paint() always draws the new frame, and that it draws it only once.

Does anyone know whether the JVM calls paint() several times if there are multiple repaint() calls queued up? or will the JVM collect them and do a single paint() call ?

Have a look at this thread for a laugh :

http://sillydog.org/forum/viewtopic.php?t=1190

public class ... {

private boolean frame_displayed;

//...

public void run()

{

//...

frame_displayed = false;

repaint();

try{

thread.sleep(15);

}catch (InterruptedException e){}

while(!frame_displayed)

thread.yield();

//...

}

public void paint()

{

frame_displayed = true;

}

}

Zyphrusa at 2007-7-9 19:12:09 > top of Java-index,Other Topics,Java Game Development...