refactoring, deploy Position and XYGridIterator
- try to use more Position instead of (x,y) - create PositionWithDpeth for its specific usage in path finding - create a XYGridITerator that walk cells from grid X then Y.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package org.artisanlogiciel.games.maze.gui;
|
||||
|
||||
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
public abstract class CellGridComponent
|
||||
extends JComponent {
|
||||
|
||||
MazeCellRenderer cp;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,11 @@ implements StatusListener
|
||||
maze.resetCellRenderer(hexagon, frameSize);
|
||||
}
|
||||
|
||||
public void setBackground(boolean background) {
|
||||
addStatus(background ? "view background" : "hide background");
|
||||
maze.setViewBackground(background);
|
||||
}
|
||||
|
||||
private class MazeFrame extends JFrame
|
||||
{
|
||||
MazeFrame(LabyModel model, WidthHeightProvider frame, MazeParams params) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.artisanlogiciel.games.maze.gui;
|
||||
|
||||
import org.artisanlogiciel.games.maze.Brick;
|
||||
import org.artisanlogiciel.games.maze.Position;
|
||||
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
|
||||
|
||||
import java.awt.*;
|
||||
@@ -20,15 +21,15 @@ extends MazeCellRenderer
|
||||
super(model, frame, x, y);
|
||||
}
|
||||
|
||||
void drawLine(Graphics g, int refx, int refy, int x, int y, int a, int b) {
|
||||
void drawLine(Graphics g, Position ref, int x, int y, int a, int b) {
|
||||
g.drawLine(
|
||||
(int) ((refx + x) * width / SUBCELL), (int) ((refy + y) * height / SUBCELL),
|
||||
(int) ((refx + a) * width / SUBCELL),(int) ((refy + b) * height / SUBCELL));
|
||||
(int) ((ref.getX() + x) * width / SUBCELL), (int) ((ref.getY() + y) * height / SUBCELL),
|
||||
(int) ((ref.getX() + a) * width / SUBCELL),(int) ((ref.getY() + b) * height / SUBCELL));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawBackground(Graphics g, int pX, int pY, short walls) {
|
||||
UV uv= new UV(pX,pY);
|
||||
public void drawBackground(Graphics g, Position cell, short walls) {
|
||||
UV uv= new UV(cell);
|
||||
int x = offsetX + (int) (uv.getX() * width / SUBCELL);
|
||||
int y = offsetY + (int) (uv.getY() * height / SUBCELL);
|
||||
Color savecolor = g.getColor();
|
||||
@@ -39,47 +40,39 @@ extends MazeCellRenderer
|
||||
}
|
||||
|
||||
class UV
|
||||
extends Position
|
||||
{
|
||||
int u;
|
||||
int v;
|
||||
|
||||
UV(int u, int v)
|
||||
UV(Position p)
|
||||
{
|
||||
this.u=u;
|
||||
this.v=v;
|
||||
}
|
||||
|
||||
int getX()
|
||||
{
|
||||
return u*4 + 2*(v%2);
|
||||
}
|
||||
|
||||
int getY()
|
||||
{
|
||||
return 3*v;
|
||||
super(p);
|
||||
u=p.getX();
|
||||
v=p.getY();
|
||||
setX(u*4 + 2*(v%2));
|
||||
setY(3*v);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawWalls(Graphics g, int pX, int pY, short walls) {
|
||||
drawHalfNW(g, pX, pY, walls);
|
||||
public void drawWalls(Graphics g, Position cell, short walls) {
|
||||
drawHalfNW(g, cell, walls);
|
||||
}
|
||||
|
||||
void drawHalfNW(Graphics g, int refx, int refy, short walls)
|
||||
void drawHalfNW(Graphics g, Position cell, short walls)
|
||||
{
|
||||
UV uv= new UV(refx,refy);
|
||||
int x = uv.getX();
|
||||
int y = uv.getY();
|
||||
UV uv= new UV(cell);
|
||||
// NW : (0,1) - (2,0)
|
||||
if (Brick.isFlagSet( Brick.UP, walls))
|
||||
drawLine(g,x, y, 0,1,2,0);
|
||||
drawLine(g,uv, 0,1,2,0);
|
||||
// W : (0,1) - (0,3)
|
||||
if (Brick.isFlagSet( Brick.LEFT, walls))
|
||||
drawLine(g,x,y,0,1,0,3);
|
||||
drawLine(g,uv,0,1,0,3);
|
||||
// NE : (2,0) - (4,1)
|
||||
// if (Brick.isFlagSet( Brick.NE, walls))
|
||||
drawLine(g,x,y,2,0,4,1);
|
||||
drawLine(g,uv,2,0,4,1);
|
||||
}
|
||||
|
||||
void drawHalfSW(Graphics g)
|
||||
|
||||
@@ -82,25 +82,25 @@ public class MazeCellRenderer {
|
||||
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);
|
||||
public void drawBackground(Graphics g, Position cell, short walls) {
|
||||
int x = offsetX + (int) (cell.getX() * width);
|
||||
int y = offsetY + (int) (cell.getY() * 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);
|
||||
if ((pY == 0) && ( Brick.isFlagSet( Brick.UP, walls)))
|
||||
public void drawWalls(Graphics g, Position cell, short walls) {
|
||||
int x = offsetX + (int) (cell.getX() * width);
|
||||
int y = offsetY + (int) (cell.getY() * height);
|
||||
if ((cell.getY() == 0) && ( Brick.isFlagSet( Brick.UP, walls)))
|
||||
g.drawLine(x, y, x + (int) width, y);
|
||||
if (Brick.isFlagSet( Brick.DOWN, walls))
|
||||
g.drawLine(x, y + (int) height, x + (int) width, y + (int) height);
|
||||
if (Brick.isFlagSet( Brick.RIGHT, walls))
|
||||
g.drawLine(x + (int) width, y, x + (int) width, y + (int) height);
|
||||
if ((pX == 0) && (Brick.isFlagSet( Brick.LEFT, walls)))
|
||||
if ((cell.getX() == 0) && (Brick.isFlagSet( Brick.LEFT, walls)))
|
||||
g.drawLine(x, y, x, y + (int) height);
|
||||
}
|
||||
|
||||
@@ -145,23 +145,23 @@ public class MazeCellRenderer {
|
||||
}
|
||||
}
|
||||
|
||||
public void drawDot(Graphics g, Position dot, int pX, int pY, int mX, int mY) {
|
||||
public void drawDot(Graphics g, Position dot, Position min, Position max) {
|
||||
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)) {
|
||||
if ((dot.getX() >= min.getX()) && (dot.getY() >= min.getY()) && (dot.getX() < max.getX()) && (dot.getY() < max.getY())) {
|
||||
int x = offsetX + (int) (dot.getX() * width);
|
||||
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);
|
||||
// g.drawLine(x+a,y+height-a,x+width-a,y+a);
|
||||
|
||||
} else {
|
||||
int x = offsetX + (int) (pX * width);
|
||||
int y = offsetY + (int) (pY * h);
|
||||
}
|
||||
/* why do we drwa anything at all ?
|
||||
else {
|
||||
int x = offsetX + (int) (min.getX() * width);
|
||||
int y = offsetY + (int) (min.getY() * h);
|
||||
g.drawLine(x + 1, y + 1, x + (int) width - 1, y + (int) h - 1);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
19
java/org/artisanlogiciel/games/maze/gui/MazeColorMap.java
Normal file
19
java/org/artisanlogiciel/games/maze/gui/MazeColorMap.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package org.artisanlogiciel.games.maze.gui;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -19,16 +19,16 @@ import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class MazeComponent
|
||||
extends JComponent
|
||||
extends CellGridComponent
|
||||
implements MazeCreationListener,
|
||||
MazeResolutionListener,
|
||||
MouseMotionListener,
|
||||
Scrollable
|
||||
{
|
||||
private static final long serialVersionUID = 3163272907991176390L;
|
||||
private boolean viewBackground = true;
|
||||
|
||||
LabyModelProvider map;
|
||||
MazeCellRenderer cp;
|
||||
Position current = null;
|
||||
LinkedList<DirectionPosition> solvedPath = null;
|
||||
LinkedList<DirectionPosition> drawingPath = null;
|
||||
@@ -52,33 +52,8 @@ 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
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public void setXpm(Xpm xpm)
|
||||
{
|
||||
this.xpm = xpm;
|
||||
@@ -287,6 +262,15 @@ public class MazeComponent
|
||||
*/
|
||||
}
|
||||
|
||||
public void setViewBackground(boolean viewBackground)
|
||||
{
|
||||
this.viewBackground = viewBackground;
|
||||
}
|
||||
private boolean hasBackground()
|
||||
{
|
||||
return viewBackground && (xpm != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
@@ -300,101 +284,95 @@ public class MazeComponent
|
||||
// Shape s=g.getClip();
|
||||
Rectangle r = g.getClipBounds();
|
||||
|
||||
int mX = (int) ((double) r.getWidth() / cp.getWidth());
|
||||
int mY = (int) ((double) r.getHeight() / cp.getHeight());
|
||||
int pX = (int) ((double) (r.getX() - cp.getOffsetX()) / cp.getWidth());
|
||||
int pY = (int) ((double) (r.getY() - cp.getOffsetY()) / cp.getHeight());
|
||||
// visible part of grid :
|
||||
// (min)-------------
|
||||
// | |
|
||||
// _______________(max)
|
||||
|
||||
if (pX < 0)
|
||||
pX = 0;
|
||||
if (pY < 0)
|
||||
pY = 0;
|
||||
if (pX >= map.getWidth())
|
||||
return;
|
||||
if (pY >= map.getHeight())
|
||||
Position min = new Position(
|
||||
(int) ((double) (r.getX() - cp.getOffsetX()) / cp.getWidth()),
|
||||
(int) ((double) (r.getY() - cp.getOffsetY()) / cp.getHeight())
|
||||
);
|
||||
|
||||
min.limitToMin(0,0);
|
||||
|
||||
if ( (min.getX() >= map.getWidth()) || (min.getY() >= map.getHeight()) )
|
||||
return;
|
||||
|
||||
mX = mX + pX;
|
||||
mY = mY + pY;
|
||||
Position extent = new Position(
|
||||
(int) ((double) r.getWidth() / cp.getWidth()),
|
||||
(int) ((double) r.getHeight() / cp.getHeight())
|
||||
);
|
||||
Position max = min.translate(extent);
|
||||
|
||||
if (mX > map.getWidth()) {
|
||||
mX = map.getWidth();
|
||||
}
|
||||
if (mY > map.getHeight()) {
|
||||
mY = map.getHeight();
|
||||
}
|
||||
max.limitToMax(map.getWidth(), map.getHeight());
|
||||
|
||||
int mX = max.getX();
|
||||
int mY = max.getY();
|
||||
|
||||
int aX = pX;
|
||||
int aY = pY;
|
||||
|
||||
// draw background
|
||||
for (; pY < mY; pY++) {
|
||||
for (pX = 0; pX < mX; pX++) {
|
||||
walls = map.getWalls(pX, pY);
|
||||
if ( xpm != null ) {
|
||||
int cX = ( pX < xpm.getWidth()) ? pX : xpm.getWidth() - 1;
|
||||
int cY = ( pY < xpm.getHeight()) ? pY : xpm.getHeight() - 1;
|
||||
g.setColor(xpm.getColor(cX,cY));
|
||||
}
|
||||
else {
|
||||
g.setColor(colorMap.background);
|
||||
}
|
||||
cp.drawBackground(g, pX, pY, walls);
|
||||
XYGridIterator iterator = new XYGridIterator(min,max);
|
||||
Position cell = null;
|
||||
while ( ( cell = iterator.next()) != null )
|
||||
{
|
||||
walls = map.getWalls(cell);
|
||||
if ( hasBackground() ) {
|
||||
Position colored = new Position(cell);
|
||||
colored.limitToMax(xpm.getWidth() - 1, xpm.getHeight() -1 );
|
||||
g.setColor(xpm.getColor(colored.getX(), colored.getY()));
|
||||
}
|
||||
else {
|
||||
g.setColor(colorMap.background);
|
||||
}
|
||||
cp.drawBackground(g, cell, walls);
|
||||
}
|
||||
|
||||
if ((sX == gX) && (sY == gY)) {
|
||||
g.setColor(colorMap.goal);
|
||||
} else {
|
||||
g.setColor(colorMap.resolved_path);
|
||||
cp.drawDot(g, new Position(gX, gY), pX, pY, mX, mY);
|
||||
cp.drawDot(g, new Position(gX, gY), min, max);
|
||||
g.setColor(colorMap.path);
|
||||
}
|
||||
cp.drawDot(g, new Position(sX, sY), pX, pY, mX, mY);
|
||||
|
||||
pX = aX;
|
||||
pY = aY;
|
||||
cp.drawDot(g, new Position(sX, sY), min, max);
|
||||
|
||||
// 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);
|
||||
cp.drawWalls(g, pX, pY, walls);
|
||||
}
|
||||
iterator.reset();
|
||||
cell = null;
|
||||
while ( ( cell = iterator.next()) != null )
|
||||
{
|
||||
cp.drawWalls(g, cell, map.getWalls(cell));
|
||||
}
|
||||
|
||||
if (this.showAll) {
|
||||
statusListener.addStatus("*");
|
||||
pX = aX;
|
||||
pY = aY;
|
||||
|
||||
// draw all path within clip bounds horiz first then lines
|
||||
for (; pY < mY; pY++) {
|
||||
for (pX = 0; pX < mX; pX++) {
|
||||
path = map.getPath(pX, pY);
|
||||
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);
|
||||
}
|
||||
iterator.reset();
|
||||
cell = null;
|
||||
while ( ( cell = iterator.next()) != null )
|
||||
{
|
||||
path = map.getPath(cell.getX(), cell.getY());
|
||||
g.setColor(colorMap.path);
|
||||
if (path != 0) {
|
||||
DirectionPosition dp = new DirectionPosition(path, new Position(cell));
|
||||
cp.drawPath(g, dp, cell.getX(), cell.getY(), cell.getX() + 1, cell.getY() + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pX = aX;
|
||||
pY = aY;
|
||||
|
||||
synchronized (lockChange) {
|
||||
g.setColor(colorMap.goal);
|
||||
if (current != null) {
|
||||
cp.drawDot(g, current, pX, pY, mX, mY);
|
||||
cp.drawDot(g, current, min, max);
|
||||
}
|
||||
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);
|
||||
cp.drawDot(g, resolved.getPosition(), min, max);
|
||||
cp.drawPath(g, resolved, min.getX(), min.getY(), mX, mY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,6 +229,11 @@ public class MazeControler extends JPanel {
|
||||
return newActionButton("load", format, action);
|
||||
}
|
||||
|
||||
private JPanel createPaletteChooser()
|
||||
{
|
||||
return new JPanel(new FlowLayout());
|
||||
}
|
||||
|
||||
private JPanel createResolveQuitBar() {
|
||||
JPanel resolveQuitBar = new JPanel(new FlowLayout());
|
||||
|
||||
@@ -388,6 +393,7 @@ public class MazeControler extends JPanel {
|
||||
display.setAutoSize(autoSlide.isSelected());
|
||||
}
|
||||
});
|
||||
|
||||
final JCheckBox hexagon = new JCheckBox("hexagon");
|
||||
hexagon.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
@@ -395,11 +401,19 @@ public class MazeControler extends JPanel {
|
||||
}
|
||||
});
|
||||
|
||||
final JCheckBox background = new JCheckBox("background");
|
||||
background.addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
display.setBackground(background.isSelected());
|
||||
}
|
||||
});
|
||||
|
||||
JPanel resizecontrol = new JPanel(new FlowLayout());
|
||||
resizecontrol.add(showAll);
|
||||
resizecontrol.add(slider);
|
||||
resizecontrol.add(autoSlide);
|
||||
resizecontrol.add(hexagon);
|
||||
resizecontrol.add(background);
|
||||
|
||||
return resizecontrol;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user