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.

Line.java 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (c) 2002 Palo Alto Research Center, Incorporated.
  2. // All Rights Reserved.
  3. package figures;
  4. import java.awt.*;
  5. import java.awt.geom.*;
  6. public class Line extends ShapeFigureElement {
  7. private Point _p1;
  8. private Point _p2;
  9. public Line(Point p1, Point p2) {
  10. _p1 = p1;
  11. _p2 = p2;
  12. }
  13. public Point getP1() { return _p1; }
  14. public Point getP2() { return _p2; }
  15. public void move(int dx, int dy) {
  16. _p1.move(dx, dy);
  17. }
  18. public void move() {
  19. _p1.move();
  20. // _p2.move(dx, dy);
  21. }
  22. public String toString() {
  23. return "Line(" + _p1 + ", " + _p2 + ")";
  24. }
  25. /**
  26. * Used to determine if this line {@link contains(Point2D)} a point.
  27. */
  28. final static int THRESHHOLD = 5;
  29. /**
  30. * Returns <code>true</code> if the point segment distance is less than
  31. * {@link THRESHHOLD}.
  32. */
  33. public boolean contains(Point2D p) {
  34. return getLine2D().ptLineDist(p) < THRESHHOLD;
  35. }
  36. private Line2D getLine2D() {
  37. return new Line2D.Float((float)getP1().getX(),
  38. (float)getP1().getY(),
  39. (float)getP2().getX(),
  40. (float)getP2().getY());
  41. }
  42. public Shape getShape() {
  43. return getLine2D();
  44. }
  45. }