Skip to content
Snippets Groups Projects
Drawer.java 1.14 KiB
Newer Older
  • Learn to ignore specific revisions
  • BasileCouetoux's avatar
    BasileCouetoux committed
    package state;
    
    import javafx.scene.canvas.Canvas;
    import shape.Shape;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class Drawer extends Canvas {
        protected List<Shape> shapes = new ArrayList<>();
    
    COUETOUX Basile's avatar
    COUETOUX Basile committed
        public DrawerContext context = new DrawerContext(this);
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    
        public Drawer(int width, int height) {
            super(width,height);
            setFocusTraversable(true);
            setOnMousePressed(event->context.mousePressed(event));
            setOnMouseReleased(event->context.mouseReleased(event));
            setOnMouseMoved(event->context.mouseMoved(event));
            setOnMouseDragged(event->context.mouseMoved(event));
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
            setOnKeyPressed(event -> context.keyPressed(event));
    
    BasileCouetoux's avatar
    BasileCouetoux committed
        }
    
        public void repaint(){
            this.getGraphicsContext2D().clearRect(0,0,this.getWidth(),this.getHeight());
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
            for(Shape shape : shapes){
                shape.paint(this.getGraphicsContext2D());
            }
        }
        public void addShape(Shape shape){
            shapes.add(shape);
        }
        public Shape shapeContains(double x, double y){
            for(Shape shape : shapes){
                if(shape.contains(x,y))  return shape;
            }
            return null;
    
    BasileCouetoux's avatar
    BasileCouetoux committed
        }
    }