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.7KB

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 Line extends ShapeFigureElement {
  16. private Point _p1;
  17. private Point _p2;
  18. public Line(Point p1, Point p2) {
  19. _p1 = p1;
  20. _p2 = p2;
  21. }
  22. public Point getP1() { return _p1; }
  23. public Point getP2() { return _p2; }
  24. public void move(int dx, int dy) {
  25. _p1.move(dx, dy);
  26. _p2.move(dx, dy);
  27. }
  28. public String toString() {
  29. return "Line(" + _p1 + ", " + _p2 + ")";
  30. }
  31. /**
  32. * Used to determine if this line {@link contains(Point2D)} a point.
  33. */
  34. final static int THRESHHOLD = 5;
  35. /**
  36. * Returns <code>true</code> if the point segment distance is less than
  37. * {@link THRESHHOLD}.
  38. */
  39. public boolean contains(Point2D p) {
  40. return getLine2D().ptLineDist(p) < THRESHHOLD;
  41. }
  42. private Line2D getLine2D() {
  43. return new Line2D.Float((float)getP1().getX(),
  44. (float)getP1().getY(),
  45. (float)getP2().getX(),
  46. (float)getP2().getY());
  47. }
  48. public Shape getShape() {
  49. return getLine2D();
  50. }
  51. }