Move (most) hardcoded defaults to MazeDefault

- harcoded values centralized
- IntegerField for JTextField containing numbers
- fix, can draw even if topleft position is not (0,0) ( ie if scrollbar were used )
This commit is contained in:
philippe lhardy
2020-12-15 22:26:57 +01:00
parent 14b6d9ff1d
commit b8cb7394cd
8 changed files with 274 additions and 110 deletions

View File

@@ -20,7 +20,9 @@ public class MazeComponent
extends JComponent
implements MazeCreationListener,
MazeResolutionListener,
MouseMotionListener {
MouseMotionListener,
Scrollable
{
private static final long serialVersionUID = 3163272907991176390L;
// WallsProvider map;
@@ -41,8 +43,8 @@ public class MazeComponent
Xpm xpm = null;
Date lastDrag = null;
// FIXME HARCDODED delay after which a draging to draw a continous line is ended, a new line will then start.
long dragTimeout = 200;
// delay after which a draging to draw a continuous line is ended, a new line will then start.
long dragTimeout = MazeDefault.dragTimeout;
// not set by default for debug, will show any path
boolean showAll = false;
@@ -72,17 +74,19 @@ public class MazeComponent
{
this.xpm = xpm;
}
// 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) {
int pX = (int) ((double) (x - cp.getOffsetX()) / cp.getWidth());
int pY = (int) ((double) (y - cp.getOffsetY()) / cp.getHeight());
return new Position(pX, pY);
// if rightmost position of this view not (0,0) this is not working.
// that's why MousListener should be attache to this MazeComponent and not to Scrollable one...
return cp.toMazeCoordinates(x,y);
}
@Override
public void mouseDragged(MouseEvent e) {
boolean add = ((e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0);
// where pixel -> maze coordinate is done
Position newPosition = getPosition(e.getX(), e.getY());
Date now = new Date(System.currentTimeMillis());
if (lastDrag == null) {
@@ -117,6 +121,8 @@ public class MazeComponent
}
}
// button 1 : add direction; button 2 : replace with direction.
// Position is Already in maze coordinates (ie not pixels)
public void addPosition(Position newPosition, boolean add) {
// should find the cell ...
DirectionPosition last = null;
@@ -134,7 +140,6 @@ public class MazeComponent
while (!last.getPosition().equals(newPosition)) {
path = LabyModel.getDirection(last.getPosition(), newPosition);
last.setDirection(path);
// button 1 : add direction; button 2 : rep lace with direction.
if (add) {
map.addDirection(last.getPosition().getX(), last.getPosition().getY(), path);
} else {
@@ -219,6 +224,7 @@ public class MazeComponent
gX = map.getWidth() - 1;
gY = map.getHeight() - 1;
this.statusListener = statusListener;
addMouseMotionListener(this);
}
// public void resetWallsProvider(WallsProvider map)
@@ -242,7 +248,8 @@ public class MazeComponent
public int getAutoSize() {
Rectangle r = getBounds();
cp.adaptTo((int) r.getWidth(), (int) r.getHeight());
// cp.resetMazeWidthHeight( (int)r.getWidth(), (int) r.getHeight());
cp.adaptTo(r.getWidth(), r.getHeight());
// should redraw ...
invalidate();
repaint();
@@ -412,4 +419,29 @@ public class MazeComponent
public void resetResolution() {
solvedPath = null;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return cp.getDimension();
}
@Override
public int getScrollableUnitIncrement(Rectangle rectangle, int i, int i1) {
return ( i == SwingConstants.VERTICAL ) ? (int) cp.getHeight() : (int) cp.getWidth();
}
@Override
public int getScrollableBlockIncrement(Rectangle rectangle, int i, int i1) {
return ( i == SwingConstants.VERTICAL ) ? (int) cp.getHeight() : (int) cp.getWidth();
}
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}