miyago icon

Labb3 - Rectangle

miyago | PRO | 09/22/19 07:33:13 PM UTC | 0 ⭐ | 444 👁️ | Never ⏰ | []
Java |

1.92 KB

|

None

|

0 👍

/

0 👎

import javafx.scene.canvas.GraphicsContext;
 
public class Rectangle extends FillableShape {
 
    private double width, height;
 
    public Rectangle() {
        this.width = 1;
        this.height = 1;
    }
 
    public Rectangle(double width_, double height_) {
        if (width_ < 0) {
            this.width = 1;
        } else {
            this.width = width_;
        }
        if (height < 0) {
            this.height = 1;
        } else {
            this.height = height_;
        }
    }
 
    public double getWidth() {
        return this.width;
    }
 
    public void setWidth(double width_) {
        if (width_ < 0) {
            this.width = 1;
        } else {
            this.width = width_;
        }
    }
 
    public double getHeight() {
        return this.height;
    }
 
    public void setHeight(double height_) {
        if (height < 0) {
            this.height = 1;
        } else {
            this.height = height_;
        }
    }
 
    @Override
    public void paint(GraphicsContext graphix){
        graphix.setStroke(super.getColor());
        
        if(this.isFilled() == true){
            graphix.setFill(super.getColor());
            graphix.fillRect(super.getX(), super.getY(), width, height);
        }else{
            graphix.strokeRect(super.getX(), super.getY(), width, height);
        }
    }
    @Override
    public void constrain(double sizeOfCanvasX, double sizeOfCanvasY, double sizeOfCanvasWidth, double sizeOfCanvasHeight){
            
 
     if(super.getX() < sizeOfCanvasX) {
         super.setVelocity(-super.getDx(),super.getDy());
        
    } else if (super.getX() > (sizeOfCanvasWidth-this.width)) {
        super.setVelocity(-super.getDx(),super.getDy());
    }
     
    if (super.getY() < sizeOfCanvasY) {
        super.setVelocity(super.getDx(),-super.getDy());
 
        
    } else if (super.getY() > (sizeOfCanvasHeight-this.height)) {
        super.setVelocity(super.getDx(),-super.getDy());
 
    }
 
    }
 
    public String toString() {
        String info = "Height: " + height + " Width: " + width;
        return info;
    }
 
}

Comments