From 0f1a15916c9712be9a48481efae757e18438f6fb Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Sun, 11 Oct 2020 15:30:28 +0200 Subject: [PATCH] move Display in gui package - and some code indentation --- build.xml | 2 +- .../games/DirectionPosition.java | 91 +++++------ java/org/artisanlogiciel/games/LabyModel.java | 8 +- .../games/MazeCreationListener.java | 2 +- .../games/{ => maze/gui}/Display.java | 4 +- .../games/maze/gui/MazeSettings.java | 1 - .../org/artisanlogiciel/games/stl/Wall3d.java | 141 +++++++----------- mybuild.xml | 2 +- 8 files changed, 93 insertions(+), 158 deletions(-) rename java/org/artisanlogiciel/games/{ => maze/gui}/Display.java (99%) diff --git a/build.xml b/build.xml index 30d02fe..da67b05 100644 --- a/build.xml +++ b/build.xml @@ -34,7 +34,7 @@ - + diff --git a/java/org/artisanlogiciel/games/DirectionPosition.java b/java/org/artisanlogiciel/games/DirectionPosition.java index 96567ac..f11f88a 100644 --- a/java/org/artisanlogiciel/games/DirectionPosition.java +++ b/java/org/artisanlogiciel/games/DirectionPosition.java @@ -1,77 +1,60 @@ package org.artisanlogiciel.games; /** - DirectionPosition - -record direction and position ( direction as defined in LabyModel ). + * DirectionPosition + *

+ * record direction and position ( direction as defined in LabyModel ). **/ -public class DirectionPosition -{ +public class DirectionPosition { private short direction; private Position position; // (direction,position) - DirectionPosition(short d, Position p) - { - direction=d; - position=p; + public DirectionPosition(short d, Position p) { + direction = d; + position = p; } - short getDirection() - { - return direction; + public short getDirection() { + return direction; } - void setDirection(short d) - { - direction = d; + public void setDirection(short d) { + direction = d; } - Position getPosition() - { - return position; + public Position getPosition() { + return position; } - public String toString() - { - if (position != null) - { - return position.toString(); - } - return "?"; + public String toString() { + if (position != null) { + return position.toString(); + } + return "?"; } // create a new DirectionPosition from this using one possible direction. - DirectionPosition moveToAdjacentDirection() - { - short pointingdirection = 0; - Position p = null; - if (LabyModel.isFlagSet(direction,LabyModel.RIGHT)) - { - p = new Position(position.getX() + 1, position.getY()); - pointingdirection |= LabyModel.LEFT; - } - else if (LabyModel.isFlagSet(direction,LabyModel.LEFT)) - { - p = new Position(position.getX() -1 ,position.getY()); - pointingdirection |= LabyModel.RIGHT; - } - else if (LabyModel.isFlagSet(direction,LabyModel.UP)) - { - p = new Position(position.getX(),position.getY() -1); - pointingdirection |= LabyModel.DOWN; - } - else if (LabyModel.isFlagSet(direction,LabyModel.DOWN)) - { - p = new Position(position.getX(),position.getY() + 1); - pointingdirection |= LabyModel.UP; - } - else - { - p = position; - } + public DirectionPosition moveToAdjacentDirection() { + short pointingdirection = 0; + Position p = null; + if (LabyModel.isFlagSet(direction, LabyModel.RIGHT)) { + p = new Position(position.getX() + 1, position.getY()); + pointingdirection |= LabyModel.LEFT; + } else if (LabyModel.isFlagSet(direction, LabyModel.LEFT)) { + p = new Position(position.getX() - 1, position.getY()); + pointingdirection |= LabyModel.RIGHT; + } else if (LabyModel.isFlagSet(direction, LabyModel.UP)) { + p = new Position(position.getX(), position.getY() - 1); + pointingdirection |= LabyModel.DOWN; + } else if (LabyModel.isFlagSet(direction, LabyModel.DOWN)) { + p = new Position(position.getX(), position.getY() + 1); + pointingdirection |= LabyModel.UP; + } else { + p = position; + } - return new DirectionPosition((short) pointingdirection, p); + return new DirectionPosition((short) pointingdirection, p); } } diff --git a/java/org/artisanlogiciel/games/LabyModel.java b/java/org/artisanlogiciel/games/LabyModel.java index 22ed466..56e22b9 100644 --- a/java/org/artisanlogiciel/games/LabyModel.java +++ b/java/org/artisanlogiciel/games/LabyModel.java @@ -1,16 +1,10 @@ package org.artisanlogiciel.games; +import java.io.*; import java.util.ArrayList; import java.util.LinkedList; -import java.util.Collections; import java.util.Random; -import java.io.OutputStream; -import java.io.InputStream; -import java.io.IOException; -import java.io.DataOutputStream; -import java.io.DataInputStream; - /** * Model of labyrinth storing only paths not walls. wall are regenerated later * on based on adjacent paths. each position (x,y) stores what move can be done diff --git a/java/org/artisanlogiciel/games/MazeCreationListener.java b/java/org/artisanlogiciel/games/MazeCreationListener.java index d425a70..633ecac 100644 --- a/java/org/artisanlogiciel/games/MazeCreationListener.java +++ b/java/org/artisanlogiciel/games/MazeCreationListener.java @@ -3,7 +3,7 @@ package org.artisanlogiciel.games; /** * MazeCreationListener **/ -interface MazeCreationListener +public interface MazeCreationListener { void changed(Position cornerleft, Position cornerright, WallsProvider provider); } diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/maze/gui/Display.java similarity index 99% rename from java/org/artisanlogiciel/games/Display.java rename to java/org/artisanlogiciel/games/maze/gui/Display.java index 733fc27..a25f413 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/maze/gui/Display.java @@ -1,4 +1,4 @@ -package org.artisanlogiciel.games; +package org.artisanlogiciel.games.maze.gui; import java.awt.BorderLayout; import java.awt.image.BufferedImage; @@ -31,7 +31,7 @@ import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import org.artisanlogiciel.games.maze.gui.MazeSettings; +import org.artisanlogiciel.games.*; import org.artisanlogiciel.games.stl.Wall3d; import org.artisanlogiciel.util.UTF8Control; diff --git a/java/org/artisanlogiciel/games/maze/gui/MazeSettings.java b/java/org/artisanlogiciel/games/maze/gui/MazeSettings.java index 71ca0c9..3feca25 100644 --- a/java/org/artisanlogiciel/games/maze/gui/MazeSettings.java +++ b/java/org/artisanlogiciel/games/maze/gui/MazeSettings.java @@ -1,6 +1,5 @@ package org.artisanlogiciel.games.maze.gui; -import org.artisanlogiciel.games.Display; import org.artisanlogiciel.games.MazeParams; import org.artisanlogiciel.games.MazeParamsFixed; diff --git a/java/org/artisanlogiciel/games/stl/Wall3d.java b/java/org/artisanlogiciel/games/stl/Wall3d.java index cb0a582..93837b1 100644 --- a/java/org/artisanlogiciel/games/stl/Wall3d.java +++ b/java/org/artisanlogiciel/games/stl/Wall3d.java @@ -12,18 +12,17 @@ import java.io.IOException; /** * Wall3d to create walls in 3d for stl conversion South, West North East... **/ -public class Wall3d -{ +public class Wall3d { // 4 triangles in 2 dim space reused 3 times - final static int BASE[][][] = { { { 0, 0 }, { 1, 0 }, { 0, 1 } }, { { 1, 0 }, { 1, 1 }, { 0, 1 } }, - { { 0, 0 }, { 1, 0 }, { 1, 1 } }, { { 0, 0 }, { 1, 1 }, { 0, 1 } } }; + final static int BASE[][][] = {{{0, 0}, {1, 0}, {0, 1}}, {{1, 0}, {1, 1}, {0, 1}}, + {{0, 0}, {1, 0}, {1, 1}}, {{0, 0}, {1, 1}, {0, 1}}}; final static short X = 1; final static short Y = 2; final static short Z = 4; // final static short AXIS[][]= {{X,Y},{-Z,Y},{X,Y},{Z,Y},{X,-Z},{X,-Z}}; - final static short AXIS[][] = { { X, Y, 0 }, { Z, Y, 0 }, { X, Y, 1 }, { Z, Y, 1 }, { X, Z, 0 }, { X, Z, 1 } }; + final static short AXIS[][] = {{X, Y, 0}, {Z, Y, 0}, {X, Y, 1}, {Z, Y, 1}, {X, Z, 0}, {X, Z, 1}}; public final static Wall3d South = new Wall3d(10, 1, 10, 0, 0, 0); public final static Wall3d West = new Wall3d(1, 10, 10, 0, 0, 0); @@ -34,51 +33,37 @@ public class Wall3d int triangle[][][] = null; - public Wall3d(int t[][][]) - { + public Wall3d(int t[][][]) { triangle = t; } - public Wall3d(Wall3d origin, int dx, int dy, int dz) - { + public Wall3d(Wall3d origin, int dx, int dy, int dz) { triangle = origin.translate(dx, dy, dz); } - Wall3d(int xl, int yl, int zl, int dx, int dy, int dz) - { + Wall3d(int xl, int yl, int zl, int dx, int dy, int dz) { int f = 0; triangle = new int[12][3][3]; - int[] factor = { xl, yl, zl }; - int[] translate = { dx, dy, dz }; - for (int i = 0; i < 12; i++) - { + int[] factor = {xl, yl, zl}; + int[] translate = {dx, dy, dz}; + for (int i = 0; i < 12; i++) { // point in a triangle - for (int p = 0; p < 3; p++) - { + for (int p = 0; p < 3; p++) { short uaxis = 0; - for (int axis = 0; axis < 2; axis++) - { + for (int axis = 0; axis < 2; axis++) { short caxis = AXIS[i / 2][axis]; - if (caxis > 0) - { + if (caxis > 0) { f = 1; - } - else if (caxis < 0) - { + } else if (caxis < 0) { f = -1; caxis = (short) -caxis; } uaxis |= caxis; - if (caxis == X) - { + if (caxis == X) { caxis = 0; - } - else if (caxis == Y) - { + } else if (caxis == Y) { caxis = 1; - } - else - { + } else { caxis = 2; } // if ( f == 0 ) @@ -89,16 +74,11 @@ public class Wall3d // " , " + BASE[i%4][p][axis] ); triangle[i][p][caxis] = translate[caxis] + BASE[i % 4][p][axis] * f * factor[caxis]; } - if ((uaxis & X) == 0) - { + if ((uaxis & X) == 0) { uaxis = 0; - } - else if ((uaxis & Y) == 0) - { + } else if ((uaxis & Y) == 0) { uaxis = 1; - } - else - { + } else { uaxis = 2; } triangle[i][p][uaxis] = translate[uaxis] + AXIS[i / 2][2] * factor[uaxis]; @@ -106,18 +86,14 @@ public class Wall3d } } - public int[][][] translate(int dx, int dy, int dz) - { - int[] translate = { dx, dy, dz }; + public int[][][] translate(int dx, int dy, int dz) { + int[] translate = {dx, dy, dz}; int t[][][] = new int[12][3][3]; - for (int i = 0; i < 12; i++) - { + for (int i = 0; i < 12; i++) { // point in a triangle - for (int p = 0; p < 3; p++) - { - for (int axis = 0; axis < 3; axis++) - { + for (int p = 0; p < 3; p++) { + for (int axis = 0; axis < 3; axis++) { t[i][p][axis] = translate[axis] + triangle[i][p][axis]; } } @@ -127,19 +103,15 @@ public class Wall3d } /** - write triangles as stl text + * write triangles as stl text **/ - public String toString() - { + public String toString() { String s = ""; - for (int t = 0; t < 12; t++) - { + for (int t = 0; t < 12; t++) { s += "facet normal 0 0 0\nouter loop\n"; - for (int p = 0; p < 3; p++) - { + for (int p = 0; p < 3; p++) { s += "vertex"; - for (int a = 0; a < 3; a++) - { + for (int a = 0; a < 3; a++) { // s+=" t "+ t + " p " + p + " a " + a + "=" + // triangle[t][p][a]; s += " " + triangle[t][p][a]; @@ -151,16 +123,14 @@ public class Wall3d return s; } - public static void prepare() - { + public static void prepare() { System.out.println(South.toString()); System.out.println(East.toString()); System.out.println(North.toString()); System.out.println(West.toString()); } - public static void streamWallsOut(String name, LabyModel provider, OutputStream stream) throws IOException - { + public static void streamWallsOut(String name, LabyModel provider, OutputStream stream) throws IOException { int width = provider.getWidth(); int height = provider.getHeight(); int xl = 10; @@ -170,58 +140,47 @@ public class Wall3d // WARNING DOWN - UP reversed ( in 2D Y is oriented to lower, in 3D it // is to upper ). stream.write(("solid " + name + "\n").getBytes()); - for (int x = 0; x < width; x++) - { + for (int x = 0; x < width; x++) { short walls = provider.getWalls(x, 0); - if ((walls & Brick.UP) != 0) - { + if ((walls & Brick.UP) != 0) { stream.write(new Wall3d(South, x * xl, 0, 0).toString().getBytes()); } - if ((walls & Brick.LEFT) != 0) - { + if ((walls & Brick.LEFT) != 0) { stream.write(new Wall3d(West, x * xl, 0, 0).toString().getBytes()); } } - for (int y = 0; y < height; y++) - { + for (int y = 0; y < height; y++) { short walls = provider.getWalls(0, y); - if ((walls & Brick.LEFT) != 0) - { + if ((walls & Brick.LEFT) != 0) { stream.write(new Wall3d(West, 0, y * yl, 0).toString().getBytes()); } - for (int x = 0; x < width; x++) - { + for (int x = 0; x < width; x++) { // south and east walls = provider.getWalls(x, y); - if ((walls & Brick.DOWN) != 0) - { + if ((walls & Brick.DOWN) != 0) { stream.write(new Wall3d(North, x * xl, y * yl, 0).toString().getBytes()); } - if ((walls & Brick.RIGHT) != 0) - { + if ((walls & Brick.RIGHT) != 0) { stream.write(new Wall3d(East, x * xl, y * yl, 0).toString().getBytes()); } - short path = provider.getPath(x,y); - if ( ( path & LabyModel.SOLVED ) == LabyModel.SOLVED ) - // if ( (walls & ( Brick.GOAL | Brick.ENTRY ) ) == 0 ) - { - // if ( ( (x+y) % 2) == 0 ) + short path = provider.getPath(x, y); + if ((path & LabyModel.SOLVED) == LabyModel.SOLVED) + // if ( (walls & ( Brick.GOAL | Brick.ENTRY ) ) == 0 ) + { + // if ( ( (x+y) % 2) == 0 ) - stream.write(new Wall3d(LowGround, x * xl, y * yl, 0).toString().getBytes()); - } - else - { - stream.write(new Wall3d(HighGround, x * xl, y * yl, 0).toString().getBytes()); - } + stream.write(new Wall3d(LowGround, x * xl, y * yl, 0).toString().getBytes()); + } else { + stream.write(new Wall3d(HighGround, x * xl, y * yl, 0).toString().getBytes()); + } } } stream.write("endsolid wall\n\n".getBytes()); } - public static void main(String args[]) - { + public static void main(String args[]) { Scanner console = new Scanner(System.in); int xl = console.nextInt(); diff --git a/mybuild.xml b/mybuild.xml index 11304cb..d05c76d 100644 --- a/mybuild.xml +++ b/mybuild.xml @@ -34,7 +34,7 @@ - +