Skip to content

Commit 9ea5541

Browse files
author
strangeCamel
committed
make hexes sizes to follow board resizing
1 parent d443e03 commit 9ea5541

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

src/friendless/games/filler/FillerBoard.java

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,71 @@
1616

1717
import java.awt.*;
1818
import java.awt.image.BufferedImage;
19+
import java.awt.event.ComponentEvent;
20+
import java.awt.event.ComponentListener;
1921
import java.io.File;
2022
import java.io.IOException;
2123
import java.text.MessageFormat;
2224
import javax.imageio.ImageIO;
2325
import javax.swing.*;
2426

27+
2528
/**
2629
* A graphical component which is the array of hexagons.
2730
*
2831
* @author John Farrell
2932
*/
30-
public class FillerBoard extends JComponent {
33+
public class FillerBoard extends JComponent implements ComponentListener {
3134
/** 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;
3440

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; }
3649
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++) {
4051
int x = FillerModel.getX(i);
4152
int y = FillerModel.getY(i);
4253
topLefts[i] = new Point(x * SIZE + SIZE,y * SIZE * 4 + (x % 2) * SIZE * 2 - SIZE);
4354
botRights[i] = new Point(topLefts[i].x + SIZE, topLefts[i].y + SIZE + SIZE + (SIZE+1)/2);
4455
}
4556
}
4657

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]; }
4869

49-
static Point bottomRight(int i) { return botRights[i]; }
70+
Point bottomRight(int i) { return botRights[i]; }
5071

5172
/** The off-screen image of the board. */
5273
protected BufferedImage off;
5374
protected FillerModel model;
5475

5576
public FillerBoard() {
5677
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();
5784
}
5885

5986
public FillerBoard(FillerModel model) {
@@ -272,17 +299,25 @@ protected void drawHex(Graphics g, Color c, int i) {
272299
drawHexCentre(g,c,i);
273300
}
274301

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+
}
276311

277312
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;
283313
return dim;
284314
}
285315

316+
public void setBoardSize(Dimension newDim) {
317+
dim = newDim;
318+
setSize(dim);
319+
}
320+
286321
public void paintComponent(Graphics g) {
287322
if (off == null) resetOffscreenImage();
288323
g.drawImage(off,0,0,this);

src/friendless/games/filler/FillerPanel.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import friendless.awt.*;
2323
import javax.swing.*;
2424
import javax.swing.border.*;
25+
import java.awt.event.ComponentEvent;
26+
import java.awt.event.ComponentListener;
27+
import java.awt.event.ComponentAdapter;
2528
// for remote player
2629
import friendless.games.filler.remote.RemoteConnection;
2730
import friendless.games.filler.remote.messages.MoveMessage;
@@ -138,12 +141,30 @@ public FillerPanel(PlayerWrappers players, ResourceBundle resources) {
138141
topPanel.add("x", p = new JPanel());
139142
topPanel.add("", buttonPanels[1]);
140143
// complete panel
141-
JPanel p2 = new JPanel(new HCodeLayout("", 0));
142-
p2.add("x", new JPanel());
143-
p2.add(board = new FillerBoard(), BorderLayout.CENTER);
144-
p2.add("x", new JPanel());
145-
add("", p2);
144+
// JPanel p2 = new JPanel(new HCodeLayout("", 0));
145+
// p2.add("x", new JPanel());
146+
// p2.add("x", board = new FillerBoard()); //, BorderLayout.CENTER
147+
// p2.add("x", new JPanel());
148+
// add("", p2);
149+
board = new FillerBoard();
150+
add("", board);
146151
showButtons();
152+
153+
FillerPanel me = this;
154+
me.addComponentListener(new ComponentAdapter() {
155+
@Override
156+
public void componentResized(ComponentEvent e) {
157+
//System.out.printf("was: x=%d y=%d w=%d h=%d\n",
158+
// board.getLocation().x, board.getLocation().y, board.getSize().width, board.getSize().height);
159+
Dimension xSize = me.getSize();
160+
Point p2Location = board.getLocation();
161+
xSize.height-= p2Location.y;
162+
board.setBoardSize(xSize);
163+
//System.out.printf("set: width=%d height=%d\n", xSize.width, xSize.height);
164+
}
165+
});
166+
167+
147168
}
148169

149170
public void showMessage(String s1, String s2) {

0 commit comments

Comments
 (0)