diff --git a/common/src/main/java/utils/ShapeUtils.java b/common/src/main/java/utils/ShapeUtils.java new file mode 100644 index 0000000..fd90a57 --- /dev/null +++ b/common/src/main/java/utils/ShapeUtils.java @@ -0,0 +1,85 @@ +package utils; + +import javafx.scene.effect.DropShadow; +import javafx.scene.effect.Effect; +import javafx.scene.effect.Glow; +import javafx.scene.paint.Color; +import javafx.scene.shape.StrokeType; +import lombok.Getter; +import org.apache.log4j.Logger; + +//singleton for constants and utils of shapes +public class ShapeUtils { + private static final Logger log = Logger.getLogger(ShapeUtils.class); + private static ShapeUtils instance; + @Getter + private final Effect EFFECT_SELECTED; + @Getter + private final Effect EFFECT_ENTERED; + @Getter + private final Effect EFFECT_DEFAULT; + @Getter + private final double OPACITY_SELECTED; + @Getter + private final double OPACITY_DEFAULT; + @Getter + private final double OPACITY_ENTERED; + @Getter + private final double STROKE_WIDTH_SELECTED; + @Getter + private final double STROKE_WIDTH_DEFAULT; + @Getter + private final double STROKE_WIDTH_ENTERED; + @Getter + private final Color FILL_SELECTED; + @Getter + private final Color FILL_DEFAULT; + @Getter + private final Color FILL_ENTERED; + @Getter + private final Color STROKE_SELECTED; + @Getter + private final Color STROKE_DEFAULT; + @Getter + private final Color STROKE_ENTERED; + @Getter + private final StrokeType STROKE_TYPE_DEFAULT; + @Getter + private final StrokeType STROKE_TYPE_INSIDE; + + public static final int TYPE_ELLIPSE = 1; + public static final int TYPE_POLYLINE = 0; + + + + public static ShapeUtils getInstance() { + if (instance == null) { + instance = new ShapeUtils(); + log.info("Utils instance made"); + } + return instance; + } + + private ShapeUtils(){ + Glow glow = new Glow(); + glow.setLevel(650); + glow.setInput(new DropShadow(9,Color.BLACK)); + EFFECT_SELECTED = glow; + EFFECT_ENTERED = new DropShadow(13,Color.LIGHTGRAY); + EFFECT_DEFAULT = null; + OPACITY_SELECTED = 0.7; + OPACITY_DEFAULT = 0.5; + OPACITY_ENTERED= 0.6; + STROKE_WIDTH_SELECTED = 0.5; + STROKE_WIDTH_DEFAULT = 0.5; + STROKE_WIDTH_ENTERED = 0.1; + FILL_SELECTED = Color.DARKGRAY; + FILL_DEFAULT = Color.GRAY; + FILL_ENTERED = Color.LIGHTGRAY; + STROKE_SELECTED = Color.DARKGRAY; + STROKE_DEFAULT = Color.BLACK; + STROKE_ENTERED = Color.BLACK; + STROKE_TYPE_DEFAULT = StrokeType.CENTERED; + STROKE_TYPE_INSIDE = StrokeType.INSIDE; + } +} diff --git a/common/src/main/java/utils/Utils.java b/common/src/main/java/utils/Utils.java index 9af6cd2..8b9cc8c 100644 --- a/common/src/main/java/utils/Utils.java +++ b/common/src/main/java/utils/Utils.java @@ -3,50 +3,48 @@ import javafx.geometry.Rectangle2D; import javafx.scene.layout.Region; import javafx.stage.Screen; +import lombok.Getter; import org.apache.log4j.Logger; +//singleton class containing the constants and utils of project + public class Utils { private static final Logger log = Logger.getLogger(Utils.class); - private double height; - private double width; private static Utils instance; + @Getter + private double screenHeight; + @Getter + private double screenWidth; - private Utils(){ + private Utils() { refreshScreen(); } - public static Utils getInstance(){ - if (instance == null){ + public static Utils getInstance() { + if (instance == null) { instance = new Utils(); log.info("Utils instance made"); } return instance; } - public void refreshScreen() { - Rectangle2D currentScreen = Screen.getPrimary().getVisualBounds(); - height = currentScreen.getHeight(); - width = currentScreen.getWidth(); - } - - public double getScreenWidth() { - return width; + //match a number with optional '-' and decimal. + public static boolean isNumeric(String str) { + return str.matches("-?\\d+(\\.\\d+)?"); } - public double getScreenHeight() { - return height; + public void refreshScreen() { + Rectangle2D currentScreen = Screen.getPrimary().getVisualBounds(); + screenHeight = currentScreen.getHeight(); + screenWidth = currentScreen.getWidth(); } public void setPercentageWidth(Region element, double percentage) { - element.setPrefWidth(width * percentage / 100); + element.setPrefWidth(screenWidth * percentage / 100); } public void setPercentageHeight(Region element, double percentage) { - element.setPrefHeight(height * percentage / 100); + element.setPrefHeight(screenHeight * percentage / 100); } - public static boolean isNumeric(String str) - { - return str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal. - } } diff --git a/editor/src/main/java/app/EditorApp.java b/editor/src/main/java/app/EditorApp.java index 8841f3e..dc37a5e 100644 --- a/editor/src/main/java/app/EditorApp.java +++ b/editor/src/main/java/app/EditorApp.java @@ -9,15 +9,11 @@ import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.TextInputDialog; -import javafx.scene.image.Image; -import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; -import javafx.scene.layout.BorderPane; import javafx.stage.FileChooser; import javafx.stage.Stage; -import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONException; @@ -49,9 +45,8 @@ public class EditorApp extends Application { private Selection[] ADD_MENU_ACTIONS; private KeyCode[] ADD_MENU_TRIGGERS; - static Utils UTILS = Utils.getInstance() ; + private static Utils UTILS = Utils.getInstance(); private Scene mainScene; - private File mapsFolder; private String defaultPath ; private FileChooser fileChooser ; private File currentlyOpenFile; @@ -77,7 +72,7 @@ public void init (){ try { super.init(); } catch (Exception e) { - log.error(e); + log.error("Init method failed",e); } initConstants(); initActionsAndVariables(); @@ -112,7 +107,6 @@ public void start(Stage editorStage) { public void newMap() { currentlyOpenFile = null; - editScreen.resetScreen(); editScreen.setQuestions(new ArrayList()); editScreen.setBackgroundPath(defaultPath); @@ -415,11 +409,11 @@ private void initConstants(){ private void initMenus(){ mainMenu = new OptionMenu( - "main", - MAIN_MENU_TEXT, - MAIN_MENU_HINT_TEXT, - MAIN_MENU_ACTIONS, - MAIN_MENU_TRIGGERS + "main", + MAIN_MENU_TEXT, + MAIN_MENU_HINT_TEXT, + MAIN_MENU_ACTIONS, + MAIN_MENU_TRIGGERS ); editMenu = new OptionMenu( @@ -464,7 +458,7 @@ private void initActionsAndVariables() { try { defaultPath = Paths.get(resource.toURI()).toString(); } catch (URISyntaxException e) { - e.printStackTrace(); + log.error("Default path couldn't be set",e); } fileChooser = new FileChooser(); currentlyOpenFile = null; diff --git a/logic/src/main/java/core/Question.java b/logic/src/main/java/core/Question.java index 5345c79..e216620 100644 --- a/logic/src/main/java/core/Question.java +++ b/logic/src/main/java/core/Question.java @@ -1,7 +1,5 @@ package core; -import javafx.geometry.Insets; -import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import lombok.Data; import org.apache.log4j.Logger; diff --git a/logic/src/main/java/core/QuestionMultiple.java b/logic/src/main/java/core/QuestionMultiple.java index a1d1738..77a68ac 100644 --- a/logic/src/main/java/core/QuestionMultiple.java +++ b/logic/src/main/java/core/QuestionMultiple.java @@ -11,6 +11,7 @@ public class QuestionMultiple extends Question { private static final Logger log = Logger.getLogger(QuestionMultiple.class); + private static final int NUMBER_OF_ANSWERS = 4; @Getter private ArrayList answers ; @@ -35,11 +36,10 @@ public QuestionMultiple(JSONObject jsonQuestion) { } }); } else { - for (int i = 0; i < 4; i++) { + for (int i = 0; i < NUMBER_OF_ANSWERS; i++) { answers.add(new AnswerMultiple("", false)); } } - log.debug("Multiple choice question created"); } @@ -50,12 +50,12 @@ public JSONObject save() { JSONArray jsonArray = new JSONArray(); answers.forEach(a->{ try { - jsonArray.put(new JSONObject() - .put("text", a.getAnswerText().getText()) - .put("correct", a.isCorrect())); + jsonArray + .put(new JSONObject() + .put("text", a.getAnswerText().getText()) + .put("correct", a.isCorrect())); } catch (JSONException e) { - log.error(e); - e.printStackTrace(); + log.error("Error with saving json",e); } }); jsonObject diff --git a/ui/src/main/java/components/AnchorImageView.java b/ui/src/main/java/components/AnchorImageView.java index d54d708..29f7e2d 100644 --- a/ui/src/main/java/components/AnchorImageView.java +++ b/ui/src/main/java/components/AnchorImageView.java @@ -52,60 +52,37 @@ public AnchorImageView(Image img, LayoutEdit parent){ this::MousePressed, this::DoubleClick) ); - - log.debug("anchorImageView constructor"); - } - - private void shapeMouseSecondoryClicked (MouseEvent event, Shapes currentShape) { - log.debug("shapes on pane: " + shapesOnPane.size()); - if (event.getButton() == MouseButton.SECONDARY) { - currentlySelectedShape = currentShape; - shapesOnPane.forEach(shape -> shape.setSelected(false)); - currentShape.setSelected(true); - log.debug("index of selected shape: "+shapesOnPane.indexOf(currentShape)); - this.parent.setSelectedQuestion(shapesOnPane.indexOf(currentShape)); - //redrawShapes(currentShape); - log.debug("secondory click"); - } - event.consume(); } - private void saveShape(Shapes currentShape) { currentShape.shape.setOnMouseClicked(event -> shapeMouseSecondoryClicked(event,currentShape)); - log.debug("current shape " + currentShape); currentShape.setArea(); - log.debug("AREA: "+currentShape.getArea()); if (currentShape.getArea()>20) { - log.debug("Area bigger thaan 10"); + //TODO smisli neki dobar kriterijum za "getArea" ne prihvatanje nacrtanog oblika :) shapesOnPane.add(currentShape); this.parent.addQuestion(currentlySelectedShape); shapesOnPane.forEach(shape -> { - if (shape != currentlySelectedShape) shape.setSelected(false); + if (shape != currentlySelectedShape){ + shape.setSelected(false); + } }); - } - else { + } else { currentShape.delete(); } } - /* private void redrawShapes(Shapes selectedShape) { - this.getChildren().clear(); - mapImageView.setImage(currentImage); - this.getChildren().add(mapImageView); - shapesOnPane.forEach(shape -> { - if (shape != selectedShape) shape.setSelected(false); - else shape.setSelected(true); - }); - this.getChildren().addAll(shapesOnPane.stream().map(Shapes::getShape).collect(Collectors.toList())); - - log.debug("Redrawed"); - }*/ + private void shapeMouseSecondoryClicked (MouseEvent event, Shapes currentShape) { + if (event.getButton() == MouseButton.SECONDARY) { + currentlySelectedShape = currentShape; + shapesOnPane.forEach(shape -> shape.setSelected(false)); + currentShape.setSelected(true); + this.parent.setSelectedQuestion(shapesOnPane.indexOf(currentShape)); + } + event.consume(); + } private void MousePressed(MouseEvent event) { - double xPosition = event.getX(); - double yPosition = event.getY(); - xPosition = setPositionLimitHelper(xPosition, WIDTH, 6); - yPosition = setPositionLimitHelper(yPosition, HEIGHT, 6); + double xPosition = setPositionLimitHelper(event.getX(), WIDTH, 6); + double yPosition = setPositionLimitHelper(event.getY(), HEIGHT, 6); x = xPosition; y = yPosition; event.consume(); @@ -113,10 +90,8 @@ private void MousePressed(MouseEvent event) { } private void DoubleClick(MouseEvent event) { - double xPosition = event.getX(); - double yPosition = event.getY(); - xPosition = setPositionLimitHelper(xPosition, WIDTH, 6); - yPosition = setPositionLimitHelper(yPosition, HEIGHT, 6); + double xPosition = setPositionLimitHelper(event.getX(), WIDTH, 6); + double yPosition = setPositionLimitHelper(event.getY(), HEIGHT, 6); ((ShapePolygon) currentShape).addPoint(xPosition, yPosition); currentShape.finish(); currentlySelectedShape = currentShape; @@ -127,12 +102,12 @@ private void DoubleClick(MouseEvent event) { private void MouseReleased(MouseEvent event) { if (dragged) { - double xPosition = event.getX(); - double yPosition = event.getY(); - xPosition = setPositionLimitHelper(xPosition, WIDTH, 6); - yPosition = setPositionLimitHelper(yPosition, HEIGHT, 6); - //mouseReleasedPolygon(event, xPosition, yPosition); - mouseReleasedEllipse(event,xPosition,yPosition); + double xPosition = setPositionLimitHelper(event.getX(), WIDTH, 6); + double yPosition = setPositionLimitHelper(event.getY(), HEIGHT, 6); + //************************************************** + mouseReleasedPolygon(event, xPosition, yPosition); + //************************************************** + //mouseReleasedEllipse(event,xPosition,yPosition); currentShape.finish(); currentlySelectedShape = currentShape; saveShape(currentShape); @@ -154,89 +129,82 @@ private void mouseReleasedEllipse(MouseEvent event, double xpos, double ypos){ } private void MouseClicked(MouseEvent event) { - double xPosition = event.getX(); - double yPosition = event.getY(); - //redrawShapes(currentlySelectedShape); - xPosition = setPositionLimitHelper(xPosition, WIDTH, 6); - yPosition = setPositionLimitHelper(yPosition, HEIGHT, 6); - if (currentShape == null) { - currentShape = new ShapePolygon(x, y); - this.getChildren().add(currentShape.shape); - } else if (currentShape instanceof ShapePolygon){ - ((ShapePolygon) currentShape).addPoint(xPosition, yPosition); - } + double xPosition = setPositionLimitHelper(event.getX(), WIDTH, 6); + double yPosition = setPositionLimitHelper(event.getY(), HEIGHT, 6); + handlePolygon(xPosition, yPosition); log.debug("moouse clicked"); } private void MouseDragged(MouseEvent event) { double xPosition = event.getX(); double yPosition = event.getY(); - //redrawShapes(currentlySelectedShape); xPosition = setPositionLimitHelper(xPosition, WIDTH, 6); yPosition = setPositionLimitHelper(yPosition, HEIGHT, 6); - //mouseDraggedPolygon(event,xPosition,yPosition); - mouseDraggedEllipse(event,xPosition,yPosition); + //*********************************************** + mouseDraggedPolygon(event,xPosition,yPosition); + //*********************************************** + //mouseDraggedEllipse(event,xPosition,yPosition); dragged = true; event.consume(); - } private void mouseDraggedPolygon (MouseEvent event,double xpos, double ypos){ + handlePolygon(xpos, ypos); + } + private void handlePolygon(double xpos, double ypos) { + //Ovo je u funkciji jer se potpuno ista stvar radi i za mousedragged i za mouseclicked + //doduse nikad istovremeno, ali da bismo mogli u slucaju potrebe da se prebacimo na specifikaciju zadatka if (currentShape == null) { - log.debug("Current shape je null"); currentShape = new ShapePolygon(x, y); this.getChildren().add(currentShape.shape); - log.debug("Velicina pane-a ??? " +this.getChildren().size()); } else if (currentShape instanceof ShapePolygon) { ((ShapePolygon) currentShape).addPoint(xpos, ypos); } } + private void mouseDraggedEllipse (MouseEvent event,double xpos, double ypos){ if (currentShape == null) { currentShape= new ShapeEllipse( (xpos + x) / 2, (ypos + y) / 2, - Math.abs((xpos - x) / 2), + Math.abs(xpos - x) / 2, Math.abs(ypos - y) / 2); - log.debug("created ellipse"); this.getChildren().add(currentShape.shape); - log.debug("getChildren : " + this.getChildren().size()); - /*((ShapeEllipse)currentShape).setRadiusXposition(xpos); - ((ShapeEllipse)currentShape).setRadiusYposition(ypos);*/ } else if (currentShape instanceof ShapeEllipse){ - log.debug("u elsu"); ((ShapeEllipse)currentShape).setEllipse( (xpos + x) / 2, (ypos + y) / 2, - Math.abs((xpos - x) / 2), + Math.abs(xpos - x) / 2, Math.abs(ypos - y) / 2); } } public void setMapImageView(Image image){ - //graphicsContext.clearRect(0,0,WIDTH,HEIGHT); this.getChildren().clear(); mapImageView.setImage(image); currentImage = image; this.getChildren().add(mapImageView); - log.debug("shapes on pane: " + shapesOnPane.size()); } public void setShapes(ArrayList shapes) { - log.debug("shapes on pane: " + shapesOnPane.size()); if (!shapes.isEmpty()){ currentlySelectedShape = shapes.get(0); shapesOnPane = shapes; - log.debug("shapes on pane size : "+shapesOnPane.size()); shapesOnPane.forEach(shape -> { - if (shape != currentlySelectedShape) shape.setSelected(false); - else shape.setSelected(true); + if (shape != currentlySelectedShape){ + shape.setSelected(false); + } else { + shape.setSelected(true); + } shape.getShape().setOnMouseClicked(event -> shapeMouseSecondoryClicked(event,shape)); }); - this.getChildren().addAll(shapesOnPane.stream().map(Shapes::getShape).collect(Collectors.toList())); - log.debug("Size of children : "+ this.getChildren().size()); + this.getChildren() + .addAll(shapesOnPane.stream() + .map(Shapes::getShape) + .collect(Collectors.toList()) + ); } else { shapesOnPane = shapes; } diff --git a/ui/src/main/java/components/ShapeEllipse.java b/ui/src/main/java/components/ShapeEllipse.java index 0e07f15..47a1258 100644 --- a/ui/src/main/java/components/ShapeEllipse.java +++ b/ui/src/main/java/components/ShapeEllipse.java @@ -4,6 +4,7 @@ import lombok.Setter; import org.apache.log4j.Logger; import org.json.JSONObject; +import utils.ShapeUtils; public class ShapeEllipse extends Shapes{ @@ -47,12 +48,11 @@ public void finish() { log.debug("FinishEllipse finished finished"); } - @Override public JSONObject save() { log.debug("Save Ellipse"); return new JSONObject() - .put("type", 1) + .put("type", ShapeUtils.TYPE_ELLIPSE) .put("centreX", this.centreX) .put("centreY", this.centreY) .put("radiusX", this.radiusX) diff --git a/ui/src/main/java/components/ShapePolygon.java b/ui/src/main/java/components/ShapePolygon.java index d80de55..7777dcc 100644 --- a/ui/src/main/java/components/ShapePolygon.java +++ b/ui/src/main/java/components/ShapePolygon.java @@ -8,104 +8,29 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import utils.ShapeUtils; import java.util.ArrayList; -public class ShapePolygon extends Shapes { - @Getter - @Setter - private ArrayList coordinates; +public class ShapePolygon extends ShapePolyline{ - private double minX = Double.MAX_VALUE; - private double maxX = -Double.MAX_VALUE; - private double minY = Double.MAX_VALUE; - private double maxY = -Double.MAX_VALUE; - private int id =0; - - private static Logger log = Logger.getLogger(ShapePolygon.class); - - public ShapePolygon(double ...coordinates){ + public ShapePolygon(JSONObject jsonObject){ super(); - initPolygon(coordinates); - } - - public ShapePolygon(JSONObject jsonObject) { - JSONArray jsonArray = jsonObject.getJSONArray("coordinates"); - this.coordinates = new ArrayList<>(); - - for (int i = 0; i < jsonArray.length() ; i ++){ - if (shape == null){ - initPolygon(jsonArray.getJSONObject(0).getDouble("x"), jsonArray.getJSONObject(0).getDouble("y")); - } - else { - this.addPoint(jsonArray.getJSONObject(i).getDouble("x"),jsonArray.getJSONObject(i).getDouble("y")); - } - } - - finish(); - } - - private void initPolygon(double ... coords){ - this.coordinates = new ArrayList<>(); - shape=new Polyline(coords); - for ( double d : coords){ - this.coordinates.add(d); - } - this.setSelected(false); - //shape.setFill(null); - setListeners(); + //finish(); } - public void addPoint (double x, double y){ - ((Polyline)shape).getPoints().add(x); - ((Polyline)shape).getPoints().add(y); - this.coordinates.add(x); - this.coordinates.add(y); - this.setSelected(false); - //shape.setFill(null); - // ((Polyline)shape).getPoints().stream().map(Object::toString).forEach(e -> log.debug("cord: " + e + " , ")); - //log.debug("addPoint finished"); - } - @Override - protected void setArea(){ - for(int i = 0; i < coordinates.size(); i += 2) { - double x = coordinates.get(i); - double y = coordinates.get(i+1); - minX = Math.min(minX, x); - maxX = Math.max(maxX, x); - minY = Math.min(minY, y); - maxY = Math.max(maxY, y); - } - double width = maxX - minX; - double height = maxY - minY; - this.area = width * height; - //log.debug("setArea finished"); - } - - @Override - public void finish() { - ((Polyline)shape).getPoints().add(((Polyline)shape).getPoints().get(0)); - ((Polyline)shape).getPoints().add(((Polyline)shape).getPoints().get(1)); - this.setSelected(true); - // ((Polyline)shape).getPoints().stream().map(Object::toString).forEach(e -> log.debug("cord: " + e + " , ")); - log.debug("FinishPolygon finished"); - } - - @Override - public void delete() { - shape = new Polyline(); - this.setSelected(false); + public ShapePolygon(double ...coordinates){ + super(coordinates); } @Override public JSONObject save() { - log.debug("Save Polygon"); JSONArray jsonArray = new JSONArray(); for (int i = 0 ; i < coordinates.size() ; i+=2){ jsonArray.put((new JSONObject()).put("x",coordinates.get(i)).put("y", coordinates.get(i+1))); } return new JSONObject() - .put("type", 0) + .put("type", ShapeUtils.TYPE_POLYLINE) .put("coordinates", jsonArray) .put("id", id); } diff --git a/ui/src/main/java/components/ShapePolyline.java b/ui/src/main/java/components/ShapePolyline.java new file mode 100644 index 0000000..13ca4b6 --- /dev/null +++ b/ui/src/main/java/components/ShapePolyline.java @@ -0,0 +1,108 @@ +package components; + +import javafx.scene.shape.Polyline; +import lombok.Getter; +import lombok.Setter; +import org.apache.log4j.Logger; +import org.json.JSONArray; +import org.json.JSONObject; +import utils.ShapeUtils; + +import java.util.ArrayList; + +public class ShapePolyline extends Shapes { + @Getter + @Setter + protected ArrayList coordinates; + private double minX = Double.MAX_VALUE; + private double maxX = -Double.MAX_VALUE; + private double minY = Double.MAX_VALUE; + private double maxY = -Double.MAX_VALUE; + protected int id =0; + + private static Logger log = Logger.getLogger(ShapePolygon.class); + + public ShapePolyline(double ...coordinates){ + super(); + initPolygon(coordinates); + } + + public ShapePolyline(JSONObject jsonObject) { + JSONArray jsonArray = jsonObject.getJSONArray("coordinates"); + this.coordinates = new ArrayList<>(); + for (int i = 0; i < jsonArray.length() ; i ++){ + if (shape == null){ + initPolygon( + jsonArray.getJSONObject(0).getDouble("x"), + jsonArray.getJSONObject(0).getDouble("y")); + } else { + this.addPoint( + jsonArray.getJSONObject(i).getDouble("x"), + jsonArray.getJSONObject(i).getDouble("y")); + } + } + } + + private void initPolygon(double ... coords){ + this.coordinates = new ArrayList<>(); + shape=new Polyline(coords); + for ( double d : coords){ + this.coordinates.add(d); + } + this.setSelected(false); + //shape.setFill(null); + setListeners(); + } + + public void addPoint (double x, double y){ + ((Polyline)shape).getPoints().add(x); + ((Polyline)shape).getPoints().add(y); + this.coordinates.add(x); + this.coordinates.add(y); + this.setSelected(false); + //shape.setFill(null); + // ((Polyline)shape).getPoints().stream().map(Object::toString).forEach(e -> log.debug("cord: " + e + " , ")); + //log.debug("addPoint finished"); + } + @Override + protected void setArea(){ + for(int i = 0; i < coordinates.size(); i += 2) { + double x = coordinates.get(i); + double y = coordinates.get(i+1); + minX = Math.min(minX, x); + maxX = Math.max(maxX, x); + minY = Math.min(minY, y); + maxY = Math.max(maxY, y); + } + this.area = (maxX - minX) * (maxY - minY); + //log.debug("setArea finished"); + } + + @Override + public void finish() { + ((Polyline)shape).getPoints().add(((Polyline)shape).getPoints().get(0)); + ((Polyline)shape).getPoints().add(((Polyline)shape).getPoints().get(1)); + this.setSelected(true); + //((Polyline)shape).getPoints().stream().map(Object::toString).forEach(e -> log.debug("cord: " + e + " , ")); + log.debug("FinishPolygon finished"); + } + + @Override + public void delete() { + shape = new Polyline(); + this.setSelected(false); + } + + @Override + public JSONObject save() { + log.debug("Save Polygon"); + JSONArray jsonArray = new JSONArray(); + for (int i = 0 ; i < coordinates.size() ; i+=2){ + jsonArray.put((new JSONObject()).put("x",coordinates.get(i)).put("y", coordinates.get(i+1))); + } + return new JSONObject() + .put("type", ShapeUtils.TYPE_POLYLINE) + .put("coordinates", jsonArray) + .put("id", id); + } +} diff --git a/ui/src/main/java/components/Shapes.java b/ui/src/main/java/components/Shapes.java index 38c07ab..6e68249 100644 --- a/ui/src/main/java/components/Shapes.java +++ b/ui/src/main/java/components/Shapes.java @@ -12,14 +12,16 @@ import lombok.Setter; import org.apache.log4j.Logger; import org.json.JSONObject; +import utils.ShapeUtils; public abstract class Shapes { + private static final Logger log = Logger.getLogger(Shapes.class); + private static final ShapeUtils shapeUtils = ShapeUtils.getInstance(); @Setter @Getter protected Shape shape; @Getter boolean selected; - private static final Logger log = Logger.getLogger(Shapes.class); @Getter protected double area; protected Shapes() { @@ -29,32 +31,46 @@ protected Shapes() { public void setSelected(boolean selected) { if (selected){ - Glow glow = new Glow(); - glow.setLevel(650); - glow.setInput(new DropShadow(9,Color.BLACK)); - this.setShapeLook(0.7,Color.DARKGRAY,Color.DARKGRAY,0.5,null,glow); + this.setShapeLook( + shapeUtils.getOPACITY_SELECTED(), + shapeUtils.getFILL_SELECTED(), + shapeUtils.getSTROKE_SELECTED(), + shapeUtils.getSTROKE_WIDTH_SELECTED(), + shapeUtils.getSTROKE_TYPE_DEFAULT(), + shapeUtils.getEFFECT_SELECTED()); this.selected = true; //log.debug( "setSelected on TRUE"); } else { - this.setShapeLook(0.5,Color.GRAY,Color.BLACK,0.5,StrokeType.INSIDE,null); + this.setShapeLook( + shapeUtils.getOPACITY_DEFAULT(), + shapeUtils.getFILL_DEFAULT(), + shapeUtils.getSTROKE_DEFAULT(), + shapeUtils.getSTROKE_WIDTH_DEFAULT(), + shapeUtils.getSTROKE_TYPE_INSIDE(), //TODO check why is only here this stroke type, what is this anyway + shapeUtils.getEFFECT_DEFAULT()); this.selected = false; //log.debug("setSelected on FALSE"); } } protected void setListeners() { - shape.addEventHandler(MouseEvent.ANY, new ShapesHandler( this::setEntered, this::setExited - )); + )); } public void setEntered(MouseEvent event){ if (!isSelected()) { - this.setShapeLook(0.6,Color.LIGHTGRAY,Color.BLACK,0.1,null,(new DropShadow(13,Color.LIGHTGRAY))); + this.setShapeLook( + shapeUtils.getOPACITY_ENTERED(), + shapeUtils.getFILL_ENTERED(), + shapeUtils.getSTROKE_ENTERED(), + shapeUtils.getSTROKE_WIDTH_ENTERED(), + shapeUtils.getSTROKE_TYPE_DEFAULT(), + shapeUtils.getEFFECT_ENTERED()); // log.debug("Entered"); event.consume(); } @@ -63,13 +79,17 @@ public void setEntered(MouseEvent event){ public void setExited(MouseEvent event){ if (!isSelected()) { //log.debug("Exited"); - this.setShapeLook(0.5,Color.GRAY,Color.BLACK,0.1,null,null); + this.setShapeLook( + shapeUtils.getOPACITY_DEFAULT(), + shapeUtils.getFILL_DEFAULT(), + shapeUtils.getSTROKE_DEFAULT(), + shapeUtils.getSTROKE_WIDTH_DEFAULT(), + shapeUtils.getSTROKE_TYPE_INSIDE(), + shapeUtils.getEFFECT_DEFAULT()); event.consume(); } } - - private void setShapeLook(double opacity, Paint fill, Paint stroke, double strokeWidth, StrokeType strokeType, Effect effect){ shape.setOpacity(opacity); shape.setFill(fill); @@ -77,7 +97,6 @@ private void setShapeLook(double opacity, Paint fill, Paint stroke, double strok shape.setStrokeWidth(strokeWidth); shape.setStrokeType(strokeType); shape.setEffect(effect); - log.debug("SetShapeLook finished"); } protected abstract void setArea(); @@ -87,4 +106,4 @@ private void setShapeLook(double opacity, Paint fill, Paint stroke, double strok public abstract void delete(); public abstract JSONObject save(); -} +} \ No newline at end of file diff --git a/ui/src/main/java/components/ShapesHandler.java b/ui/src/main/java/components/ShapesHandler.java index 3330cab..91ccda6 100644 --- a/ui/src/main/java/components/ShapesHandler.java +++ b/ui/src/main/java/components/ShapesHandler.java @@ -7,13 +7,9 @@ public class ShapesHandler implements EventHandler { private final EventHandler onEnteredShapeEventHandler; private final EventHandler onExitedShapeEventHandler; - - private boolean dragging = false; - public ShapesHandler(EventHandler onEnteredShapeEventHandler, EventHandler onExitedShapeEventHandler){ this.onEnteredShapeEventHandler = onEnteredShapeEventHandler; this.onExitedShapeEventHandler = onExitedShapeEventHandler; - } @Override