-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
320 lines (279 loc) · 7.87 KB
/
Grid.java
File metadata and controls
320 lines (279 loc) · 7.87 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
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
public class Grid extends JComponent implements KeyListener, MouseListener
{
private Cell[][] cells;
private JFrame frame;
private int lastKeyPressed;
private Location lastLocationClicked;
private Color lineColor;
public Grid(int numRows, int numCols)
{
init(numRows, numCols);
}
public Grid(String imageFileName)
{
BufferedImage image = loadImage(imageFileName);
init(image.getHeight(), image.getWidth());
showImage(image);
setTitle(imageFileName);
}
private BufferedImage loadImage(String imageFileName)
{
URL url = getClass().getResource(imageFileName);
if (url == null)
throw new RuntimeException("cannot find file: " + imageFileName);
try
{
return ImageIO.read(url);
}
catch(IOException e)
{
throw new RuntimeException("unable to read from file: " + imageFileName);
}
}
public int getNumRows()
{
return cells.length;
}
public int getNumCols()
{
return cells[0].length;
}
private void init(int numRows, int numCols)
{
lastKeyPressed = -1;
lastLocationClicked = null;
lineColor = null;
cells = new Cell[numRows][numCols];
for (int row = 0; row < numRows; row++)
{
for (int col = 0; col < numCols; col++)
cells[row][col] = new Cell();
}
frame = new JFrame("Grid");
//frame.setContentPane(new JLabel(new ImageIcon("bg.jpeg")));
//frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addKeyListener(this);
int cellSize = Math.max(Math.min(500 / getNumRows(), 500 / getNumCols()), 1);
setPreferredSize(new Dimension(cellSize * numCols, cellSize * numRows));
addMouseListener(this);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
private void showImage(BufferedImage image)
{
for (int row = 0; row < getNumRows(); row++)
{
for (int col = 0; col < getNumCols(); col++)
{
int x = col * image.getWidth() / getNumCols();
int y = row * image.getHeight() / getNumRows();
int c = image.getRGB(x, y);
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
cells[row][col].setColor(new Color(red, green, blue));
}
}
repaint();
}
private int getCellSize()
{
int cellWidth = getWidth() / getNumCols();
int cellHeight = getHeight() / getNumRows();
return Math.min(cellWidth, cellHeight);
}
public void keyPressed(KeyEvent e)
{
lastKeyPressed = e.getKeyCode();
}
public void keyReleased(KeyEvent e)
{
//ignored
}
public void keyTyped(KeyEvent e)
{
//ignored
}
public void mousePressed(MouseEvent e)
{
int cellSize = getCellSize();
int row = e.getY() / cellSize;
if (row < 0 || row >= getNumRows())
return;
int col = e.getX() / cellSize;
if (col < 0 || col >= getNumCols())
return;
lastLocationClicked = new Location(row, col);
}
public void mouseReleased(MouseEvent e)
{
//ignore
}
public void mouseClicked(MouseEvent e)
{
//ignore
}
public void mouseEntered(MouseEvent e)
{
//ignore
}
public void mouseExited(MouseEvent e)
{
//ignore
}
private static java.awt.Color toJavaColor(Color color)
{
return new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue());
}
public void paintComponent(Graphics g)
{
for (int row = 0; row < getNumRows(); row++)
{
for (int col = 0; col < getNumCols(); col++)
{
Location loc = new Location(row, col);
Cell cell = cells[loc.getRow()][loc.getCol()];
Color color = cell.getColor();
g.setColor(toJavaColor(color));
int cellSize = getCellSize();
int x = col * cellSize;
int y = row * cellSize;
g.fillRect(x, y, cellSize, cellSize);
String imageFileName = cell.getImageFileName();
if (imageFileName != null)
{
URL url = getClass().getResource(imageFileName);
if (url == null)
System.out.println("File not found: " + imageFileName);
else
{
Image image = new ImageIcon(url).getImage();
int width = image.getWidth(null);
int height = image.getHeight(null);
int max;
if (width > height)
{
int drawHeight = cellSize * height / width;
g.drawImage(image, x, y + (cellSize - drawHeight) / 2, cellSize, drawHeight, null);
}
else
{
int drawWidth = cellSize * width / height;
g.drawImage(image, x + (cellSize - drawWidth) / 2, y, drawWidth, cellSize, null);
}
}
}
if (lineColor != null)
{
g.setColor(toJavaColor(lineColor));
g.drawRect(x, y, cellSize, cellSize);
}
}
}
}
public void setTitle(String title)
{
frame.setTitle(title);
}
public boolean isValid(Location loc)
{
int row = loc.getRow();
int col = loc.getCol();
return 0 <= row && row < getNumRows() && 0 <= col && col < getNumCols();
}
public void setColor(Location loc, Color color)
{
if (!isValid(loc))
throw new RuntimeException("cannot set color of invalid location " + loc + " to color " + color);
cells[loc.getRow()][loc.getCol()].setColor(color);
repaint();
}
public Color getColor(Location loc)
{
if (!isValid(loc))
throw new RuntimeException("cannot get color from invalid location " + loc);
return cells[loc.getRow()][loc.getCol()].getColor();
}
public void setImage(Location loc, String imageFileName)
{
if (!isValid(loc))
throw new RuntimeException("cannot set image for invalid location " + loc + " to \"" + imageFileName + "\"");
cells[loc.getRow()][loc.getCol()].setImageFileName(imageFileName);
repaint();
}
public String getImage(Location loc)
{
if (!isValid(loc))
throw new RuntimeException("cannot get image for invalid location " + loc);
return cells[loc.getRow()][loc.getCol()].getImageFileName();
}
public static void pause(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch(Exception e)
{
//ignore
}
}
//returns -1 if no key pressed since last call.
//otherwise returns the code for the last key pressed.
public int checkLastKeyPressed()
{
int key = lastKeyPressed;
lastKeyPressed = -1;
return key;
}
//returns null if no location clicked since last call.
public Location checkLastLocationClicked()
{
Location loc = lastLocationClicked;
lastLocationClicked = null;
return loc;
}
public void load(String imageFileName)
{
showImage(loadImage(imageFileName));
setTitle(imageFileName);
}
public void save(String imageFileName)
{
try
{
BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
paintComponent(bi.getGraphics());
int index = imageFileName.lastIndexOf('.');
if (index == -1)
throw new RuntimeException("invalid image file name: " + imageFileName);
ImageIO.write(bi, imageFileName.substring(index + 1), new File(imageFileName));
}
catch(IOException e)
{
throw new RuntimeException("unable to save image to file: " + imageFileName);
}
}
public void setLineColor(Color color)
{
lineColor = color;
repaint();
}
public void showMessageDialog(String message)
{
JOptionPane.showMessageDialog(this, message);
}
public String showInputDialog(String message)
{
return JOptionPane.showInputDialog(this, message);
}
}