answersLogoWhite

0


Best Answer

import java.awt.*;

import java.util.*;

import java.applet.*;

/*

<APPLET CODE="Animation.JAVA" WIDTH=400 HEIGHT=300>

</APPLET>

*/

//The basic applet class.The applet shows 4 cars crossing each other at a square.

public class Animation extends Applet implements Runnable

{

Thread t;

//4 variables used to vary the car's positions.

int x1=0,x2=380,y1=50,y2=250;

public void start()

{

if(t==null)

{

t=new Thread(this,"New Thread");//New side Thread created on start of applet.

t.start();

}

}

public void stop()

{

if(t!=null)

{

t=null;//On stop of applet the created thread is destroyed.

}

}

//Implementation of method run() of Runnable interface.

public void run()

{

Thread t1=Thread.currentThread();

while(t==t1)

{

repaint();

try

{

Thread.sleep(100);

}

catch(Exception e)

{ }

}

}

public void paint(Graphics g)

{

setBackground(Color.cyan);

g.setColor(Color.BLACK);

x1=(x1+16)%400;

x2=x2-16;

y1=(y1+12)%300;

y2=y2-12;

if(y2<0)

y2=288;

if(x2<0)

x2=384;

//Draw the roads using 2 filled rectangles using black color.

g.fillRect(0,130,400,40);

g.fillRect(180,0,40,305);

//Draw the white colored lines.

g.setColor(Color.white);

for(int i=0;i<20;i++)

{

if(i!=9 && i!=10)

g.drawLine(i*20,150,i*20+10,150);

}

for(int j=0;j<15;j++)

{

if(j!=7 && j!=8)

g.drawLine(200,j*20,200,j*20+10);

}

//Draw 4 colored cars using filled round rectangles.

g.setColor(Color.red);

g.fillRoundRect(x2,152,20,8,2,2);

g.fillRoundRect(x1,140,20,8,2,2);

g.fillRoundRect(190,y1,8,20,2,2);

g.fillRoundRect(202,y2,8,20,2,2);

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

If people want to give answer for what is data abstraction, people will use car as an best example.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Java program to simulate a car?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.


What is java program on computers?

#!/usr/bin/perl print 'java program';


What powder game doesnt need java?

Any program written for the Java technology needs Java. Any program NOT written for Java DOESN'T need it.


Java Track?

Java Track is the java training program offered by SynergisticIT


Where can one get the program Java?

The program Java can be downloaded from the official Java website on the internet. The program is free for personal use, so it can be downloaded without any cost by anyone.


What is a Java applet?

PCH answer small Embedded Java Program.


Do I need Java?

No, if a program needs Java it'll specify that it does in the installer package for the program or on the website for the program or a section usually called "Requirements" for a specific program.


What is a java programming?

A java program is a program that is coded and run in the programming language called java. Java is similar to c++ in structure, and is more common in web apps. C++ is the equivalent for more heavy duty programs such as most software used to compose a java program.


Where java program started?

If it is already compiled, you can start your Java program from the command line. Just type java myclass replacing "myclass" with the program you want to start - a program with the ".class" extension, for example, myclass.class.


Can we add website link when we write java program?

yes ,i can add the website link in java program when we write.


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'