runnable & paint relation?
Dear all,
I have a concept question which is related to runnable and paint since I try to make 2D animation. My question is if I have a class which extend Applet/Canvas and implements Runnable, then what is the relation between runnable() & paint() ?
Let me make my question clear, I should use paint() to put a 2D shape on the frame and I should use runnable if I want to move the 2D shape.
- Do I have to use paint() before runnable()?
- Should I consider that runnable is a while loop?
thanks
import java.awt.*;
import java.awt.event.*;
/*###################################################*/
publicclass BoxShootextends Canvasimplements Runnable{
int dY,dX;
Image offScrImg;
Circle ball;
boolean initCanvas=true;
Thread drawThread;
int frameRate;
double deltaT;
double speedUp;
double gPhysical;
double physicalHeight;
/*--*/
//
public BoxShoot(){
setBackground(Color.white);
dY=240;
dX=196;
setSize(dX,dY);
init();
}
/*--*/
//
publicvoid init(){
// Get the drawing area of the applet
// Choose some parameter for the object to drop
int radius = dY/20;
int x0 = 2 * radius;
int y0 = 2 * radius;
// These parameters separate thread animation
// frame rates from the physical time of the
// falling object.
frameRate = 10;
speedUp = 1.0;
deltaT = frameRate/1000.0;
deltaT *= speedUp;
gPhysical = 980.;// = 9.8cm/s**2
physicalHeight = 200.0;
// Scale physical g units to the graphical pixel units
// Let the frame represent a 200cm in height.
double g = gPhysical * ( dY/physicalHeight);
// Create the object to drop
ball =new Circle(x0,y0,radius,dX,dY,g);
// set it's initial velocity
ball.VxInit= 20.0;
ball.VyInit= 0.0;
// Lowest velocity before declaring stopped
ball.Vxstop= 1.00;
ball.Vystop= 1.00;
//
}
/*--*/
publicvoid start(){
if (drawThread !=null){
stop();
}
ball.reset();
initCanvas=true;
drawThread =new Thread(this);
drawThread.start();
}
/*--*/
publicvoid stop(){
drawThread =null;
// Give this thread time to jump out of its loop in run()
try{
Thread.sleep(200);
}catch (InterruptedException e)
{}
}
/*--*/
publicvoid run(){
repaint();
while (drawThread !=null){
// Use sleep to pause between movements
try{
Thread.sleep(frameRate);
}catch (InterruptedException e)
{}
if( ball.xStop && ball.yStop){
stop();
break;
}
ball.move(deltaT);
repaint();
}
System.out.println("Drop finished");
}
/*--*/
publicsynchronizedvoid update( Graphics g ){
// Create an offscreen image and then get its graphics context
if ( offScrImg ==null )
offScrImg = createImage( dX, dY );
Graphics og = offScrImg.getGraphics();
// Now draw on the offscreen image.
paint( og );
// Don't bother to call paint, just draw the offscreen image
// to the screen.
g.drawImage(offScrImg, 0, 0,this);
// Get rid of the offscreen graphics context. Can't unclip a graphics
// context so have to get a new one next time around.
og.dispose();
}
/*--*/
publicsynchronizedvoid paint(Graphics g){
if( initCanvas ){
g.setColor(Color.white);
g.fillRect(0,0,dX,dY);
initCanvas=false;
}
ball.draw(g);
}
}

