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.

Figures.java 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package grafikchat.model;
  2. import java.awt.Point;
  3. import java.io.Serializable;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. /**
  7. * Implementation of a list of figures
  8. *
  9. * @author marian
  10. */
  11. public class Figures implements Serializable
  12. {
  13. private ArrayList<Figure> figureList;
  14. private Figure lastFigure;
  15. private Point lastPoint;
  16. private Point secondToLastPoint;
  17. private static final long serialVersionUID = 8582433437601788990L;
  18. /**
  19. * Constructor, initialize variables
  20. */
  21. public Figures()
  22. {
  23. figureList = new ArrayList<>();
  24. lastFigure = null;
  25. lastPoint = null;
  26. secondToLastPoint = null;
  27. }
  28. /**
  29. * Add point to latest figure
  30. * @param p new point to add
  31. */
  32. public void addPoint(Point p)
  33. {
  34. lastFigure.addPoint(p);
  35. secondToLastPoint = lastPoint;
  36. lastPoint = p;
  37. }
  38. /**
  39. * Create new figure
  40. */
  41. public void newFigure()
  42. {
  43. Figure f = new Figure();
  44. figureList.add(f);
  45. lastFigure = f;
  46. lastPoint = null;
  47. secondToLastPoint = null;
  48. }
  49. /**
  50. * Get the last two points of the latest figure
  51. * @return last two points of latest figure
  52. * @throws Exception if not yet available
  53. */
  54. public List<Point> getLastTwoPoints() throws Exception
  55. {
  56. if (secondToLastPoint == null || lastPoint == null) {
  57. throw new Exception("Not enough points available");
  58. }
  59. ArrayList<Point> p = new ArrayList<>();
  60. p.add(lastPoint);
  61. p.add(secondToLastPoint);
  62. return p;
  63. }
  64. /**
  65. * Get latest figure
  66. * @return latest figure
  67. * @throws Exception if not yet available
  68. */
  69. private List<Point> getFigure() throws Exception
  70. {
  71. if (lastFigure == null) {
  72. throw new Exception("No figure available");
  73. }
  74. return lastFigure.getPoints();
  75. }
  76. /**
  77. * Set new figures
  78. * @param f
  79. */
  80. public void setFigures(ArrayList<Figure> f)
  81. {
  82. figureList = f;
  83. }
  84. /**
  85. * Get all current figures
  86. * @return
  87. */
  88. public ArrayList<Figure> getFigures()
  89. {
  90. return figureList;
  91. }
  92. }