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

@@ -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);
}
}