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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user