-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoDimArray.java
118 lines (107 loc) · 3.73 KB
/
TwoDimArray.java
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
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TwoDimArray extends JFrame {
private final int START_X = 20;
private final int START_Y = 40;
private final int ROWS = 6;
private final int COLS = 6;
private final int BOX_WIDTH = 20;
private final int BOX_HEIGHT = 20;
private MaskableBox boxes[][];
private Color boxColors[][];
private Button resetButton;
private JPanel panel1;
private JPanel panel2;
private Color picked_color;
public static void main(String[] args) {
TwoDimArray frame = new TwoDimArray();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,300);
frame.setVisible(true);
frame.setLayout(new BorderLayout());
}
public TwoDimArray(){
boxes = new MaskableBox[ROWS][COLS];
boxColors = new Color[ROWS][COLS];
resetButton = new Button("ResetColors");
resetButton.setBounds(200, 200, 120, 40);
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//randomizeColors();
buildBoxes();
repaint();
}
});
panel2 = new JPanel();
panel2.setLocation(100, 200);
resetButton.setLocation(100, 200);
panel2.add(resetButton);
this.add(panel2);
//separate building colors so we can add a button later
//to re-randomize them.
// randomizeColors();
buildBoxes();
}
public void paint(Graphics g) {
for(int row = 0; row < boxes.length; row ++) {
for(int col = 0; col < boxes[row].length; col++) {
if(boxes[row][col].isClicked()) {
picked_color = JColorChooser.showDialog(panel1, "Pick Color", picked_color);
boxes[row][col].setMaskColor(picked_color);
boxes[row][col].setMask(!boxes[row][col].isMask());
boxes[row][col].setClicked(false);
}
boxes[row][col].draw(g);
}
}
}
private void removeMouseListeners() {
for(int row = 0; row < boxes.length; row ++) {
for(int col = 0; col < boxes[row].length; col++) {
removeMouseListener(boxes[row][col]);
}
}
}
private void buildBoxes() {
removeMouseListeners();
for(int row = 0; row < boxes.length; row++) {
for(int col = 0; col < boxes[row].length; col++) {
boxes[row][col] =
new MaskableBox(START_X + col * BOX_WIDTH,
START_Y + row * BOX_HEIGHT,
BOX_WIDTH,
BOX_HEIGHT,
Color.gray,
boxColors[row][col],
true,
this);
addMouseListener(boxes[row][col]);
}
}
}
private void randomizeColors() {
int[] chosenColors = {0, 0, 0, 0, 0, 0, 0, 0};
Color[] availableColors = { Color.red, Color.blue, Color.green,
Color.yellow, Color.cyan, Color.magenta, Color.pink, Color.orange };
for(int row = 0; row < boxes.length; row++) {
for(int col = 0; col < boxes[row].length; col++) {
for(;;) {
int rnd = (int)(Math.random() * 8);
if(chosenColors[rnd] < 2) {
chosenColors[rnd]++;
boxColors[row][col] = availableColors[rnd];
break;
}
}
}
}
}
}