minor chnages / renaming / path painting

- draw resolved path as showAll correctly
it was displaying all direction of same cell as solution while only one direction is ok
- Renderer renaming for class rendering cells
- draw directions as arrows
This commit is contained in:
philippe lhardy
2020-12-28 13:16:35 +01:00
parent afbe26065b
commit 0bab1d51b1
7 changed files with 58 additions and 44 deletions

View File

@@ -538,7 +538,7 @@ lues, they are used as it is for
}
public final static boolean isFlagSet(short check, short flag) {
return ((check & flag) == flag);
return Brick.isFlagSet(flag,check);
}
public final short getCell(Position p) {

View File

@@ -103,7 +103,7 @@ implements StatusListener
private MazeComponent createMazeComponent(LabyModel model, int W, int H) {
MazeCellParameters cp = mHexagon ? new HexagonCell(model.getWidth(), model.getHeight(), W, H, 0, 0) : new MazeCellParameters(model.getWidth(), model.getHeight(), W, H, 0, 0);
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);
Xpm xpm = new Xpm();
try {

View File

@@ -9,12 +9,12 @@ import java.awt.*;
* using a square model ( LEFT UP only).
*/
public class HexagonCell
extends MazeCellParameters
public class HexagonCellRenderer
extends MazeCellRenderer
{
public final static int SUBCELL = 4;
public HexagonCell(int mapw, int maph, int W, int H, int x, int y) {
public HexagonCellRenderer(int mapw, int maph, int W, int H, int x, int y) {
super(mapw, maph, W, H, x, y);
}

View File

@@ -7,7 +7,7 @@ import org.artisanlogiciel.games.maze.solve.DirectionPosition;
import java.awt.*;
public class MazeCellParameters {
public class MazeCellRenderer {
int offsetX; // x offset of upper corner left in pixels
int offsetY; // y offset of upper corner left in pixers
@@ -18,7 +18,7 @@ public class MazeCellParameters {
double width; // width of one cell in pixels
double height; // height of one cell in pixels
public MazeCellParameters(int mapw, int maph, int W, int H, int x, int y) {
public MazeCellRenderer(int mapw, int maph, int W, int H, int x, int y) {
mapWidth = mapw;
mapHeight = maph;
offsetX = x;
@@ -106,6 +106,7 @@ public class MazeCellParameters {
public void drawPath(Graphics g, DirectionPosition dp, int pX, int pY, int mX, int mY) {
if (dp != null) {
boolean arrow = true;
Position dot = dp.getPosition();
if ((dot.getX() >= pX) && (dot.getY() >= pY) && (dot.getX() < mX) && (dot.getY() < mY)) {
int x = offsetX + (int) (dot.getX() * width);
@@ -113,25 +114,29 @@ public class MazeCellParameters {
short path = dp.getDirection();
int xm = x + (int) (width / 2);
int ym = y + (int) (height / 2);
if ((path & LabyModel.HORIZONTAL) == LabyModel.HORIZONTAL) {
if ((path & LabyModel.POSITIVE) == LabyModel.POSITIVE) {
g.drawLine(xm, ym, x + (int) width, ym);
if (LabyModel.isFlagSet(path,LabyModel.HORIZONTAL)) {
if (LabyModel.isFlagSet(path, LabyModel.POSITIVE)) {
g.drawLine(xm, ym - (int) (height / 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);
}
// LEFT /_
if ((path & LabyModel.NEGATIVE) == LabyModel.NEGATIVE) {
g.drawLine(x, ym, xm, ym);
if (LabyModel.isFlagSet(path,LabyModel.NEGATIVE)) {
g.drawLine(x, ym, xm, ym + (int) (height / 4));
if (arrow) g.drawLine(x, ym, xm, ym);
g.drawLine(x, ym, xm, ym - (int) (height / 4));
}
}
if ((path & LabyModel.VERTICAL) == LabyModel.VERTICAL) {
if ((path & LabyModel.POSITIVE) == LabyModel.POSITIVE) {
g.drawLine(xm, ym, xm, y + (int) height);
if (LabyModel.isFlagSet(path,LabyModel.VERTICAL)) {
if (LabyModel.isFlagSet(path ,LabyModel.POSITIVE)) {
g.drawLine(xm + (int) (width / 4), ym, xm, y + (int) height);
if (arrow) g.drawLine(xm, ym, xm, y + (int) height);
g.drawLine(xm - (int) (width / 4), ym, xm, y + (int) height);
}
// UP |\
if ((path & LabyModel.NEGATIVE) == LabyModel.NEGATIVE) {
g.drawLine(xm, ym, xm, y);
if (LabyModel.isFlagSet(path,LabyModel.NEGATIVE)) {
g.drawLine(xm - (int) (width / 4), ym, xm, y);
if (arrow) g.drawLine(xm, ym, xm, y);
g.drawLine(xm + (int) (width / 4), ym, xm, y);
}
}
@@ -145,8 +150,8 @@ public class MazeCellParameters {
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 r2 = (int) ((radius * 3) / 4);
g.drawOval(x + 1, y + 1, r2, r2);
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);
// g.drawLine(x+a,y+height-a,x+width-a,y+a);

View File

@@ -27,7 +27,7 @@ public class MazeComponent
private static final long serialVersionUID = 3163272907991176390L;
LabyModelProvider map;
final MazeCellParameters cp;
final MazeCellRenderer cp;
Position current = null;
LinkedList<DirectionPosition> solvedPath = null;
LinkedList<DirectionPosition> drawingPath = null;
@@ -216,7 +216,7 @@ public class MazeComponent
* offsetX, offsetY of upper left corner
**/
// public MazeComponent(WallsProvider map, MazeCellParameters cp)
public MazeComponent(LabyModel map, MazeCellParameters cp, StatusListener statusListener) {
public MazeComponent(LabyModel map, MazeCellRenderer cp, StatusListener statusListener) {
super();
this.cp = cp;
this.map = map;
@@ -350,22 +350,6 @@ public class MazeComponent
}
}
pX = aX;
pY = aY;
synchronized (lockChange) {
g.setColor(colorMap.goal);
if (current != null) {
cp.drawDot(g, current, pX, pY, mX, mY);
}
if (solvedPath != null) {
for (DirectionPosition resolved : solvedPath) {
// cp.drawDot(g, resolved.getPosition(), pX, pY, mX, mY);
cp.drawPath(g, resolved, pX, pY, mX, mY);
}
}
}
if (this.showAll) {
statusListener.addStatus("*");
pX = aX;
@@ -375,11 +359,7 @@ public class MazeComponent
for (; pY < mY; pY++) {
for (pX = 0; pX < mX; pX++) {
path = map.getPath(pX, pY);
if ((path & LabyModel.SOLVED) == LabyModel.SOLVED) {
g.setColor(colorMap.resolved_path);
} else {
g.setColor(colorMap.path);
}
g.setColor(colorMap.path);
if (path != 0) {
DirectionPosition dp = new DirectionPosition(path, new Position(pX, pY));
cp.drawPath(g, dp, pX, pY, pX + 1, pY + 1);
@@ -387,6 +367,24 @@ public class MazeComponent
}
}
}
pX = aX;
pY = aY;
synchronized (lockChange) {
g.setColor(colorMap.goal);
if (current != null) {
cp.drawDot(g, current, pX, pY, mX, mY);
}
g.setColor(colorMap.resolved_path);
if (solvedPath != null) {
for (DirectionPosition resolved : solvedPath) {
cp.drawDot(g, resolved.getPosition(), pX, pY, mX, mY);
cp.drawPath(g, resolved, pX, pY, mX, mY);
}
}
}
}
@@ -402,9 +400,15 @@ public class MazeComponent
}
public void notifySearchError(String error) {
System.err.println(error);
notifySearchInfoLevel(0,error);
}
public void notifySearchInfoLevel(int infoLevel, String pInfo)
{
statusListener.addStatus(pInfo);
}
public void notifyCompletion(LinkedList<DirectionPosition> solvedPath) {
LinkedList<DirectionPosition> newPath = new LinkedList<>(solvedPath);
statusListener.addStatus("resolution completed");

View File

@@ -10,8 +10,13 @@ public interface MazeResolutionListener
boolean notifySearch(DirectionPosition pPosition);
/* @deprecated
use notifySearchInfoLevel(0, String pInfo);
*/
void notifySearchError(String error);
void notifySearchInfoLevel(int infoLevel, String pInfo);
void notifyCompletion(LinkedList<DirectionPosition> solvedPath);
}

View File

@@ -19,7 +19,7 @@ extends LabyModel
}
/**
* resolve this labrynth using internal representation
* resolve this labyrinth using internal representation
* initial (x,y) is exit, will return list of positions from start (0,0) to end (x,y)
**/
public LinkedList<DirectionPosition> resolve(int x, int y, MazeResolutionListener rlistener) {