Hello friends, this is a Java applet program of moving car. In this java program draw a car. Car moves left to right direction on the screen continuously. Here use concepts of applet life cycle, multithreading.
Program Code: Applet_Animation.java
//Java Applet Animation Program
//Developed by: Om Prakash Vishnoi
import java.awt.*; //import package
import java.applet.*; // import package
/*
<applet code=Applet_Animation width=1000 height =500>
</applet>
*/
public class Applet_Animation extends Applet implements Runnable
{
int col,col1, row;
int angle;
Thread th;
int x,y;
public void init() //initilize applet cycle
{
col=50;
col1=col+220;
row=400;
angle=0;
repaint();
}
public void start() //start applet
{
th=new Thread(this);
th.start();
}
public void run() //run applet
{
for(int i=0;;i++)
{
try
{
Thread.sleep(100);
repaint();
}
catch(InterruptedException e){}
}
}
public void paint(Graphics g) //draw on screen
{
Color c1=new Color(255,255,255);
Color black = new Color(0,0,0);
Color c2=new Color(255,0,0);
Color c3=new Color(0,255,0);
Color c4=new Color(0,0,255);
Color c5=new Color(150,150,150);
Color c6=new Color(190,160,160);
g.setColor(c2);
g.drawString("Developed By: Om Prakash Vishnoi",100,100);
g.setColor(c1);
g.setColor(black);
//Car Designing
g.drawLine(col+47, row-4, col1-10, row-4); //connection line of wheel cover
g.drawLine(col-47, row-4, col-10, row-4); //back wheel cover to back line- engine down line
g.drawLine(col1+10, row-4, col1+80, row-4); //front wheel cover to front line
g.drawLine(col-47, row-4, col-47, row-60); //back panel line
//silencer
g.drawLine(col-47, row-7,col-57,row-7 );
g.drawLine(col-47, row-15,col-57,row-15 );
g.drawArc(col-58, row-15, 4, 8, 0, 360);
g.drawLine(col1+80, row-4, col1+80, row-60); //engine front line
g.drawLine(col-47, row-60, col, row-60); //back panel line
g.drawLine(col1+20, row-60, col1+80, row-60); //engine up line
g.drawArc(col, row-130, 240,140,15,165); //uper arc
g.drawLine(col1+18,321, col1+18,340);
//red light
if(col%5==0)
g.setColor(c2);
else
g.setColor(c1);
g.fillArc(col+150, row-145, 15,40, 0,180);
g.setColor(black);
g.drawArc(col+40, row-100, 50, 80, 0, 180); //back mirror
g.drawLine(col+40, row-60, col+90, row-60); //back mirror down line
g.drawArc(col1-70, row-100, 50,80,0,180); //front mirror
g.drawLine(col1-70, row-60,col1-20, row-60); //front mirror back line
//back wheel cover
g.fillArc(col-9, row-28, 58,50, 0, 180);
g.setColor(c1);
g.fillArc(col-5, row-25, 50,50, 0, 180);
//front wheel cover
g.setColor(black);
g.fillArc(col1-9, row-28, 58,50, 0, 180);
g.setColor(c1);
g.fillArc(col1-5, row-25, 50,50, 0, 180);
//back wheel
g.setColor(c3);
g.drawOval(col,row-20,40,40);
//back wheel spikes
g.setColor(c2);
x=(col+20)+(int)(20*Math.sin(angle));
y=(row)+(int)(20*Math.cos(angle));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+45));
y=(row)+(int)(20*Math.cos(angle+45));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+90));
y=(row)+(int)(20*Math.cos(angle+90));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+100));
y=(row)+(int)(20*Math.cos(angle+100));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+135));
y=(row)+(int)(20*Math.cos(angle+135));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+145));
y=(row)+(int)(20*Math.cos(angle+145));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+180));
y=(row)+(int)(20*Math.cos(angle+180));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+190));
y=(row)+(int)(20*Math.cos(angle+190));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+225));
y=(row)+(int)(20*Math.cos(angle+225));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+235));
y=(row)+(int)(20*Math.cos(angle+235));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+280));
y=(row)+(int)(20*Math.cos(angle+280));
g.drawLine(col+20, row, x, y);
x=(col+20)+(int)(20*Math.sin(angle+325));
y=(row)+(int)(20*Math.cos(angle+325));
g.drawLine(col+20, row, x, y);
//front wheel
g.setColor(c3);
g.drawOval(col1,row-20,40,40);
//front wheel spikes
g.setColor(c2);
x=(col1+20)+(int)(20*Math.sin(angle));
y=(row)+(int)(20*Math.cos(angle));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+45));
y=(row)+(int)(20*Math.cos(angle+45));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+90));
y=(row)+(int)(20*Math.cos(angle+90));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+100));
y=(row)+(int)(20*Math.cos(angle+100));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+135));
y=(row)+(int)(20*Math.cos(angle+135));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+145));
y=(row)+(int)(20*Math.cos(angle+145));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+180));
y=(row)+(int)(20*Math.cos(angle+180));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+190));
y=(row)+(int)(20*Math.cos(angle+190));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+225));
y=(row)+(int)(20*Math.cos(angle+225));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+235));
y=(row)+(int)(20*Math.cos(angle+235));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+280));
y=(row)+(int)(20*Math.cos(angle+280));
g.drawLine(col1+20, row, x, y);
x=(col1+20)+(int)(20*Math.sin(angle+325));
y=(row)+(int)(20*Math.cos(angle+325));
g.drawLine(col1+20, row, x, y);
String str=x+" "+y;
if(col%3==0)
{
g.setColor(c6);
g.fillOval(col-90, row-30, 5,8);
g.fillOval(col-90, row-15, 5,8);
g.fillOval(col-90, row, 5,8);
}
if(col%3==1)
{
g.setColor(c5);
g.fillOval(col-80, row-20, 3,5);
g.fillOval(col-80, row-5, 3,5);
}
if(col%3==2)
{
g.setColor(black);
g.fillOval(col-68, row-10, 3,4);
}
angle+=1;
if(col==1500)
{
col=-300;
col1=col+220;
}
else
{
col+=1;
col1+=1;
}
}
}
Output
Java Program Name: Applet_Animation.java