Skip to content
Snippets Groups Projects
Rectangle.java 821 B
Newer Older
  • Learn to ignore specific revisions
  • BasileCouetoux's avatar
    BasileCouetoux committed
    package shape;
    
    import javafx.geometry.Point2D;
    import javafx.scene.canvas.GraphicsContext;
    import javafx.scene.paint.Color;
    
    
    public class Rectangle extends AbstractShape{
        private final Color color;
    
        public Rectangle(Color color, Point2D point0, Point2D point1){
    
    BasileCouetoux's avatar
    BasileCouetoux committed
            this.color = color;
    
            addPoints(point0, point1);
    
    BasileCouetoux's avatar
    BasileCouetoux committed
        }
    
        @Override
    
        public void draw(GraphicsContext context) {
            context.setStroke(color);
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    
    
            Point2D point0 = points.get(0);
            Point2D point1 = points.get(1);
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    
    
            double x = Math.min(point0.getX(), point1.getX());
            double y = Math.min(point0.getY(), point1.getY());
            double width = Math.abs(point1.getX() - point0.getX());
            double height = Math.abs(point1.getY() - point0.getY());
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    
    
            context.strokeRect(x,y,width,height);
    
    BasileCouetoux's avatar
    BasileCouetoux committed
        }
    }