-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyAndMouse.java
More file actions
445 lines (402 loc) · 9.4 KB
/
KeyAndMouse.java
File metadata and controls
445 lines (402 loc) · 9.4 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//Name: Rishi Saravanan
//Date: 3/9/22
//Program Name: KeyAndMouse
//Program Goal- Use mouseListener and keyListener
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import javax.swing.*;
public class KeyAndMouse{
JFrame keyFrame;
JFrame mouseFrame;
DrawingPanel canvas2;
MousePanel canvas1;
Scanner s1 = new Scanner(System.in);
public static void main(String[] args) {
KeyAndMouse kt = new KeyAndMouse();
kt.Run();
} // end main
public void Run() {
System.out.println("Would you like to run mouse(1) or key(2)? ");
int choice = s1.nextInt();
if (choice == 1) {
System.out.println("Here you go.");
mouseFrame = new JFrame("SimpleJFrame");
mouseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas1 = new MousePanel();
mouseFrame.getContentPane().add(canvas1, BorderLayout.CENTER);
canvas1.addMouseListener(canvas1);
canvas1.addMouseMotionListener(canvas1);
mouseFrame.setSize(500, 500);
mouseFrame.setVisible(true);
} else if (choice == 2) {
System.out.println("Here you go.");
keyFrame = new JFrame("KeyTimer");
keyFrame.setSize(600, 600);
keyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas2 = new DrawingPanel();
keyFrame.getContentPane().add(canvas2);
keyFrame.setVisible(true);
} else {
System.out.println("Invalid option. Please enter 1 or 2");
}
}
}
class MousePanel extends JPanel implements MouseListener, MouseMotionListener
{
private int xloc, yloc;
private int width, height;
private boolean dragging;
private int xMouse, yMouse;
private Rectangle rect;
private boolean clicked;
public MousePanel()
{
xloc = yloc = 100;
dragging = false;
xMouse = yMouse = 0;
clicked = false;
}
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);
if(clicked) {
g.setColor(Color.PINK);
clicked = false;
} else {
g.setColor(Color.BLUE);
}
g.fillRect(xloc, yloc, 150, 150);
int yoffset = 24;
rect = new Rectangle(xloc, yloc+yoffset, 150, 150);
g.setColor(Color.BLACK);
if(dragging) {
g.drawString("Dragging", 10, 40);
} else {
g.drawString("Not Dragging", 10, 40);
}
} // 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
repaint(); // repaint when dragging
}
public void mouseClicked (MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
public void mouseMoved (MouseEvent e) {
if (!rect.contains(e.getX(), e.getY())) {
dragging = false;
} else {
clicked = true;
}
repaint(); // repaint when dragging
}
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();
}
}
class DrawingPanel extends JPanel implements KeyListener {
private int ballX, ballY;
private int carX1, carY1, carX2;
private boolean ballMoveIt; // starts and stops ball movement
private boolean carMoveIt1;
private boolean carMoveIt2;
// starts and stops car movement
private Timer balltimer, cartimer1, cartimer2;
boolean shiftClicked = false;
boolean upClicked = false;
boolean downClicked = false;
int speed1 = 0;
int speed2 = 0;
int ballSpeed = 0;
public DrawingPanel() {
ballX = 200;
ballY = 10;
carX1 = 10;
carY1 = 400;
ballMoveIt = true;
carMoveIt1 = true;
carMoveIt2 = true;
addKeyListener(this);
// create timer for animation of ball
BallMover ballmover = new BallMover();
balltimer = new Timer(5, ballmover);
balltimer.start();
CarMover carmover1 = new CarMover();
CarMover carmover2 = new CarMover();
cartimer1 = new Timer(10, carmover1);
cartimer1.start();
cartimer2 = new Timer(10, carmover2);
cartimer2.start();
}
class BallMover implements ActionListener {
public void actionPerformed(ActionEvent e) {
// ball XY location
ballY++;
if (ballSpeed <= 0) {
ballSpeed = 0;
}
if(ballMoveIt == true) {
ballY += ballSpeed;
}
if (ballY > 470) {
ballY = 10;
ballX = (int) (Math.random() * 470) + 1;
}
repaint();
grabFocus();
}
} // end BallMover
class CarMover implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (carMoveIt2 == true) { // car shouldn't move
carX2++;
carX2+= speed2;
}
if (carMoveIt1 == true) {
// car XY location
carX1++;
carX1 += speed1;
}
if (speed1 <= 0) {
speed1 = 0;
}
if (speed2 <= 0) {
speed2 = 0;
}
if (carX1 > 490)
carX1 = 0;
if (carX2 > 490)
carX2 = 0;
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, 14));
g.drawString("Press any number to stop/start the ball", 100, 50);
g.drawString("Press any vowel to stop/start car 1", 100, 100);
g.drawString("Shift key changes ball color.", 100, 150);
g.drawString("Up down changes car1 speed. Left and right changes ball speed.", 100, 200);
g.drawString("Car2 start/stops when letter from zxcvbnm is pressed", 100, 250);
g.drawString("f makes car2 faster, s makes car2 slower", 100, 300);
// paint ball
if (shiftClicked == true) {
g.setColor(Color.green);
} else {
g.setColor(Color.pink);
}
g.fillOval(ballX, ballY, 30, 30);
// paint car
g.setColor(Color.orange);
g.fillOval(carX1 + 2, carY1, 20, 20); // back wheel
g.fillOval(carX1 + 40, carY1, 20, 20); // front wheel
g.fillRect(carX1, carY1 - 20, 60, 20); // car body
g.fillRect(carX1 + 10, carY1 - 35, 40, 15); // car top
g.setColor(Color.blue);
g.fillOval(600 - (carX2 + 2), carY1, 20, 20); // back wheel
g.fillOval(600 - (carX2 + 40), carY1, 20, 20); // front wheel
g.fillRect(600 - (carX2 + 40), carY1 - 20, 60, 20); // car body
g.fillRect(600 - (carX2 + 28), carY1 - 35, 40, 15); // car top
} // end paintComponent
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
switch (c) {
case 'a':
if (carMoveIt1)
cartimer1.stop();
else
cartimer1.start();
carMoveIt1 = !carMoveIt1;
break;
case 'e':
if (carMoveIt1)
cartimer1.stop();
else
cartimer1.start();
carMoveIt1 = !carMoveIt1;
break;
case 'i':
if (carMoveIt1)
cartimer1.stop();
else
cartimer1.start();
carMoveIt1 = !carMoveIt1;
break;
case 'o':
if (carMoveIt1)
cartimer1.stop();
else
cartimer1.start();
carMoveIt1 = !carMoveIt1;
break;
case 'u':
if (carMoveIt1)
cartimer1.stop();
else
cartimer1.start();
carMoveIt1 = !carMoveIt1;
break;
case '1':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case '2':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case '3':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case '4':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case '5':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case '6':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case '7':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case '8':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case '9':
if (ballMoveIt)
balltimer.stop();
else
balltimer.start();
ballMoveIt = !ballMoveIt;
break;
case 'z':
if (carMoveIt2)
cartimer2.stop();
else
cartimer2.start();
carMoveIt2 = !carMoveIt2;
break;
case 'x':
if (carMoveIt2)
cartimer2.stop();
else
cartimer2.start();
carMoveIt2 = !carMoveIt2;
break;
case 'c':
if (carMoveIt2)
cartimer2.stop();
else
cartimer2.start();
carMoveIt2 = !carMoveIt2;
break;
case 'v':
if (carMoveIt2)
cartimer2.stop();
else
cartimer2.start();
carMoveIt2 = !carMoveIt2;
break;
case 'b':
if (carMoveIt2)
cartimer2.stop();
else
cartimer2.start();
carMoveIt2 = !carMoveIt2;
break;
case 'n':
if (carMoveIt2)
cartimer2.stop();
else
cartimer2.start();
carMoveIt2 = !carMoveIt2;
break;
case 'm':
if (carMoveIt2)
cartimer2.stop();
else
cartimer2.start();
carMoveIt2 = !carMoveIt2;
break;
case 'f':
speed2++;
break;
case 's':
speed2--;
break;
}
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_SHIFT:
shiftClicked = !shiftClicked;
break;
case KeyEvent.VK_UP:
speed1++;
break;
case KeyEvent.VK_DOWN:
speed1--;
break;
case KeyEvent.VK_LEFT:
ballSpeed--;
break;
case KeyEvent.VK_RIGHT:
ballSpeed++;
break;
}
}
public void keyReleased(KeyEvent e) {
}
}