Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package geometri;
- import java.awt.Color;
- import java.awt.Graphics;
- /**
- * A drawable line defined by its starting and ending position in two-dimensional space, and by its color.
- * The starting position is defined by the position of the upper left corner of the smallest rectangle
- * that covers the entire line as neatly as possible.
- */
- public class Line extends GeometricShape2D {
- /** The x-coordinate of the starting point of this line. */
- private int x1;
- /** The y-coordinate of the starting point of this line. */
- private int y1;
- /** The x-coordinate of the endpoint of this line. */
- private int x2;
- /** The y-coordinate of the endpoint of this line. */
- private int y2;
- /**
- * Create a new line with the given starting point (x1, y1), endpoint (x2, y2) and color c.
- * @param x1 The x-coordinate of the starting point of the line.
- * @param y1 The y-coordinate of the starting point of the line.
- * @param x2 The x-coordinate of the endpoint of the line.
- * @param y2 The y-coordinate of the endpoint of the line.
- * @param c The color of the line.
- * @throws IllegalPositionException if x1, y1, x2 or y2 is negative.
- */
- public Line(int x1, int y1, int x2, int y2, Color c) throws IllegalPositionException {
- super(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2), c);
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- }
- /**
- * Create a new line with starting point as given by f1, endpoint as given by f2 and color c.
- * @param f1 The GeometricShape with the desired starting point of the line.
- * @param f2 The GeometricShape with the desired endpoint of the line.
- * @param c The color of the line.
- */
- public Line(GeometricShape f1, GeometricShape f2, Color c) {
- super(f1, f2, c);
- this.x1 = f1.getX();
- this.y1 = f1.getY();
- this.x2 = f2.getX();
- this.y2 = f2.getY();
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int getArea() {
- return 0;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void fill(Graphics g) {
- g.setColor(this.getColor());
- g.drawLine(this.x1, this.y1, this.x2, this.y2);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void move(int dx, int dy) throws IllegalPositionException {
- super.move(dx, dy);
- this.x1 += dx;
- this.y1 += dy;
- this.x2 += dx;
- this.y2 += dy;
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public int getPerimeter() {
- double length = Math.sqrt(Math.pow(this.x2 - this.x1, 2.0) + Math.pow(this.y2 - this.y1, 2.0));
- return (int) (length*2.0);
- }
- /**
- * {@inheritDoc}
- */
- @Override
- public void place(int x, int y) throws IllegalPositionException {
- super.place(x, y);
- int dx = x - Math.min(this.x1, this.x2);
- int dy = y - Math.min(this.y1, this.y2);
- this.x1 += dx;
- this.y1 += dy;
- this.x2 += dx;
- this.y2 += dy;
- }
- /**
- * Get the length of this line.
- * @return the length.
- */
- public int getLength() {
- return (int) Math.sqrt(Math.pow(this.x2 - this.x1, 2.0) + Math.pow(this.y2 - this.y1, 2.0));
- }
- }
- // new constructor in GeometricShape2D
- /**
- * Create a new 2D shape whose x and y-coordinate will equal the minimum of the given GeometricShape f1 and f2's
- * x and y-coordinates, respectively, and with the given color.
- * and with the given color.
- * @param f A GeometricShape containing the desired position of the 2D shape.
- * @param c The color of the 2D shape.
- */
- public GeometricShape2D(GeometricShape f1, GeometricShape f2, Color c) {
- this.x = Math.min(f1.getX(), f2.getX());
- this.y = Math.min(f1.getY(), f2.getY());
- this.width = Math.abs(f1.getX()-f2.getX());
- this.height = Math.abs(f1.getY()-f2.getY());
- this.color = new Color(c.getRed(), c.getGreen(), c.getBlue());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement