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

@@ -8,26 +8,22 @@ import org.artisanlogiciel.games.maze.solve.DirectionPosition;
import java.awt.*;
public class MazeCellParameters {
double width = 10; // width of one cell
double height = 10; // height of one cell
int offsetX = 5; // x offset of upper corner left
int offsetY = 5; // y offset of upper corner left
int offsetX; // x offset of upper corner left in pixels
int offsetY; // y offset of upper corner left in pixers
int mapWidth = 0;
int mapHeight = 0;
int mapWidth;
int mapHeight;
// computed see adaptTo
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) {
double w = (W - x) / mapw;
double h = (H - y) / maph;
mapWidth = mapw;
mapHeight = maph;
if (w < 5)
w = 5;
if (h < 5)
h = 5;
setCellSize(w, h);
offsetX = x;
offsetY = y;
adaptTo(W,H);
}
public void resetMazeWidthHeight(int mapw, int maph) {
@@ -38,8 +34,6 @@ public class MazeCellParameters {
public void adaptTo(double W, double H) {
double w = (W - offsetX) / mapWidth;
double h = (H - offsetY) / mapHeight;
mapWidth = mapWidth;
mapHeight = mapHeight;
if (w < 5)
w = 5;
if (h < 5)
@@ -47,6 +41,13 @@ public class MazeCellParameters {
setCellSize(w, h);
}
// for a given (x,y) pixel return cell position.
Position toMazeCoordinates(int x, int y) {
int pX = (int) ((double) (x - getOffsetX()) / getWidth());
int pY = (int) ((double) (y - getOffsetY()) / getHeight());
return new Position(pX, pY);
}
public void setCellSize(double w, double h) {
width = w;
height = h;