Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Line.java 1.4KB

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