-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyAndMouse.java
More file actions
347 lines (283 loc) · 8.23 KB
/
KeyAndMouse.java
File metadata and controls
347 lines (283 loc) · 8.23 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Parth Upadhyay
// 3/9/2022
// KeyAndMouse.java
// To undertand and use keyboard and mouse listeners
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
public class KeyAndMouse
{
int num = 0;
JFrame frame;
DrawingPanel canvas;
MyPanel panel;
public static void main (String[] args)
{
KeyAndMouse kt = new KeyAndMouse();
kt.question();
kt.run();
} // end main
public void question()
{
Scanner in = new Scanner (System.in);
System.out.println("Do you want to run mouse(1) or key(2)?");
num = in.nextInt();
}
public void run()
{
frame = new JFrame("KeyAndMouse.java");
if (num == 2)
{
DrawingPanel canvas = new DrawingPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 600, 600);
frame.setLocation(400,50);
frame.setResizable(true);
frame.setContentPane(canvas);
frame.setVisible(true);
}
if (num == 1)
{
MyPanel panel = new MyPanel();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize( 600, 600);
frame.setLocation(400,50);
frame.setResizable(true);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.addMouseListener(panel);
frame.addMouseMotionListener(panel);
frame.setVisible(true);
}
}
}
class MyPanel extends JPanel implements MouseListener, MouseMotionListener
{
private int xloc, yloc;
private int width, height;
private boolean dragging;
private int xMouse, yMouse;
private Rectangle rect;
boolean entered = false;
public MyPanel()
{
xloc = yloc = 100;
dragging = false;
xMouse = yMouse = 0;
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // execute the superclass method first
width = getWidth(); // width of JPanel
height = getHeight(); // height of JPanel
setBackground(Color.white);
g.setColor(Color.BLACK);
g.setFont(new Font("Serif", Font.BOLD, 36));
if (entered == true)
g.setColor(Color.PINK);
if (entered == false)
g.setColor(Color.BLACK);
if (dragging == true)
g.drawString("Dragging", 100, 50);
if (dragging == false)
g.drawString("Not Dragging", 100, 50);
g.fillRect(xloc, yloc, 150, 150);
int yoffset = 24;
rect = new Rectangle(xloc, yloc+yoffset, 150, 150);
} // end paintComponent
public void mousePressed (MouseEvent e) {
xMouse = e.getX();
yMouse = e.getY();
// determine if mouse is pressed inside drawn rectangle
if (rect.contains(e.getX(), e.getY())) dragging = true;
}
public void mouseReleased (MouseEvent e) {
////////////////////////////////////////
// Stop dragging
dragging = false; // stop dragging
}
public void mouseClicked (MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mouseMoved (MouseEvent e)
{
xMouse = e.getX();
yMouse = e.getY();
if (rect.contains(e.getX(), e.getY())) entered = true;
if (!rect.contains(e.getX(), e.getY())) entered = false;
repaint();
}
public void mouseDragged (MouseEvent e) {
if (dragging) {
xloc = xloc + (e.getX() - xMouse);
yloc = yloc + (e.getY() - yMouse);
xMouse = e.getX(); // reset mouse to new location
yMouse = e.getY();
repaint(); // repaint when dragging
}
}
}
class DrawingPanel extends JPanel implements KeyListener {
private int ballX, ballY;
private int carX, carY;
private int car2X;
private boolean ballMoveIt; // starts and stops ball movement
private boolean carMoveIt; // starts and stops car movement
private Timer balltimer, cartimer, cartimer2;
boolean shift = false;
boolean up = false;
boolean down = false;
int speed = 0;
int speed2 = 0;
int ballspeed = 0;
public DrawingPanel() {
ballX = 200;
ballY = 10;
carX = 10;
carY = 400;
car2X = 400;
ballMoveIt = carMoveIt = true;
addKeyListener(this);
// create timer for animation of ball
BallMover ballmover = new BallMover();
balltimer = new Timer(5, ballmover);
balltimer.start();
// Format --> timer = new Timer( millisDelay, listener );
CarMover carmover = new CarMover();
cartimer = new Timer(10, carmover);
cartimer.start();
CarMover carmover2 = new CarMover();
cartimer2 = new Timer(10, carmover2);
cartimer2.start();
}
class BallMover implements ActionListener {
public void actionPerformed(ActionEvent e) {
// ball XY location
ballY++;
ballY += ballspeed;
if (ballspeed < 0)
ballspeed = 0;
if (ballY > 600) {
ballY = 10;
ballX = (int)(Math.random() * 600) + 1;
}
repaint();
grabFocus();
}
} // end BallMover
class CarMover implements ActionListener {
public void actionPerformed(ActionEvent e) {
// car XY location
carX ++;
car2X--;
if (speed2 < 0)
speed2 = 0;
if (speed < 0)
speed = 0;
carX += speed;
car2X -= speed2;
if (carX > 560 || car2X < 0){ carX = 0; car2X = 600;}
repaint();
grabFocus();
}
} // end CarMover
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.gray);
g.setColor(Color.red);
g.setFont(new Font("Serif", Font.BOLD, 36));
g.drawString("Press any number for ball", 100, 50);
g.drawString("Press any vowel for car", 100, 100);
g.drawString("Press shift to change ball color", 100, 150);
g.drawString("Change speed of orange car by up/down", 100, 200);
g.drawString("Change speed of ball by left/right", 100, 250);
g.drawString("Press zxcvhnm for blue car", 100, 300);
g.drawString("Change speed blue car by f/s", 100, 350);
// paint ball
g.setColor(Color.PINK);
if (shift == true)
g.setColor(Color.GREEN);
if (shift == false)
g.setColor(Color.PINK);
g.fillOval(ballX, ballY, 30, 30);
// paint car
g.setColor(Color.orange);
g.fillOval(carX+2, carY, 20, 20); // back wheel
g.fillOval(carX+40, carY, 20, 20); // front wheel
g.fillRect(carX, carY-20, 60, 20); // car body
g.fillRect(carX+10, carY-35, 40, 15); // car top
//car #2
g.setColor(Color.BLUE);
g.fillOval(car2X+2, carY, 20, 20); // back wheel
g.fillOval(car2X+40, carY, 20, 20); // front wheel
g.fillRect(car2X, carY-20, 60, 20); // car body
g.fillRect(car2X+10, carY-35, 40, 15); // car top
} // end paintComponent
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
if (carMoveIt) cartimer.stop();
else cartimer.start();
carMoveIt = ! carMoveIt;
break;
case 'z':
case 'x':
case 'c':
case 'v':
case 'b':
case 'n':
case 'm':
if (carMoveIt) cartimer2.stop();
else cartimer2.start();
carMoveIt = ! carMoveIt;
break;
case 'f':
speed2 ++;
break;
case 's':
speed2 --;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (ballMoveIt) balltimer.stop();
else balltimer.start();
ballMoveIt = ! ballMoveIt;
break;
}
} // end keyPressed
public void keyPressed(KeyEvent e)
{
int c = e.getKeyCode();
switch(c)
{
case KeyEvent.VK_SHIFT:
shift = !shift;
break;
case KeyEvent.VK_UP:
speed++;
break;
case KeyEvent.VK_DOWN:
speed--;
break;
case KeyEvent.VK_LEFT:
ballspeed --;
break;
case KeyEvent.VK_RIGHT:
ballspeed ++;
break;
}
}
public void keyReleased(KeyEvent e) {}
}