Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. package geometri;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5.  
  6. /**
  7. * A drawable rectangle defined by its position in two-dimensional space, width, height and color.
  8. * The position is defined by the position of the upper left corner of the rectangle.
  9. */
  10.  
  11. public class Rectangle extends GeometricShape2D {
  12.  
  13. private final int height;
  14.  
  15. private final int width;
  16.  
  17.  
  18.  
  19. public Rectangle(int x, int y, int width, int height, Color c) throws IllegalPositionException {
  20. super(x, y, c);
  21. this.height = height;
  22. this.width = width;
  23. }
  24.  
  25.  
  26. public Rectangle(GeometricShape f, int width, int height, Color c) {
  27. super(f, c);
  28. this.width = width;
  29. this.height = height;
  30. }
  31.  
  32. @Override
  33. public int getArea() {
  34.  
  35. return width*height;
  36. }
  37.  
  38. @Override
  39. public int getWidth() {
  40.  
  41. return 2*(width+height);
  42. }
  43.  
  44. @Override
  45. public int getHeight() {
  46.  
  47. return height;
  48. }
  49.  
  50. @Override
  51. public int getPerimeter() {
  52.  
  53. return width;
  54. }
  55.  
  56. public void fill(Graphics g) {
  57. g.setColor(this.getColor());
  58. g.fillRect(this.getX(), this.getY(), this.width, this.height);
  59. }
  60.  
  61. @Override
  62. public boolean equals(Object o) {
  63. if (!super.equals(o))
  64. return false;
  65.  
  66. Rectangle r = (Rectangle) o;
  67. return this.width == r.width && this.height == r.height;
  68. }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement