diff --git a/build.gradle b/build.gradle index 20534b2a198109bf2e4f0efc3434f50b39d21bc8..de962db55c356a9bc2a541b01ca3cf8f500408ac 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ dependencies { testImplementation 'org.hamcrest:hamcrest-library:1.3' } -mainClassName = "shape.App" +mainClassName = "state.App" test { diff --git a/src/main/java/shape/App.java b/src/main/java/shape/App.java index f83bfd0c9bf1c78709a981941f806f5f1f03d195..c5ec45dc36621fdf62babb6a308158d0b2e3b173 100644 --- a/src/main/java/shape/App.java +++ b/src/main/java/shape/App.java @@ -1,16 +1,11 @@ package shape; import javafx.application.Application; -import javafx.geometry.Point2D; import javafx.scene.Group; import javafx.scene.Scene; -import javafx.scene.canvas.Canvas; -import javafx.scene.canvas.GraphicsContext; -import javafx.scene.paint.Color; import javafx.stage.Stage; - public class App extends Application { @Override @@ -20,7 +15,7 @@ public class App extends Application { Group root = new Group(); root.getChildren().add(drawer.getCanvas()); - Scene scene = new Scene(root, 400, 400); + Scene scene = new Scene(root, 600, 400); primaryStage.setTitle("JavaFX Drawer"); primaryStage.setScene(scene); diff --git a/src/main/java/shape/Drawer.java b/src/main/java/shape/Drawer.java index c9d157cdf52c9deb8e2b632b78af43001e122bd1..6ace9a37fbb186d6ad03d5baa71f5dba78427cfc 100644 --- a/src/main/java/shape/Drawer.java +++ b/src/main/java/shape/Drawer.java @@ -1,10 +1,12 @@ package shape; + import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.input.KeyCode; import javafx.scene.input.MouseButton; - +import state.DrawerContext; +import state.StateMoveShape; import java.util.ArrayList; import java.util.List; @@ -19,6 +21,7 @@ public class Drawer { private String currentShapeType = "rectangle"; private Shape tempShape = null; private boolean isFinished = false; + public DrawerContext context; public Drawer(double width, double height) { @@ -50,6 +53,7 @@ public class Drawer { double x = event.getX(); double y = event.getY(); // tempShape = new Rectangle(x, y, 0, 0); + System.out.println(currentShapeType); if (currentShapeType.equals("rectangle")) { tempShape = new Rectangle(x, y, 0, 0); }else if (currentShapeType.equals("circle")) { @@ -95,7 +99,7 @@ public class Drawer { currentShapeType = "circle"; System.out.println("Current shape type is: " + currentShapeType); } else if (event.getCode() == KeyCode.M) { - currentShapeType = state.StateMoveShape(); + currentShapeType = null ; } }); } diff --git a/src/main/java/state/App.java b/src/main/java/state/App.java index 09f71cfcd001774eae6aab16480707e102a24332..7a694f230df4444885133e0ecabc53d42f2ba8c0 100644 --- a/src/main/java/state/App.java +++ b/src/main/java/state/App.java @@ -4,6 +4,7 @@ import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; +import shape.Circle; public class App extends Application { @@ -15,6 +16,7 @@ public class App extends Application { public void start(Stage primaryStage) { Group root = new Group(); Drawer container = new Drawer(800, 600); + root.getChildren().add(container); primaryStage.setScene(new Scene(root)); primaryStage.show(); diff --git a/src/main/java/state/Drawer.java b/src/main/java/state/Drawer.java index 5e2b152cc57104be0c95ac1cbcada797d50fb01a..0533cbd3a6d220321f36f9bebc9319ee6050382f 100644 --- a/src/main/java/state/Drawer.java +++ b/src/main/java/state/Drawer.java @@ -29,6 +29,7 @@ public class Drawer extends Canvas { } public void addShape(Shape shape){ shapes.add(shape); + repaint(); } public Shape shapeContains(double x, double y){ for(Shape shape : shapes){ diff --git a/src/main/java/state/DrawerContext.java b/src/main/java/state/DrawerContext.java index 8a36641d31ddd658a89f3267a4817600976d7726..082b59fd27cca52a98b98901175030b36d2ee26f 100644 --- a/src/main/java/state/DrawerContext.java +++ b/src/main/java/state/DrawerContext.java @@ -35,12 +35,14 @@ public class DrawerContext { switch (event.getText()) { case "c": setState(new StateCircle0()); + System.out.println("circle state"); break; case "r": setState(new StateRectangle0()); break; case "m": setState(new StateMoveShape()); + System.out.println("state: " + currentState); break; default: setState(new NullDrawerState()); diff --git a/src/main/java/state/StateCircle0.java b/src/main/java/state/StateCircle0.java index c3608dee13e3c1c92b4e843387a29c40081b3ba4..2a91ba09f3cc4376a2ad1cbfe4e0fce5ccd61d5f 100644 --- a/src/main/java/state/StateCircle0.java +++ b/src/main/java/state/StateCircle0.java @@ -1,8 +1,14 @@ package state; +import shape.Circle; + public class StateCircle0 implements DrawerState { @Override public void mousePressed(DrawerContext context, double x, double y) { + Circle circle = new Circle(x, y,0); + circle.setFinished(false); + context.getDrawer().addShape(circle); + context.setState(new StateCircle1(circle)); } diff --git a/src/main/java/state/StateMoveShape.java b/src/main/java/state/StateMoveShape.java index b6e827553c6b9823be25970186599ae5652226d4..dfcbb295001b97d22c67c7a621dd766072548271 100644 --- a/src/main/java/state/StateMoveShape.java +++ b/src/main/java/state/StateMoveShape.java @@ -9,6 +9,7 @@ public class StateMoveShape implements DrawerState{ @Override public void mousePressed(DrawerContext context, double x, double y) { selectedShape = context.getDrawer().shapeContains(x,y); + System.out.println("selectedShape : " + selectedShape); if(selectedShape != null) { previousX = x; previousY = y; @@ -18,6 +19,11 @@ public class StateMoveShape implements DrawerState{ @Override public void mouseReleased(DrawerContext context, double x, double y) { + selectedShape = null; + } + + @Override + public void mouseMoved(DrawerContext context, double x, double y) { if(selectedShape != null) { double deltaX = x - previousX; double deltaY = y - previousY; @@ -28,11 +34,6 @@ public class StateMoveShape implements DrawerState{ } } - @Override - public void mouseMoved(DrawerContext context, double x, double y) { - selectedShape = null; - } - @Override public void paint(DrawerContext context) {