Skip to content
Snippets Groups Projects
Select Git revision
  • ede6cf57f9572d8f9208da99f21ec3edb0c49fc7
  • main default protected
  • correction_video
  • going_further
  • ImprovedMouseInteraction
  • final2023
  • template
  • ModifGUI
8 results

CellularAutomatonSimulation.class

Blame
  • Forked from YAGOUBI Rim / Game of life Template
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Drawer.java 3.53 KiB
    package shape;
    
    import javafx.scene.canvas.Canvas;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.scene.input.KeyCode;
    import javafx.scene.input.MouseButton;
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Drawer {
        private List<Shape> shapes;
    //    private double width;
    //    private double height;
        private Canvas canvas;
        private GraphicsContext gc;
    
        private  String currentShapeType = "rectangle";
        private Shape tempShape = null;
        private boolean isFinished = false;
    
    
        public Drawer(double width, double height) {
    //        this.width = width;
    //        this.height = height;
            shapes = new ArrayList<>();
            canvas = new Canvas(width,height);
            gc = canvas.getGraphicsContext2D();
            setupMouseHandlers();
            setupKeyHandlers();
        }
    
        public void add(Shape shape) {
            shapes.add(shape);
        }
        public void repaint(){
            gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
            for(Shape shape : shapes){
                shape.paint(gc);
            }
            if (tempShape != null){
                tempShape.paint(gc);
            }
        }
    
        private void setupMouseHandlers(){
            canvas.setOnMousePressed(event -> {
                if(event.getButton() == MouseButton.PRIMARY){
                    double x = event.getX();
                    double y = event.getY();
                 //   tempShape = new Rectangle(x, y, 0, 0);
                    if (currentShapeType.equals("rectangle")) {
                        tempShape = new Rectangle(x, y, 0, 0);
                    }else if (currentShapeType.equals("circle")) {
                        tempShape = new Circle(x, y, 0);
                    }
                }
            });
    
            canvas.setOnMouseDragged(event ->{
                if (tempShape != null) {
                    double x = event.getX();
                    double y = event.getY();
                    if (tempShape instanceof Rectangle){
                        ((Rectangle) tempShape).updateSize(x,y);
                    }else if (tempShape instanceof Circle){
                        ((Circle) tempShape).updateRadius(x,y);
                    }
                    repaint();
                }
            });
    
            canvas.setOnMouseReleased(event -> {
                if(event.getButton() == MouseButton.PRIMARY && tempShape != null){
                    shapes.add(tempShape);
                    if (tempShape instanceof Rectangle){
                        tempShape.setFillColor(((Rectangle) tempShape).fillColor);
                        tempShape.isFinished();
                    }else if (tempShape instanceof Circle){
                        tempShape.setFillColor(((Circle) tempShape).fillColor);
                        tempShape.isFinished();
                    }
                    isFinished = true;
                    repaint();
                    tempShape = null;
                }
            });
        }
        private void setupKeyHandlers(){
            canvas.setFocusTraversable(true);
            canvas.setOnKeyPressed(event -> {
                if (event.getCode() == KeyCode.R){
                    currentShapeType = "rectangle";
                    System.out.println("Current shape type is: " + currentShapeType);
                }else if (event.getCode() == KeyCode.C){
                    currentShapeType = "circle";
                    System.out.println("Current shape type is: " + currentShapeType);
                }
            });
        }
    
       /* public Shape shapeContaining(double x, double y){
            for(Shape shape : shapes){
                if (shape.contains(x, y)){
                    return shape;
                }
            }
            return null;
        }*/
    
    
        public Canvas getCanvas() {
            return canvas;
        }
    }