Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. package geometri;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5.  
  6. /**
  7.  * A drawable line defined by its starting and ending position in two-dimensional space, and by its color.
  8.  * The starting position is defined by the position of the upper left corner of the smallest rectangle
  9.  * that covers the entire line as neatly as possible.
  10.  */
  11.  
  12. public class Line extends GeometricShape2D {
  13.    
  14.     /** The x-coordinate of the starting point of this line. */
  15.     private int x1;
  16.    
  17.     /** The y-coordinate of the starting point of this line. */
  18.     private int y1;
  19.    
  20.     /** The x-coordinate of the endpoint of this line. */
  21.     private int x2;
  22.    
  23.     /** The y-coordinate of the endpoint of this line. */
  24.     private int y2;
  25.    
  26.     /**
  27.      * Create a new line with the given starting point (x1, y1), endpoint (x2, y2) and color c.
  28.      * @param x1 The x-coordinate of the starting point of the line.
  29.      * @param y1 The y-coordinate of the starting point of the line.
  30.      * @param x2 The x-coordinate of the endpoint of the line.
  31.      * @param y2 The y-coordinate of the endpoint of the line.
  32.      * @param c The color of the line.
  33.      * @throws IllegalPositionException if x1, y1, x2 or y2 is negative.
  34.      */
  35.     public Line(int x1, int y1, int x2, int y2, Color c) throws IllegalPositionException {
  36.         super(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2), c);
  37.         this.x1 = x1;
  38.         this.y1 = y1;
  39.         this.x2 = x2;
  40.         this.y2 = y2;
  41.     }
  42.    
  43.     /**
  44.      * Create a new line with starting point as given by f1, endpoint as given by f2 and color c.
  45.      * @param f1 The GeometricShape with the desired starting point of the line.
  46.      * @param f2 The GeometricShape with the desired endpoint of the line.
  47.      * @param c The color of the line.
  48.      */
  49.     public Line(GeometricShape f1, GeometricShape f2, Color c) {
  50.         super(f1, f2, c);
  51.         this.x1 = f1.getX();
  52.         this.y1 = f1.getY();
  53.         this.x2 = f2.getX();
  54.         this.y2 = f2.getY();
  55.     }
  56.    
  57.     /**
  58.      * {@inheritDoc}
  59.      */
  60.     @Override
  61.     public int getArea() {
  62.         return 0;
  63.     }
  64.    
  65.     /**
  66.      * {@inheritDoc}
  67.      */
  68.     @Override
  69.     public void fill(Graphics g) {
  70.         g.setColor(this.getColor());
  71.         g.drawLine(this.x1, this.y1, this.x2, this.y2);
  72.     }
  73.    
  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     @Override
  78.     public void move(int dx, int dy) throws IllegalPositionException {
  79.         super.move(dx, dy);
  80.         this.x1 += dx;
  81.         this.y1 += dy;
  82.         this.x2 += dx;
  83.         this.y2 += dy;
  84.     }
  85.    
  86.     /**
  87.      * {@inheritDoc}
  88.      */
  89.     @Override
  90.     public int getPerimeter() {
  91.         double length = Math.sqrt(Math.pow(this.x2 - this.x1, 2.0) + Math.pow(this.y2 - this.y1, 2.0));
  92.         return (int) (length*2.0);
  93.     }
  94.    
  95.     /**
  96.      * {@inheritDoc}
  97.      */
  98.     @Override
  99.     public void place(int x, int y) throws IllegalPositionException {
  100.         super.place(x, y);
  101.        
  102.         int dx = x - Math.min(this.x1, this.x2);
  103.         int dy = y - Math.min(this.y1, this.y2);
  104.         this.x1 += dx;
  105.         this.y1 += dy;
  106.         this.x2 += dx;
  107.         this.y2 += dy;
  108.     }
  109.    
  110.     /**
  111.      * Get the length of this line.
  112.      * @return the length.
  113.      */
  114.     public int getLength() {
  115.         return (int) Math.sqrt(Math.pow(this.x2 - this.x1, 2.0) + Math.pow(this.y2 - this.y1, 2.0));
  116.     }
  117. }
  118.  
  119. // new constructor in GeometricShape2D
  120.     /**
  121.      * Create a new 2D shape whose x and y-coordinate will equal the minimum of the given GeometricShape f1 and f2's
  122.      * x and y-coordinates, respectively, and with the given color.
  123.      * and with the given color.
  124.      * @param f A GeometricShape containing the desired position of the 2D shape.
  125.      * @param c The color of the 2D shape.
  126.      */
  127.    
  128.     public GeometricShape2D(GeometricShape f1, GeometricShape f2, Color c) {
  129.         this.x = Math.min(f1.getX(), f2.getX());
  130.         this.y = Math.min(f1.getY(), f2.getY());
  131.         this.width = Math.abs(f1.getX()-f2.getX());
  132.         this.height = Math.abs(f1.getY()-f2.getY());
  133.         this.color = new Color(c.getRed(), c.getGreen(), c.getBlue());
  134.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement