صممت برنامج applet ويقوم بتحريك سيارة بالاعتماد على التسارع المأخوذ من معادلة الحركةوأريده أن يقوم بعمل الآتي
اللخمس ثواني الأولى التسارع متزايد
الخمس التي تليها التسارع منتظم الخمس التي تليها متناقص
بدل ان يتسارع بعجلة منتظمة
a1 and ddtt =0.0 to 5.0 متزايدة
a2 and ddtt=6.0 to 10.0 منتظمة
a3 and ddtt =10.0 to 15.0 متناقصة
//Sourcecode
import java.awt.*;
import java.applet.*;
public class M_Sprite1 extends Applet implements Runnable
{
Thread runner;
private Image Buffer;
private Graphics gBuffer;
Image background, snail, snail1;
/////////////////////////////////////////////////////////////////////////////////////////////
private double dt=0.07,dtdt=0.0,fX = 1.0, fVx = 0.0, a=0.00;
private double[] AA = new double[2];
// direction of animated arc.
private int F = 1;
private int B = 0;
private int x=0, y=0; // position animated
private int sw,sh;
private int direction = B;
/////////////////////////////////////////////////////////////////////////////////////////////
//Init is called first, do any initialisation here
public void init()
{
sw = size().width; sh=size().height; ////////////////////
//create graphics buffer, the size of the applet
Buffer=createImage(size().width,size().height);
//System.out.println("W = "+sw);
gBuffer=Buffer.getGraphics();
background=getImage(getCodeBase(),"road3.png");
snail=getImage(getCodeBase(),"ncar1.png");
snail1=getImage(getCodeBase(),"car2.png");
}
public void start()
{
if (runner == null)
{
runner = new Thread (this);
runner.start();
}
}
public void stop()
{
if (runner != null)
{
runner.stop();
runner = null;
}
}
public void run()
{
while(true)
{
//Thread sleeps for 15 milliseconds here
try {runner.sleep(15);}
catch (Exception e) { }
//System.out.println("W = "+sw);
gBuffer.drawImage(background,0,30,this);
AA= calcPosition (dt);
/////////////////////////////////////////////////////////////////////////////////////////////
// direction animated
if (x >= sw && direction == F) {direction = B;x= 0;} {dtdt+=dt;}
if (x<= 0 && direction == B) direction = F;
// angle start
if (direction == F) {
x +=AA[1]; y = 0;
}
// if (direction == B ) {
// x -=AA[1]; y = 0;
// }
/////////////////////////////////////////////////////////////////////////////////////////////
gBuffer.drawImage(snail,x,100,this);
gBuffer.drawImage(snail1,x,50,this);
repaint();
}
}
//is needed to avoid erasing the background by Java
public void update(Graphics g)
{
paint( g );
}
public void paint(Graphics g)
{
Dimension d = getSize();
//step(d.width, d.height);
g.drawImage (Buffer,0,0, this);
}
public double[] calcPosition (double ddtt) {
double[] A = new double[2];
fX += fVx* ddtt+a*ddtt*ddtt/2;
fVx += a * ddtt;
for(ddtt=0;ddtt<=5.0;ddtt++)
fVx = +Math.abs(fVx);
for(ddtt=6.0;ddtt<=10.0;ddtt++)
fVx= -Math.abs(fVx);
System.out.println ( "\n\n\tfX = "+ fX + ", fVx = " + fVx );
A[1] = fX;A[0]=fVx;
return A;
}
}