Advertisement
miyago

Labb3 - Rectangle

Sep 22nd, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1. import javafx.scene.canvas.GraphicsContext;
  2.  
  3. public class Rectangle extends FillableShape {
  4.  
  5.     private double width, height;
  6.  
  7.     public Rectangle() {
  8.         this.width = 1;
  9.         this.height = 1;
  10.     }
  11.  
  12.     public Rectangle(double width_, double height_) {
  13.         if (width_ < 0) {
  14.             this.width = 1;
  15.         } else {
  16.             this.width = width_;
  17.         }
  18.         if (height < 0) {
  19.             this.height = 1;
  20.         } else {
  21.             this.height = height_;
  22.         }
  23.     }
  24.  
  25.     public double getWidth() {
  26.         return this.width;
  27.     }
  28.  
  29.     public void setWidth(double width_) {
  30.         if (width_ < 0) {
  31.             this.width = 1;
  32.         } else {
  33.             this.width = width_;
  34.         }
  35.     }
  36.  
  37.     public double getHeight() {
  38.         return this.height;
  39.     }
  40.  
  41.     public void setHeight(double height_) {
  42.         if (height < 0) {
  43.             this.height = 1;
  44.         } else {
  45.             this.height = height_;
  46.         }
  47.     }
  48.  
  49.     @Override
  50.     public void paint(GraphicsContext graphix){
  51.         graphix.setStroke(super.getColor());
  52.        
  53.         if(this.isFilled() == true){
  54.             graphix.setFill(super.getColor());
  55.             graphix.fillRect(super.getX(), super.getY(), width, height);
  56.         }else{
  57.             graphix.strokeRect(super.getX(), super.getY(), width, height);
  58.         }
  59.     }
  60.     @Override
  61.     public void constrain(double sizeOfCanvasX, double sizeOfCanvasY, double sizeOfCanvasWidth, double sizeOfCanvasHeight){
  62.            
  63.  
  64.      if(super.getX() < sizeOfCanvasX) {
  65.          super.setVelocity(-super.getDx(),super.getDy());
  66.        
  67.     } else if (super.getX() > (sizeOfCanvasWidth-this.width)) {
  68.         super.setVelocity(-super.getDx(),super.getDy());
  69.     }
  70.      
  71.     if (super.getY() < sizeOfCanvasY) {
  72.         super.setVelocity(super.getDx(),-super.getDy());
  73.  
  74.        
  75.     } else if (super.getY() > (sizeOfCanvasHeight-this.height)) {
  76.         super.setVelocity(super.getDx(),-super.getDy());
  77.  
  78.     }
  79.  
  80.     }
  81.  
  82.     public String toString() {
  83.         String info = "Height: " + height + " Width: " + width;
  84.         return info;
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement