-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMousePractice.java
More file actions
128 lines (89 loc) · 2.74 KB
/
Copy pathMousePractice.java
File metadata and controls
128 lines (89 loc) · 2.74 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
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.Random;
public class MousePractice extends Applet implements MouseListener, ActionListener
{
boolean lights[][] = new boolean[7][7];
int mouseX = -200, mouseY = -200;
Button resetBtn = new Button();
Button toggleCustomColors = new Button();
Boolean customColorsEnabled = false;
Random rand = new Random();
String moveHistory = "";
public void resetGame()
{
for (int r = 1; r <= 5; r++)
for (int c = 1; c <= 5; c++)
lights[r][c] = false;
}
public void togglePiece(int row, int col)
{
lights[row][col] = !lights[row][col];
}
public void addRandomTiles()
{
int randSpot = rand.nextInt(3);
for (int r = 1; r <= 5; r++)
for (int c = 1; c <= 5; c++)
{
int randNum = rand.nextInt(2);
if (randNum == 0) {
lights[r][c] = false;
} else {
lights[r][c] = true;
}
}
}
public Color randColor() {
int randNum = rand.nextInt(4);
if (randNum == 0) return Color.red;
if (randNum == 1) return Color.blue;
if (randNum == 2) return Color.green;
if (randNum == 3) return Color.yellow;
if (randNum == 4) return Color.orange;
return Color.red;
}
public void init()
{
resize(1400, 700);
resetBtn.addActionListener(this);
resetBtn.setBounds(525, 500, 250, 40);
this.add(resetBtn);
this.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
int x = (mouseX - 250) / 100;
int y = mouseY / 100;
togglePiece(y, x);
togglePiece(y - 1, x);
togglePiece(y + 1, x);
togglePiece(y, x + 1);
togglePiece(y, x - 1);
moveHistory = moveHistory = "(" + y + ", " + x + ")";
repaint();
}
public void paint(Graphics g)
{
for (int r = 1; r <= 5; r++)
for (int c = 1; c <= 5; c++)
{
if (lights[r][c] == false)
g.setColor(Color.black);
else
g.setColor(randColor());
g.fillRect(250 + 100 * c,100 * r, 90, 90);
}
}
public void actionPerformed(ActionEvent e) {
//if (e.getSource() == resetBtn)
//resetGame();
// repaint();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}