1
+ import java .applet .*;
2
+ import java .awt .*;
3
+ import java .awt .event .*;
4
+
5
+ public class Car2 extends Applet implements KeyListener
6
+ {
7
+ void slep ()
8
+ {
9
+ try
10
+ {
11
+ Thread .sleep (100 );
12
+ }
13
+ catch (Exception ex )
14
+ {
15
+ }
16
+ }
17
+ int x =600 ,y =360 ;
18
+ public void paint (Graphics g )
19
+ {
20
+
21
+ addKeyListener (this );
22
+ setBackground (Color .gray );
23
+ g .setColor (Color .black );
24
+ g .drawOval (x -2 ,y +15 ,7 ,20 );
25
+ g .fillOval (x -2 ,y +15 ,7 ,20 );
26
+ g .drawOval (x -2 ,y +75 ,7 ,20 );
27
+ g .fillOval (x -2 ,y +75 ,7 ,20 );
28
+ g .setColor (Color .blue );
29
+ g .drawRoundRect (x ,y ,76 ,105 ,10 ,20 );
30
+ g .fillRoundRect (x ,y ,76 ,105 ,10 ,20 );
31
+ g .setColor (Color .black );
32
+ g .drawRect (x +7 ,y +75 ,62 ,18 );
33
+ g .fillRect (x +7 ,y +75 ,62 ,18 );
34
+ g .setColor (Color .red );
35
+ g .drawOval (x +4 ,y +95 ,6 ,9 );
36
+ g .drawOval (x +66 ,y +95 ,6 ,9 );
37
+ g .fillOval ( x +4 ,y +95 ,6 ,9 );
38
+ g .fillOval ( x +66 ,y +95 ,6 ,9 );
39
+ g .setColor (Color .black );
40
+ g .drawRoundRect (x +7 ,y +35 ,63 ,35 ,10 ,20 );
41
+
42
+ g .setColor (Color .yellow );
43
+ g .drawOval (x +4 ,y +1 ,9 ,15 );
44
+ g .drawOval (x +3 ,y +1 ,9 ,15 );
45
+ g .fillOval (x +63 ,y +1 ,9 ,15 );
46
+ g .fillOval (x +4 ,y +1 ,9 ,15 );
47
+
48
+ slep ();
49
+
50
+ }
51
+ public void keyPressed (KeyEvent ke ){
52
+ int KeyCode = ke .getKeyCode ();
53
+
54
+ switch (KeyCode )
55
+ {
56
+ case KeyEvent .VK_UP :
57
+ if (y >0 )
58
+ y =y -1 ;
59
+ break ;
60
+ case KeyEvent .VK_DOWN :
61
+ if (y <getHeight ()-105 )
62
+ y =y +1 ;
63
+ break ;
64
+ case KeyEvent .VK_LEFT :
65
+ if (x >0 )
66
+ x =x -1 ;
67
+ break ;
68
+ case KeyEvent .VK_RIGHT :
69
+ if (x <getWidth ()-76 )
70
+ x ++;
71
+ break ;
72
+ }
73
+ repaint ();
74
+ }
75
+
76
+ public void keyTyped (KeyEvent ke ) {
77
+
78
+ }
79
+
80
+ public void keyReleased (KeyEvent ke ){
81
+ }
82
+
83
+
84
+
85
+
86
+ }
0 commit comments