Skip to content

build scripts and resizeable hexes rendering #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.class
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ fast enough for complex algorithms and complex games.

Please look at the INSTALL file for installation tips.
Please look at the HACKER file for ideas for developers.

Also under Linux may use ./rebuild.sh && ./run.sh to build&run game inplace
4 changes: 4 additions & 0 deletions rebuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
find -type f -name *.class -delete
javac -cp "$(pwd)/src:$(pwd)/res" src/friendless/games/filler/Filler.java
javac -cp "$(pwd)/src:$(pwd)/res" src/friendless/games/filler/player/*.java
2 changes: 2 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
java -cp "$(pwd)/src:$(pwd)/res" friendless/games/filler/Filler
65 changes: 50 additions & 15 deletions src/friendless/games/filler/FillerBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,71 @@

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import javax.imageio.ImageIO;
import javax.swing.*;


/**
* A graphical component which is the array of hexagons.
*
* @author John Farrell
*/
public class FillerBoard extends JComponent {
public class FillerBoard extends JComponent implements ComponentListener {
/** the pixel coordinates of the hexes */
static Point[] topLefts, botRights;
private static final int SIZE = 7;
Point[] topLefts, botRights;
public final static int MIN_SIZE = 5;
int count;
int SIZE = MIN_SIZE;
Dimension dim;

static {
public void updateSize() {
Dimension mySize = getSize();
int dX = FillerSettings.COLUMNS > 0 ? mySize.width / (FillerSettings.COLUMNS + 1) : MIN_SIZE;
int dY = FillerSettings.ROWS > 0 ? mySize.height / FillerSettings.ROWS / 4 : MIN_SIZE;
// System.out.printf("width=%d height=%d cols=%d rows=%d dx=%d dy=%d\n",
// mySize.width, mySize.height, FillerSettings.COLUMNS, FillerSettings.ROWS, dX, dY);
SIZE = (dX < dY) ? dX : dY;
if (SIZE < MIN_SIZE) { SIZE = MIN_SIZE; }
int size = FillerSettings.SIZE;
topLefts = new Point[size];
botRights = new Point[size];
for (int i=0; i<size; i++) {
for (int i=0; i<count; i++) {
int x = FillerModel.getX(i);
int y = FillerModel.getY(i);
topLefts[i] = new Point(x * SIZE + SIZE,y * SIZE * 4 + (x % 2) * SIZE * 2 - SIZE);
botRights[i] = new Point(topLefts[i].x + SIZE, topLefts[i].y + SIZE + SIZE + (SIZE+1)/2);
}
}

static Point topLeft(int i) { return topLefts[i]; }
public void componentHidden(ComponentEvent e) { /* System.out.printf("componentHidden\n"); */ }
public void componentMoved(ComponentEvent e) { /* System.out.printf("componentMoved\n"); */ }
public void componentShown(ComponentEvent e) { /* System.out.printf("componentShown\n"); */ }
public void componentResized(ComponentEvent e) {
/* System.out.printf("componentResized\n"); */
updateSize();
revalidate();
repaint();
}

Point topLeft(int i) { return topLefts[i]; }

static Point bottomRight(int i) { return botRights[i]; }
Point bottomRight(int i) { return botRights[i]; }

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

public FillerBoard() {
this(new FillerModel());
count = FillerSettings.SIZE;
topLefts = new Point[count];
botRights = new Point[count];
dim = new Dimension(600, 400);
this.addComponentListener(this);
updateSize();
}

public FillerBoard(FillerModel model) {
Expand Down Expand Up @@ -272,17 +299,25 @@ protected void drawHex(Graphics g, Color c, int i) {
drawHexCentre(g,c,i);
}

public Dimension getMinimumSize() { return getPreferredSize(); }
public Dimension getMinimumSize() {
// Point p1 = bottomRight(FillerModel.makeIndex(FillerSettings.COLUMNS-1,FillerSettings.ROWS-1));
// Point p2 = bottomRight(FillerModel.makeIndex(FillerSettings.COLUMNS-2,FillerSettings.ROWS-1));
// Dimension min_dim = new Dimension((p1.x < p2.x) ? p2.x : p1.x, (p1.y < p2.y) ? p2.y : p1.y);
// min_dim.width += MIN_SIZE;
// min_dim.height += MIN_SIZE;
// return min_dim;
return dim;
}

public Dimension getPreferredSize() {
Point p1 = bottomRight(FillerModel.makeIndex(FillerSettings.COLUMNS-1,FillerSettings.ROWS-1));
Point p2 = bottomRight(FillerModel.makeIndex(FillerSettings.COLUMNS-2,FillerSettings.ROWS-1));
Dimension dim = new Dimension((p1.x < p2.x) ? p2.x : p1.x, (p1.y < p2.y) ? p2.y : p1.y);
dim.width += SIZE;
dim.height += SIZE;
return dim;
}

public void setBoardSize(Dimension newDim) {
dim = newDim;
setSize(dim);
}

public void paintComponent(Graphics g) {
if (off == null) resetOffscreenImage();
g.drawImage(off,0,0,this);
Expand Down
31 changes: 26 additions & 5 deletions src/friendless/games/filler/FillerPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import friendless.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentAdapter;
// for remote player
import friendless.games.filler.remote.RemoteConnection;
import friendless.games.filler.remote.messages.MoveMessage;
Expand Down Expand Up @@ -138,12 +141,30 @@ public FillerPanel(PlayerWrappers players, ResourceBundle resources) {
topPanel.add("x", p = new JPanel());
topPanel.add("", buttonPanels[1]);
// complete panel
JPanel p2 = new JPanel(new HCodeLayout("", 0));
p2.add("x", new JPanel());
p2.add(board = new FillerBoard(), BorderLayout.CENTER);
p2.add("x", new JPanel());
add("", p2);
// JPanel p2 = new JPanel(new HCodeLayout("", 0));
// p2.add("x", new JPanel());
// p2.add("x", board = new FillerBoard()); //, BorderLayout.CENTER
// p2.add("x", new JPanel());
// add("", p2);
board = new FillerBoard();
add("", board);
showButtons();

FillerPanel me = this;
me.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
//System.out.printf("was: x=%d y=%d w=%d h=%d\n",
// board.getLocation().x, board.getLocation().y, board.getSize().width, board.getSize().height);
Dimension xSize = me.getSize();
Point p2Location = board.getLocation();
xSize.height-= p2Location.y;
board.setBoardSize(xSize);
//System.out.printf("set: width=%d height=%d\n", xSize.width, xSize.height);
}
});


}

public void showMessage(String s1, String s2) {
Expand Down