|
16 | 16 |
|
17 | 17 | import java.awt.*; |
18 | 18 | import java.awt.image.BufferedImage; |
| 19 | +import java.awt.event.ComponentEvent; |
| 20 | +import java.awt.event.ComponentListener; |
19 | 21 | import java.io.File; |
20 | 22 | import java.io.IOException; |
21 | 23 | import java.text.MessageFormat; |
22 | 24 | import javax.imageio.ImageIO; |
23 | 25 | import javax.swing.*; |
24 | 26 |
|
| 27 | + |
25 | 28 | /** |
26 | 29 | * A graphical component which is the array of hexagons. |
27 | 30 | * |
28 | 31 | * @author John Farrell |
29 | 32 | */ |
30 | | -public class FillerBoard extends JComponent { |
| 33 | +public class FillerBoard extends JComponent implements ComponentListener { |
31 | 34 | /** the pixel coordinates of the hexes */ |
32 | | - static Point[] topLefts, botRights; |
33 | | - private static final int SIZE = 7; |
| 35 | + Point[] topLefts, botRights; |
| 36 | + public final static int MIN_SIZE = 5; |
| 37 | + int count; |
| 38 | + int SIZE = MIN_SIZE; |
| 39 | + Dimension dim; |
34 | 40 |
|
35 | | - static { |
| 41 | + public void updateSize() { |
| 42 | + Dimension mySize = getSize(); |
| 43 | + int dX = FillerSettings.COLUMNS > 0 ? (mySize.width - 10) / FillerSettings.COLUMNS : MIN_SIZE; |
| 44 | + int dY = FillerSettings.ROWS > 0 ? (mySize.height - 10) / FillerSettings.ROWS / 4 : MIN_SIZE; |
| 45 | +// System.out.printf("width=%d height=%d cols=%d rows=%d dx=%d dy=%d\n", |
| 46 | +// mySize.width, mySize.height, FillerSettings.COLUMNS, FillerSettings.ROWS, dX, dY); |
| 47 | + SIZE = (dX < dY) ? dX : dY; |
| 48 | + if (SIZE < MIN_SIZE) { SIZE = MIN_SIZE; } |
36 | 49 | int size = FillerSettings.SIZE; |
37 | | - topLefts = new Point[size]; |
38 | | - botRights = new Point[size]; |
39 | | - for (int i=0; i<size; i++) { |
| 50 | + for (int i=0; i<count; i++) { |
40 | 51 | int x = FillerModel.getX(i); |
41 | 52 | int y = FillerModel.getY(i); |
42 | 53 | topLefts[i] = new Point(x * SIZE + SIZE,y * SIZE * 4 + (x % 2) * SIZE * 2 - SIZE); |
43 | 54 | botRights[i] = new Point(topLefts[i].x + SIZE, topLefts[i].y + SIZE + SIZE + (SIZE+1)/2); |
44 | 55 | } |
45 | 56 | } |
46 | 57 |
|
47 | | - static Point topLeft(int i) { return topLefts[i]; } |
| 58 | + public void componentHidden(ComponentEvent e) { /* System.out.printf("componentHidden\n"); */ } |
| 59 | + public void componentMoved(ComponentEvent e) { /* System.out.printf("componentMoved\n"); */ } |
| 60 | + public void componentShown(ComponentEvent e) { /* System.out.printf("componentShown\n"); */ } |
| 61 | + public void componentResized(ComponentEvent e) { |
| 62 | + /* System.out.printf("componentResized\n"); */ |
| 63 | + updateSize(); |
| 64 | + revalidate(); |
| 65 | + repaint(); |
| 66 | + } |
| 67 | + |
| 68 | + Point topLeft(int i) { return topLefts[i]; } |
48 | 69 |
|
49 | | - static Point bottomRight(int i) { return botRights[i]; } |
| 70 | + Point bottomRight(int i) { return botRights[i]; } |
50 | 71 |
|
51 | 72 | /** The off-screen image of the board. */ |
52 | 73 | protected BufferedImage off; |
53 | 74 | protected FillerModel model; |
54 | 75 |
|
55 | 76 | public FillerBoard() { |
56 | 77 | this(new FillerModel()); |
| 78 | + count = FillerSettings.SIZE; |
| 79 | + topLefts = new Point[count]; |
| 80 | + botRights = new Point[count]; |
| 81 | + dim = new Dimension(600, 400); |
| 82 | + this.addComponentListener(this); |
| 83 | + updateSize(); |
57 | 84 | } |
58 | 85 |
|
59 | 86 | public FillerBoard(FillerModel model) { |
@@ -272,17 +299,25 @@ protected void drawHex(Graphics g, Color c, int i) { |
272 | 299 | drawHexCentre(g,c,i); |
273 | 300 | } |
274 | 301 |
|
275 | | - public Dimension getMinimumSize() { return getPreferredSize(); } |
| 302 | + public Dimension getMinimumSize() { |
| 303 | +// Point p1 = bottomRight(FillerModel.makeIndex(FillerSettings.COLUMNS-1,FillerSettings.ROWS-1)); |
| 304 | +// Point p2 = bottomRight(FillerModel.makeIndex(FillerSettings.COLUMNS-2,FillerSettings.ROWS-1)); |
| 305 | +// Dimension min_dim = new Dimension((p1.x < p2.x) ? p2.x : p1.x, (p1.y < p2.y) ? p2.y : p1.y); |
| 306 | +// min_dim.width += MIN_SIZE; |
| 307 | +// min_dim.height += MIN_SIZE; |
| 308 | +// return min_dim; |
| 309 | + return dim; |
| 310 | + } |
276 | 311 |
|
277 | 312 | public Dimension getPreferredSize() { |
278 | | - Point p1 = bottomRight(FillerModel.makeIndex(FillerSettings.COLUMNS-1,FillerSettings.ROWS-1)); |
279 | | - Point p2 = bottomRight(FillerModel.makeIndex(FillerSettings.COLUMNS-2,FillerSettings.ROWS-1)); |
280 | | - Dimension dim = new Dimension((p1.x < p2.x) ? p2.x : p1.x, (p1.y < p2.y) ? p2.y : p1.y); |
281 | | - dim.width += SIZE; |
282 | | - dim.height += SIZE; |
283 | 313 | return dim; |
284 | 314 | } |
285 | 315 |
|
| 316 | + public void setBoardSize(Dimension newDim) { |
| 317 | + dim = newDim; |
| 318 | + setSize(dim); |
| 319 | + } |
| 320 | + |
286 | 321 | public void paintComponent(Graphics g) { |
287 | 322 | if (off == null) resetOffscreenImage(); |
288 | 323 | g.drawImage(off,0,0,this); |
|
0 commit comments