-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
executable file
·118 lines (91 loc) · 2.08 KB
/
MainClass.java
File metadata and controls
executable file
·118 lines (91 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class MyKeyListener extends Frame implements KeyListener{
TextArea area;
String text;
MyKeyListener(){
area = new TextArea();
area.setBounds(0, 30, 50, 50);
area.addKeyListener(this);
add(area);
setSize(50,70);
setLayout(null);
setVisible(true);
}
//Abstract Method overriding
public void keyPressed(KeyEvent e) {
area.setText("");
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
text = area.getText();
System.out.println("Key pressed : "+text);
}
public String getInput() {
return text;
}
}
public class MainClass extends Applet implements Runnable {
int x=165, y=510;
Image road;
Image car;
MyKeyListener kle;
public void init () {
kle = new MyKeyListener();
(new Thread(this)).start();
road = getImage (getDocumentBase(), "road.jpg");
car = getImage (getDocumentBase(), "PlayerCar.png");
}
public void paint (Graphics G) {
//drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
G.drawImage(road, 30, 30, 500, 600, null);
G.drawImage(car, x, y, 65, 134, null);
//System.out.println(x+":"+y);
}
public void run () {
String inp;
while (true) {
//Getting user controls
inp = kle.getInput();
if (inp != null){
if(inp.equals("a")) //Moving Left
{
if (x > 165) x -= 5;
else x = 165;
}
else if(inp.equals("d")) //Moving Right
{
if (x < 330) x += 5;
else x = 330;
}
else if(inp.equals("w")) //Moving Up
{
if (y > 30) y -= 2;
else y = 30;
}
else if(inp.equals("s")) //Moving Down
{
if (y < 510) y += 3;
else y = 510;
}
}
y -= 1; //Speed
repaint();
//Delay
try{
Thread.sleep(100);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
<applet code = "MainClass.class"
height = 700
width = 600>
</applet>
*/