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 rectangle defined by its position in two-dimensional space, width, height and color.
- * The position is defined by the position of the upper left corner of the rectangle.
- */
- public class Rectangle extends GeometricShape2D {
- private final int height;
- private final int width;
- public Rectangle(int x, int y, int width, int height, Color c) throws IllegalPositionException {
- super(x, y, c);
- this.height = height;
- this.width = width;
- }
- public Rectangle(GeometricShape f, int width, int height, Color c) {
- super(f, c);
- this.width = width;
- this.height = height;
- }
- @Override
- public int getArea() {
- return width*height;
- }
- @Override
- public int getWidth() {
- return 2*(width+height);
- }
- @Override
- public int getHeight() {
- return height;
- }
- @Override
- public int getPerimeter() {
- return width;
- }
- public void fill(Graphics g) {
- g.setColor(this.getColor());
- g.fillRect(this.getX(), this.getY(), this.width, this.height);
- }
- @Override
- public boolean equals(Object o) {
- if (!super.equals(o))
- return false;
- Rectangle r = (Rectangle) o;
- return this.width == r.width && this.height == r.height;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement