-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrowKeys.java
More file actions
122 lines (113 loc) · 3.71 KB
/
Copy pathArrowKeys.java
File metadata and controls
122 lines (113 loc) · 3.71 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
118
119
120
121
122
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* Creates a GUI of buttons for controlling main player
*
* @Jonathan Ke
* @9/13/2019
*/
public class ArrowKeys extends JPanel {
private Player player;
private JButton left;
private JButton right;
private JButton up;
private JButton down;
private Boolean[] isClicked = new Boolean[]{false,false,false,false};
private static int UP = KeyEvent.VK_UP;
private static int DOWN = KeyEvent.VK_DOWN;
private static int RIGHT = KeyEvent.VK_RIGHT;
private static int LEFT = KeyEvent.VK_LEFT;
public ArrowKeys(Player p, MainScreen m){
super();
player = p;
left = new JButton("LEFT");
right = new JButton("RIGHT");
up = new JButton("UP");
down = new JButton("DOWN");
ActionListener l = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if (isClicked[0]){
isClicked[0] = false;
player.stop();
} else{
for (int i = 0; i < isClicked.length; i++){
isClicked[i] = false;
}
player.stop();
isClicked[0] = true;
player.keyMove(LEFT);
}
m.getFrame().requestFocus();
}
};
ActionListener r = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if (isClicked[1]){
isClicked[1] = false;
player.stop();
} else{
for (int i = 0; i < isClicked.length; i++){
isClicked[i] = false;
}
player.stop();
isClicked[1] = true;
player.keyMove(RIGHT);
}
m.getFrame().requestFocus();
}
};
ActionListener u = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if (isClicked[2]){
isClicked[2] = false;
player.stop();
} else{
for (int i = 0; i < isClicked.length; i++){
isClicked[i] = false;
}
player.stop();
isClicked[2] = true;
player.keyMove(UP);
}
m.getFrame().requestFocus();
}
};
ActionListener d = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
if (isClicked[3]){
isClicked[3] = false;
player.stop();
} else{
for (int i = 0; i < isClicked.length; i++){
isClicked[i] = false;
}
player.stop();
isClicked[3] = true;
player.keyMove(DOWN);
}
m.getFrame().requestFocus();
}
};
left.addActionListener(l);
right.addActionListener(r);
up.addActionListener(u);
down.addActionListener(d);
this.add(left);
this.add(right);
this.add(up);
this.add(down);
}
public static void main(String[] args){
JFrame f = new JFrame("POKEMON ADVENTURE");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.pack();
f.setFocusable(true);
f.setVisible(true);
}
}