Java Applet Program to Draw a Car
x
87
87
1
import java.applet.*;
2
import java.awt.*;
3
import java.awt.event.*;
4
public class Car extends Applet implements ActionListener
5
{
6
int x, y, z;
7
int t1,t2;
8
Button b1, b2;
9
String msg=" ";
10
void slep()
11
{
12
try
13
{
14
Thread.sleep(100);
15
}
16
catch(Exception ex)
17
{
18
}
19
}
20
public void init()
21
{
22
t1=0;
23
t2=1;
24
x=20;
25
y=60;
26
setLayout(new FlowLayout(FlowLayout.CENTER));
27
Label l=new Label("webeduclick");
28
b1=new Button("Forward");
29
add(b1);
30
b2=new Button("Stop");
31
add(b2);
32
b1.addActionListener(this);
33
b2.addActionListener(this);
34
}
35
public void start()
36
{
37
}
38
public void actionPerformed(ActionEvent e)
39
{
40
String s=e.getActionCommand();
41
if(s.equals("Forward"))
42
{
43
msg="Forward";
44
repaint();
45
}
46
else if(s.equals("Stop"))
47
{
48
msg=" ";
49
repaint();
50
}
51
}
52
public void paint(Graphics g)
53
{
54
setBackground(Color.black);
55
z=getWidth();
56
Color c1=new Color(20,160,200);
57
Color c2=new Color(200,60,200);
58
g.setColor(c1);
59
g.drawLine(0,y+75,z,y+75);
60
g.setColor(Color.red);
61
g.fillRoundRect(x,y+20,100,40,5,5);
62
g.fillArc(x+90,y+20,20,40,270,180);
63
g.setColor(Color.BLUE);
64
g.fillRoundRect(x+10,y,70,25,10,10);
65
g.setColor(Color.white);
66
g.fillRect(x+20,y+5,20,25);
67
g.fillRect(x+50,y+5,20,25);
68
g.setColor(Color.black);
69
g.fillRoundRect(x+55,y+10,10,20,10,10);
70
g.fillOval(x+10,y+50,25,25);
71
g.fillOval(x+60,y+50,25,25);
72
g.setColor(Color.white);
73
g.fillOval(x+15,y+55,10,10);
74
g.fillOval(x+65,y+55,10,10);
75
x=x+10;
76
slep();
77
if(msg.equals("Forward"))
78
{
79
if(x+120<z)
80
{
81
x=x+1;
82
showStatus("Press Forward for Starting Car");
83
repaint();
84
}
85
}
86
}
87
}
Output: