DrawingPane.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package drawing;
  2. import javafx.scene.layout.Pane;
  3. import javafx.scene.layout.Region;
  4. import javafx.scene.shape.Rectangle;
  5. import javafx.scene.shape.Shape;
  6. import java.util.ArrayList;
  7. /**
  8. * Created by lewandowski on 20/12/2020.
  9. */
  10. public class DrawingPane extends Pane {
  11. private MouseMoveHandler mouseMoveHandler;
  12. private ArrayList<Shape> shapes;
  13. public DrawingPane() {
  14. clipChildren();
  15. shapes = new ArrayList<>();
  16. mouseMoveHandler = new MouseMoveHandler(this);
  17. }
  18. /**
  19. * Clips the children of this {@link Region} to its current size.
  20. * This requires attaching a change listener to the region’s layout bounds,
  21. * as JavaFX does not currently provide any built-in way to clip children.
  22. */
  23. void clipChildren() {
  24. final Rectangle outputClip = new Rectangle();
  25. this.setClip(outputClip);
  26. this.layoutBoundsProperty().addListener((ov, oldValue, newValue) -> {
  27. outputClip.setWidth(newValue.getWidth());
  28. outputClip.setHeight(newValue.getHeight());
  29. });
  30. }
  31. public void addShape(Shape shape) {
  32. shapes.add(shape);
  33. this.getChildren().add(shape);
  34. }
  35. public void removeShape(Shape shape) {
  36. shapes.remove(shape);
  37. this.getChildren().remove(shape);
  38. }
  39. public ArrayList<Shape> getShapes() {
  40. return shapes;
  41. }
  42. public void clear() {
  43. this.getChildren().removeAll(shapes);
  44. shapes.clear();
  45. }
  46. }