You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Box.java 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package figures;
  13. import java.awt.*;
  14. import java.awt.geom.*;
  15. public class Box extends ShapeFigureElement {
  16. private Point _p0;
  17. private Point _p1;
  18. private Point _p2;
  19. private Point _p3;
  20. public Box(int x0, int y0, int width, int height) {
  21. _p0 = new Point(x0, y0);
  22. _p1 = new Point(x0+width, y0);
  23. _p2 = new Point(x0+width, y0+height);
  24. _p3 = new Point(x0, y0+height);
  25. }
  26. public Point getP0() { return _p0; }
  27. public Point getP1() { return _p1; }
  28. public Point getP2() { return _p2; }
  29. public Point getP3() { return _p3; }
  30. public void move(int dx, int dy) {
  31. _p0.move(dx, dy);
  32. _p1.move(dx, dy);
  33. _p2.move(dx, dy);
  34. _p3.move(dx, dy);
  35. }
  36. public void checkBoxness() {
  37. if ((_p0.getX() == _p3.getX()) &&
  38. (_p1.getX() == _p2.getX()) &&
  39. (_p0.getY() == _p1.getY()) &&
  40. (_p2.getY() == _p3.getY()))
  41. return;
  42. throw new IllegalStateException("This is not a square.");
  43. }
  44. public String toString() {
  45. return "Box(" + _p0 + ", " + _p1 + ", " + _p2 + ", " + _p3 + ")";
  46. }
  47. public Shape getShape() {
  48. return new Rectangle(getP1().getX(),
  49. getP1().getY(),
  50. getP3().getX() - getP1().getX(),
  51. getP3().getY() - getP1().getY());
  52. }
  53. }