Square or Hexagon Cell

- allows to select between square and hexagon representation
 - remark : this is still a square model
This commit is contained in:
philippe lhardy
2020-12-28 15:09:54 +01:00
parent 0bab1d51b1
commit 0c886f0cd1
5 changed files with 89 additions and 43 deletions

View File

@@ -4,6 +4,7 @@ import org.artisanlogiciel.games.maze.LabyModel;
import org.artisanlogiciel.games.maze.Maze;
import org.artisanlogiciel.games.maze.MazeParams;
import org.artisanlogiciel.games.maze.MazeParamsFixed;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import org.artisanlogiciel.games.maze.persist.MazePersistRaw;
import org.artisanlogiciel.games.maze.solve.SolvingModel;
import org.artisanlogiciel.graphics.Drawing;
@@ -50,7 +51,7 @@ implements StatusListener
boolean statusEnable = true;
boolean mGrow = false;
boolean mHexagon = false;
JTextField statusField = null;
@@ -58,11 +59,19 @@ implements StatusListener
MazeFrame mazeFrame;
WidthHeightProvider frameSize = null;
public void setHexagon(boolean hexagon) {
addStatus(hexagon ? "hexagon" : "square");
maze.resetCellRenderer(hexagon, frameSize);
}
private class MazeFrame extends JFrame
{
MazeFrame(LabyModel model, int W, int H, MazeParams params) {
MazeFrame(LabyModel model, WidthHeightProvider frame, MazeParams params) {
super(MazeDefault.labels.getString("title"));
maze = createMazeComponent(model, W, H);
frameSize = frame;
maze = createMazeComponent(model, frame);
Container con = this.getContentPane();
JScrollPane scrollableMaze = new JScrollPane(maze);
@@ -85,26 +94,25 @@ implements StatusListener
model.setMazeListener(maze);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(W, H, W, H);
setBounds(frame.getWidth(), frame.getHeight(), frame.getWidth(), frame.getHeight());
setVisible(true);
}
}
public Display(LabyModel model, int W, int H, MazeParams params)
public Display(LabyModel model, WidthHeightProvider frame, MazeParams params)
{
super(model);
mazeFrame = new MazeFrame(model,W,H,params);
mazeFrame = new MazeFrame(model, frame,params);
if (params != null) {
// fixedParams = new MazeParamsFixed(params.getSaveDir(),params.getWidth(),params.getHeight(),params.getMaxDepth());
this.params = params;
}
}
private MazeComponent createMazeComponent(LabyModel model, int W, int H) {
private MazeComponent createMazeComponent(LabyModel model, WidthHeightProvider frame) {
MazeCellRenderer cp = mHexagon ? new HexagonCellRenderer(model.getWidth(), model.getHeight(), W, H, 0, 0) : new MazeCellRenderer(model.getWidth(), model.getHeight(), W, H, 0, 0);
MazeComponent comp = new MazeComponent(model, cp, this);
MazeComponent comp = new MazeComponent(model, frame , this);
Xpm xpm = new Xpm();
try {
xpm.parse(new FileInputStream(MazeDefault.getInstance().getXpmBackground()));
@@ -226,8 +234,8 @@ implements StatusListener
return button;
}
private static void setupDisplay(LabyModel model, int W, int H, MazeParams params) {
Display display = new Display(model, W, H, params);
private static void setupDisplay(LabyModel model, WidthHeightProvider frame, MazeParams params) {
Display display = new Display(model, frame, params);
}
@@ -251,8 +259,17 @@ implements StatusListener
public static void main(String pArgs[]) {
LabyModel model = null;
int W = 600;
int H = 400;
WidthHeightProvider frame = new WidthHeightProvider() {
@Override
public int getWidth() {
return 600;
}
@Override
public int getHeight() {
return 400;
}
};
System.out.println("Default Locale " + Locale.getDefault());
@@ -264,12 +281,12 @@ implements StatusListener
System.exit(1);
}
setupDisplay(model, W, H, null);
setupDisplay(model, frame, null);
} else {
MazeParamsFixed params = new MazeParamsFixed(MazeDefault.getInstance().getParams());
model = new LabyModel(params);
setupDisplay(model, W, H, params);
setupDisplay(model, frame, params);
/*
model.generateWithEntry(0, 0);

View File

@@ -1,6 +1,7 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.Brick;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import java.awt.*;
@@ -13,15 +14,16 @@ public class HexagonCellRenderer
extends MazeCellRenderer
{
public final static int SUBCELL = 4;
public final static int SUBCELLY = 3;
public HexagonCellRenderer(int mapw, int maph, int W, int H, int x, int y) {
super(mapw, maph, W, H, x, y);
public HexagonCellRenderer(WidthHeightProvider model, WidthHeightProvider frame, int x, int y) {
super(model, frame, x, y);
}
void drawLine(Graphics g, int refx, int refy, int x, int y, int a, int b) {
g.drawLine(
(int) ((refx + x) * getWidth() / SUBCELL), (int) ((refy + y) * getHeight() / SUBCELL),
(int) ((refx + a) * getWidth() / SUBCELL),(int) ((refy + b) * getHeight() / SUBCELL));
(int) ((refx + x) * width / SUBCELL), (int) ((refy + y) * height / SUBCELL),
(int) ((refx + a) * width / SUBCELL),(int) ((refy + b) * height / SUBCELL));
}
@Override
@@ -87,4 +89,8 @@ extends MazeCellRenderer
// E : (4,3) - (4,1)
}
@Override
public double getHeight() {
return height * SUBCELLY / SUBCELL;
}
}

View File

@@ -3,6 +3,7 @@ package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.Brick;
import org.artisanlogiciel.games.maze.LabyModel;
import org.artisanlogiciel.games.maze.Position;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import org.artisanlogiciel.games.maze.solve.DirectionPosition;
import java.awt.*;
@@ -18,17 +19,16 @@ public class MazeCellRenderer {
double width; // width of one cell in pixels
double height; // height of one cell in pixels
public MazeCellRenderer(int mapw, int maph, int W, int H, int x, int y) {
mapWidth = mapw;
mapHeight = maph;
public MazeCellRenderer(WidthHeightProvider model, WidthHeightProvider frame, int x, int y) {
resetMazeWidthHeight(model);
offsetX = x;
offsetY = y;
adaptTo(W,H);
adaptTo(frame.getWidth(), frame.getHeight());
}
public void resetMazeWidthHeight(int mapw, int maph) {
mapWidth = mapw;
mapHeight = maph;
public void resetMazeWidthHeight(WidthHeightProvider model) {
mapWidth = model.getWidth();
mapHeight = model.getHeight();
}
public void adaptTo(double W, double H) {
@@ -108,30 +108,31 @@ public class MazeCellRenderer {
if (dp != null) {
boolean arrow = true;
Position dot = dp.getPosition();
double h = getHeight();
if ((dot.getX() >= pX) && (dot.getY() >= pY) && (dot.getX() < mX) && (dot.getY() < mY)) {
int x = offsetX + (int) (dot.getX() * width);
int y = offsetY + (int) (dot.getY() * height);
int y = offsetY + (int) (dot.getY() * h);
short path = dp.getDirection();
int xm = x + (int) (width / 2);
int ym = y + (int) (height / 2);
int ym = y + (int) (h / 2);
if (LabyModel.isFlagSet(path,LabyModel.HORIZONTAL)) {
if (LabyModel.isFlagSet(path, LabyModel.POSITIVE)) {
g.drawLine(xm, ym - (int) (height / 4), x + (int) width, ym);
g.drawLine(xm, ym - (int) (h / 4), x + (int) width, ym);
if (arrow ) g.drawLine(xm, ym, x + (int) width, ym);
g.drawLine(xm, ym + (int) (height / 4), x + (int) width, ym);
g.drawLine(xm, ym + (int) (h / 4), x + (int) width, ym);
}
// LEFT /_
if (LabyModel.isFlagSet(path,LabyModel.NEGATIVE)) {
g.drawLine(x, ym, xm, ym + (int) (height / 4));
g.drawLine(x, ym, xm, ym + (int) (h / 4));
if (arrow) g.drawLine(x, ym, xm, ym);
g.drawLine(x, ym, xm, ym - (int) (height / 4));
g.drawLine(x, ym, xm, ym - (int) (h / 4));
}
}
if (LabyModel.isFlagSet(path,LabyModel.VERTICAL)) {
if (LabyModel.isFlagSet(path ,LabyModel.POSITIVE)) {
g.drawLine(xm + (int) (width / 4), ym, xm, y + (int) height);
g.drawLine(xm + (int) (width / 4), ym, xm, y + (int) h);
if (arrow) g.drawLine(xm, ym, xm, y + (int) height);
g.drawLine(xm - (int) (width / 4), ym, xm, y + (int) height);
g.drawLine(xm - (int) (width / 4), ym, xm, y + (int) h);
}
// UP |\
if (LabyModel.isFlagSet(path,LabyModel.NEGATIVE)) {
@@ -145,11 +146,12 @@ public class MazeCellRenderer {
}
public void drawDot(Graphics g, Position dot, int pX, int pY, int mX, int mY) {
double radius = (height > width) ? width : height;
double h = getHeight();
double radius = (h > width) ? width : h;
int a = (int) (radius / 4);
if ((dot.getX() >= pX) && (dot.getY() >= pY) && (dot.getX() < mX) && (dot.getY() < mY)) {
int x = offsetX + (int) (dot.getX() * width);
int y = offsetY + (int) (dot.getY() * height);
int y = offsetY + (int) (dot.getY() * h);
int r2 = (int) radius ; //(int) ((radius * 3) / 4);
g.drawOval(x, y, r2, r2);
// g.drawLine(x+a,y+a,x+width-a,y+height-a);
@@ -157,8 +159,8 @@ public class MazeCellRenderer {
} else {
int x = offsetX + (int) (pX * width);
int y = offsetY + (int) (pY * height);
g.drawLine(x + 1, y + 1, x + (int) width - 1, y + (int) height - 1);
int y = offsetY + (int) (pY * h);
g.drawLine(x + 1, y + 1, x + (int) width - 1, y + (int) h - 1);
}
}

View File

@@ -2,6 +2,7 @@ package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.*;
import org.artisanlogiciel.games.maze.model.LabyModelProvider;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import org.artisanlogiciel.games.maze.solve.DirectionPosition;
import org.artisanlogiciel.games.maze.solve.MazeResolutionListener;
import org.artisanlogiciel.graphics.Drawing;
@@ -27,7 +28,7 @@ public class MazeComponent
private static final long serialVersionUID = 3163272907991176390L;
LabyModelProvider map;
final MazeCellRenderer cp;
MazeCellRenderer cp;
Position current = null;
LinkedList<DirectionPosition> solvedPath = null;
LinkedList<DirectionPosition> drawingPath = null;
@@ -51,6 +52,14 @@ public class MazeComponent
StatusListener statusListener;
MazeCellRenderer createCellRenderer(boolean hexagon, WidthHeightProvider model, WidthHeightProvider frame)
{
MazeCellRenderer cellRenderer = hexagon ?
new HexagonCellRenderer(model, frame, 0, 0)
: new MazeCellRenderer(model, frame, 0, 0);
return cellRenderer;
}
MazeColorMap colorMap = new MazeColorMap(Color.white, Color.black, Color.blue, Color.green, Color.red);
public class MazeColorMap
@@ -216,17 +225,22 @@ public class MazeComponent
* offsetX, offsetY of upper left corner
**/
// public MazeComponent(WallsProvider map, MazeCellParameters cp)
public MazeComponent(LabyModel map, MazeCellRenderer cp, StatusListener statusListener) {
public MazeComponent(LabyModel map, WidthHeightProvider frame, StatusListener statusListener) {
super();
this.cp = cp;
this.map = map;
setPreferredSize(cp.getDimension());
gX = map.getWidth() - 1;
gY = map.getHeight() - 1;
resetCellRenderer(false,frame);
this.statusListener = statusListener;
addMouseMotionListener(this);
}
public void resetCellRenderer(boolean hexagon, WidthHeightProvider frame)
{
cp = createCellRenderer(hexagon,map,frame);
setPreferredSize(cp.getDimension());
}
// public void resetWallsProvider(WallsProvider map)
public void resetWallsProvider(LabyModel map) {
this.map = map;
@@ -235,7 +249,7 @@ public class MazeComponent
drawingPath = null;
sX = 0;
sY = 0;
cp.resetMazeWidthHeight(map.getWidth(), map.getHeight());
cp.resetMazeWidthHeight(map);
setPreferredSize(cp.getDimension());
}

View File

@@ -388,11 +388,18 @@ public class MazeControler extends JPanel {
display.setAutoSize(autoSlide.isSelected());
}
});
final JCheckBox hexagon = new JCheckBox("hexagon");
hexagon.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
display.setHexagon(hexagon.isSelected());
}
});
JPanel resizecontrol = new JPanel(new FlowLayout());
resizecontrol.add(showAll);
resizecontrol.add(slider);
resizecontrol.add(autoSlide);
resizecontrol.add(hexagon);
return resizecontrol;