From 56585712c856fcad0d6b72a143169314461869b8 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Sun, 13 Dec 2020 18:05:02 +0100 Subject: [PATCH] prepare for some colors - greyed background - background is not greyed depending on surrouding walls - MazeColorMap to prepare for color edition --- .../games/maze/gui/MazeCellParameters.java | 17 +++++++ .../games/maze/gui/MazeComponent.java | 45 ++++++++++++++++--- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/java/org/artisanlogiciel/games/maze/gui/MazeCellParameters.java b/java/org/artisanlogiciel/games/maze/gui/MazeCellParameters.java index 83cb209..53f7151 100644 --- a/java/org/artisanlogiciel/games/maze/gui/MazeCellParameters.java +++ b/java/org/artisanlogiciel/games/maze/gui/MazeCellParameters.java @@ -73,6 +73,23 @@ public class MazeCellParameters { offsetY + (int) (mapHeight * height)); } + public Color getGreyedColor(Color color, int greylevel) + { + Color greyedColor = new Color( + Math.max(0,color.getRed() - greylevel), + Math.max(0,color.getGreen() - greylevel), + Math.max(0,color.getBlue() - greylevel)); + return greyedColor; + } + public void drawBackground(Graphics g, int pX, int pY, short walls) { + int x = offsetX + (int) (pX * width); + int y = offsetY + (int) (pY * height); + Color savecolor = g.getColor(); + int greylevel = walls << 2; + g.setColor(getGreyedColor(savecolor,greylevel)); + //g.fillRect(x+1,y+1,((int)width) -1,((int)height) - 1); + g.fillRect(x,y,((int)width),((int)height)); + } public void drawWalls(Graphics g, int pX, int pY, short walls) { int x = offsetX + (int) (pX * width); int y = offsetY + (int) (pY * height); diff --git a/java/org/artisanlogiciel/games/maze/gui/MazeComponent.java b/java/org/artisanlogiciel/games/maze/gui/MazeComponent.java index 62b85ac..15df3b6 100644 --- a/java/org/artisanlogiciel/games/maze/gui/MazeComponent.java +++ b/java/org/artisanlogiciel/games/maze/gui/MazeComponent.java @@ -45,6 +45,24 @@ public class MazeComponent StatusListener statusListener; + MazeColorMap colorMap = new MazeColorMap(Color.white, Color.black, Color.blue, Color.green, Color.red); + + public class MazeColorMap + { + public Color background; + public Color wall; + public Color path; + public Color resolved_path; + public Color goal; + + public MazeColorMap(Color background, Color wall, Color path, Color resolved_path, Color goal) { + this.background = background; + this.wall = wall; + this.path = path; + this.resolved_path = resolved_path; + this.goal = goal; + } + } // for a given (x,y) pixel return cell position. // if rightmost position of view not (0,0) this is not working. Position getPosition(int x, int y) { @@ -277,16 +295,16 @@ public class MazeComponent } if ((sX == gX) && (sY == gY)) { - g.setColor(Color.red); + g.setColor(colorMap.goal); } else { - g.setColor(Color.green); + g.setColor(colorMap.resolved_path); cp.drawDot(g, new Position(gX, gY), pX, pY, mX, mY); - g.setColor(Color.blue); + g.setColor(colorMap.path); } cp.drawDot(g, new Position(sX, sY), pX, pY, mX, mY); synchronized (lockChange) { - g.setColor(Color.red); + g.setColor(colorMap.goal); if (current != null) { cp.drawDot(g, current, pX, pY, mX, mY); } @@ -301,9 +319,22 @@ public class MazeComponent int aX = pX; int aY = pY; - g.setColor(Color.black); + + + // draw background + for (; pY < mY; pY++) { + for (pX = 0; pX < mX; pX++) { + walls = map.getWalls(pX, pY); + g.setColor(colorMap.background); + cp.drawBackground(g, pX, pY, walls); + } + } + + pX = aX; + pY = aY; // draw all walls within clip bounds horiz first then lines + g.setColor(colorMap.wall); for (; pY < mY; pY++) { for (pX = 0; pX < mX; pX++) { walls = map.getWalls(pX, pY); @@ -321,9 +352,9 @@ public class MazeComponent for (pX = 0; pX < mX; pX++) { path = map.getPath(pX, pY); if ((path & LabyModel.SOLVED) == LabyModel.SOLVED) { - g.setColor(Color.green); + g.setColor(colorMap.resolved_path); } else { - g.setColor(Color.blue); + g.setColor(colorMap.path); } if (path != 0) { DirectionPosition dp = new DirectionPosition(path, new Position(pX, pY));