Skip to content
Snippets Groups Projects
Rectangle.java 2.09 KiB
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 implements Shape{
    
    Chadi's avatar
    Chadi committed
        private double x,y,width, height;
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
        private boolean isFinished;
    
    Chadi's avatar
    Chadi committed
        public Color fillColor;
    
    
        public Rectangle(double x, double y, double width, double height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
    
    Chadi's avatar
    Chadi committed
            this.isFinished = true;
    
    BasileCouetoux's avatar
    BasileCouetoux committed
        }
    
        @Override
    
        public void paint(GraphicsContext graphicsContext) {
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
            graphicsContext.setStroke(Color.BLACK);
    
    Chadi's avatar
    Chadi committed
            graphicsContext.setLineWidth(2);
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
            if(isFinished){
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
                graphicsContext.setFill(Color.RED.deriveColor(0,1,1,0.5));
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
                graphicsContext.fillRect(x, y, width, height);
            }
    
            graphicsContext.strokeRect(x, y, width, height);
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    
    
        @Override
        public boolean contains(double x, double y) {
            return x>= this.x && x<= this.x + this.width
                    && y>= this.y && y<= this.y + this.height;
        }
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    
    
        @Override
        public void translate(double dx, double dy) {
            x += dx;
            y += dy;
    
    BasileCouetoux's avatar
    BasileCouetoux committed
        }
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
    
        @Override
        public boolean isFinished() {
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
            return isFinished;
        }
    
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
    
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
        public void updateSize(double newX, double newY) {
            this.width = Math.abs(newX - this.x);
            this.height = Math.abs(newY - this.y);
            if (newX < this.x && newY < this.y) {
                this.x = newX;
                this.y = newY;
            }
        }
    
    Chadi's avatar
    Chadi committed
    
        public Color getFillColor() {
            return fillColor;
        }
    
        public void setFinished(boolean finished) {
            isFinished = finished;
        }
    
        public void setFillColor(Color fillColor) {
            this.fillColor = fillColor;
        }
    
    MANSOUR Chadi's avatar
    MANSOUR Chadi committed
    
        public double getX() {
            return x;
        }
    
        public double getY() {
            return y;
        }
    
        public void setDimensions(double x, double y, double width, double height) {
            this.x = x;
            this.y = y;
            this.width = Math.abs(width);
            this.height = Math.abs(height);
            if(width < 0 ){ this.x += width;}
            if(height < 0){ this.y += height;}
        }
    
    BasileCouetoux's avatar
    BasileCouetoux committed
    }