Animating a line
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Garrett_Allen
Posted On:   Thursday, July 24, 2003 09:49 AM


I can draw a line on a canvas, and then run a for loop to make it move, but it draws every frame on the canvas at once, instead of each frame at a time.


How do I make it display each frame, in order to animate the moving line?

Re: Animating a line

Posted By:   Anonymous  
Posted On:   Thursday, September 18, 2003 12:44 AM

This a java , you have to learn to use Threads, if you wantto write multithread programs.

check the code below, use your imagination, and u'll get the result.

public class Animator extends Frame implements Runnable{

int x=0;
int y=0;
public Animator(){
a.setLayout(null);
a.resize(1000,1000);
}
public void run(){
while(true){
try{
Thread.currentThread().sleep(200);
x=x+20;
y=y+20;
repaint();
}catch(Exception ignore){}
}
}
public void paint(Graphics g){
g.drawLine(x,y,x+50,y+50);
}
public static void main(String [] args){
Animator a=new Animator();
Thread t=new Thread(a);
a.show();
t.start();
}
}

Re: Animating a line

Posted By:   Stephen_McConnell  
Posted On:   Friday, July 25, 2003 05:29 AM

You have to put some wait state in-between each draw.


Usually, you create a thread that redraws the canvas. Then, you put it to sleep for a few milli seconds between each draw.


This way, you can go on with executing something else in your program while the thread takes care of the animation. Just running a loop to redraw it does so faster than one wants (unless you are programming on the Apple II then it took forever... even in 6502 Assembler Language).


Stephen McConnell

About | Sitemap | Contact