package grafikchat.model; import java.awt.Point; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * Implementation of a list of figures * * @author marian */ public class Figures implements Serializable { private ArrayList
figureList; private Figure lastFigure; private Point lastPoint; private Point secondToLastPoint; private static final long serialVersionUID = 8582433437601788990L; /** * Constructor, initialize variables */ public Figures() { figureList = new ArrayList<>(); lastFigure = null; lastPoint = null; secondToLastPoint = null; } /** * Add point to latest figure * @param p new point to add */ public void addPoint(Point p) { lastFigure.addPoint(p); secondToLastPoint = lastPoint; lastPoint = p; } /** * Create new figure */ public void newFigure() { Figure f = new Figure(); figureList.add(f); lastFigure = f; lastPoint = null; secondToLastPoint = null; } /** * Get the last two points of the latest figure * @return last two points of latest figure * @throws Exception if not yet available */ public List getLastTwoPoints() throws Exception { if (secondToLastPoint == null || lastPoint == null) { throw new Exception("Not enough points available"); } ArrayList p = new ArrayList<>(); p.add(lastPoint); p.add(secondToLastPoint); return p; } /** * Get latest figure * @return latest figure * @throws Exception if not yet available */ private List getFigure() throws Exception { if (lastFigure == null) { throw new Exception("No figure available"); } return lastFigure.getPoints(); } /** * Set new figures * @param f */ public void setFigures(ArrayList
f) { figureList = f; } /** * Get all current figures * @return */ public ArrayList
getFigures() { return figureList; } }