forked from League-Archive/IntroToJavaWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcats.java
More file actions
108 lines (85 loc) · 2.09 KB
/
Copy pathcats.java
File metadata and controls
108 lines (85 loc) · 2.09 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
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Label;
import java.awt.event.KeyEvent;
import org.jointheleague.graphical.robot.Robot;
import org.jointheleague.graphical.robot.RobotWindow;
public class cats implements KeyEventDispatcher
{
Robot robot = new Robot();
private void feedTheRobot()
{
// 1. use the addFood() method to add food for the robot
addFood(50,50);
}
{
// TODO Auto-generated method stub
}
private void goUp()
{
// 2. Print “going up”. Test it out by running your code and pressing the up key
// 3. Make the robot move up the screen
}
private void goDown()
{
// 4. make the robot move down the screen
}
private void goLeft()
{
// 5. make the robot move left
}
private void goRight()
{
// 6. make the robot move right
}
private void checkIfFoodFound() throws Exception
{
int robotLocationX = robot.getX();
int robotLocationY = robot.getY();
// 7. Print out the variables for robotLocationX and robotLocationY
// 8. if robot is at same location as food
// print "chomp"
// say “chomp” with Runtime.getRuntime().exec("say chomp");
// call the eatFood() method
}
private void eatFood() {
component.setText("");
}
private void addFood(int x, int y)
{
component.setLocation(x, y);
window.add(component);
}
RobotWindow window = robot.getWindow();
Label component = new Label("*");
public static void main(String[] args)
{
cats feeder = new cats();
feeder.controlTheRobot();
feeder.feedTheRobot();
}
private void controlTheRobot()
{
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
robot.show();
robot.setSpeed(10);
}
public boolean dispatchKeyEvent(KeyEvent e)
{
if (e.getID() == KeyEvent.KEY_PRESSED)
{
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
goRight();
if (e.getKeyCode() == KeyEvent.VK_LEFT)
goLeft();
if (e.getKeyCode() == KeyEvent.VK_UP)
goUp();
if (e.getKeyCode() == KeyEvent.VK_DOWN)
goDown();
try {
checkIfFoodFound();
} catch (Exception exception) {}
}
return false;
}
}