123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792 |
- package ui.view;
- import java.awt.BasicStroke;
- import java.awt.Color;
- import java.awt.Cursor;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.event.ComponentEvent;
- import java.awt.event.ComponentListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.awt.geom.Path2D;
- import java.awt.geom.Point2D;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import javax.swing.JPanel;
- import classes.UnitGraphPoint;
- import interfaces.GraphEditable;
- import interfaces.GraphEditable.Graphtype;
- import interfaces.LocalMode;
- import ui.controller.Control;
- import ui.model.Model;
- import utility.Vector2Int;
- /**
- * This Class represents a Graph where the User can model the behavior of
- * elements and switches over time.
- *
- * @author Tom Troppmann
- */
- public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
- private static final long serialVersionUID = 1L;
-
-
- // Normal Settings
- private int border = 4;
- private int clickThreshholdSquared = 25;
-
- // Display Settings
- /**
- * The size of a dot in the graph.
- * It should be at least 1.
- * */
- int dotSize = 8;
- /** The Color of a dot in the graph. */
- Color dotColor = Color.blue;
- Color editDotColor = new Color(255, 119, 0);
-
- //Intern Variables
- //TODO: JavaDoc
- private LinkedList<UnitGraphPoint> actualGraphPoints = new LinkedList<UnitGraphPoint>();
- private Graphtype actualGraphType;
- private GraphEditable actualElement;
- Vector2Int editPosition;
- boolean editMode = false;
- private enum pointType {Normal, StartPoint, EndPoint};
- pointType editPoint = pointType.Normal;
-
- //Maybe Needed
- private Model model;
-
-
- private int widthWithBorder, heightWithBorder;
-
-
-
-
-
-
- /**
- * Constructor.
- *
- * @param model the Model
- * @param control the Controller
- */
- public UnitGraph(final Model model, Control control) {
- setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- this.model = model;
- this.setBackground(Color.WHITE);
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- this.addComponentListener(this);
- }
- /**
- * When the UnitGraph should represent a new GraphEditable Element.
- * Its Updates the Graph and give access to the Element.
- * @param element
- */
- public void initNewElement(GraphEditable element)
- {
- overrideUnitGraph(element.getStateGraph());
- actualGraphType = element.getGraphType();
- actualElement = element;
- repaint();
- }
-
-
-
- /**
- * Paints the Graph, the Grid, the actual Line fro the currentIteration
- * @param g Graphics
- */
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2D = (Graphics2D) g;
- drawGrid(g2D);
- g2D.setColor(Color.BLACK);
- g2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
- g2D.setStroke(new BasicStroke(2));
- drawUnitGraph(g2D);
- g2D.setColor(dotColor);
- if(editMode)
- {
- drawUnitGraphPointsReleased(g2D);
- }else
- {
- drawUnitGraphPoints(g2D);
- }
- g2D.setColor(dotColor);
- g2D.setStroke(new BasicStroke(1));
- drawCurrentIterartionLine(g2D);
- }
- // Draw Methods only to let the User see the changes. Nothing its saved here or changed.
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Methods draws the UnitGraph whether its a boolGraph or a doubleGraph.
- * @param g to draw.
- */
- private void drawUnitGraph(Graphics2D g) {
- switch(actualGraphType) {
- case boolGraph:
- if(editMode)
- drawBoolGraphInEditMode(g);
- else
- drawBoolGraph(g);
- break;
- case doubleGraph:
- if(editMode)
- drawDoubleGraphInEditMode(g);
- else
- drawDoubleGraph(g);
- break;
- default:
- throw new UnsupportedOperationException();
- }
- }
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Methods draws the UnitGraphPoints of the UnitGraph.
- * @param g to draw.
- */
- private void drawUnitGraphPoints(Graphics2D g) {
- g.setColor(dotColor);
- for(UnitGraphPoint p : actualGraphPoints){
- drawDot(g, p.displayedPosition);
- }
- }
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Methods draws the UnitGraphPoints of the UnitGraph when its in EditMode.
- * @param g to draw.
- */
- private void drawUnitGraphPointsReleased(Graphics2D g) {
- drawUnitGraphPoints(g);
- g.setColor(editDotColor);
- drawDot(g, editPosition);
- }
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Methods draws the Grid on the Canvas.
- * @param g2D to draw.
- */
- private void drawGrid(Graphics2D g2D) {
- g2D.setStroke(new BasicStroke(1));
- g2D.setColor(Color.lightGray);
- int amountOfLines = 10;
- int width = widthWithBorder + 2 * border;
- int height = heightWithBorder;
- for(int i = 0; i<=amountOfLines; i++)
- {
- int linehieght = (int) (((double)i/ (double) amountOfLines) * (double) height) + border;
- g2D.drawLine(0, linehieght, width, linehieght);
- }
- }
-
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws the CurrentIterationLine.
- * @param g2D to draw.
- */
- private void drawCurrentIterartionLine(Graphics2D g)
- {
- int cur = model.getCurIteration();
- int max = model.getIterations();
- double where;
- if(!this.isUsingLocalPeriod()) {
- where = ((double) cur)/((double) max);
- }
- else
- {
- int lPeriod = this.getLocalPeriod();
- where = ((double) cur%lPeriod)/((double) lPeriod);
- }
- Vector2Int oben = new Vector2Int(border + (int)(where * widthWithBorder), 0);
- Vector2Int unten = new Vector2Int(border + (int)(where * widthWithBorder), 2 * border + heightWithBorder);
- drawLine(g,oben,unten);
-
- }
-
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws a line between two Positions on the Canvas.
- * @param g2D to draw.
- * @param start the Position of one end of the line to draw.
- * @param end the other Ends Position of the Line to draw.
- */
- private void drawLine(Graphics2D g, Vector2Int start, Vector2Int end)
- {
- Path2D.Double path = new Path2D.Double();
- path.moveTo(start.getX(), start.getY());
- path.lineTo(end.getX(), end.getY());
- g.draw(path);
- }
-
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * Initialize a Cubic BezierCurve.
- * @param start The Position to start the Curve.
- */
- private Path2D.Double initBezier(Vector2Int start) {
- //Good Source for basic understanding for Bezier Curves
- //http://www.theappguruz.com/blog/bezier-curve-in-games
- Path2D.Double path = new Path2D.Double();
- path.moveTo(start.getX(), start.getY());
- return path;
- }
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * Calculate the Path of a the Cubic BezierCurve with the special controlPoints to make the wanted behavior.
- * @param path the path of the Bezier.
- * @param actaul the actual Position of the Path.
- * @param target the end Position of the Curve.
- */
- private void curveTo(Path2D.Double path, Vector2Int actual, Vector2Int target) {
- double mitte = (actual.getX() + target.getX())* 0.5;
- path.curveTo(mitte, actual.getY(), mitte, target.getY(), target.getX(), target.getY());
- }
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * Draws a Dot at a Position.
- * @param g to draw.
- * @param p the position of the Dot.
- */
- private void drawDot(Graphics2D g, Vector2Int p)
- {
- g.fillOval(p.getX() -dotSize/2, p.getY()-dotSize/2, dotSize, dotSize);
- }
-
-
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws the UnitGraph as BoolGraph.
- * @param g2D to draw.
- */
- private void drawBoolGraph(Graphics2D g) {
- if(actualGraphPoints.size() <= 1) return;
- LinkedList<Vector2Int> cornerPoints = new LinkedList<Vector2Int>();
- ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
- Vector2Int actual = actualGraphPoints.getFirst().displayedPosition;
- Path2D.Double path = new Path2D.Double();
- path.moveTo(actual.getX(), actual.getY());
- while (iter.hasNext())
- {
- Vector2Int target = iter.next().displayedPosition;
- //BooleanConnection
- path.lineTo(target.getX(), actual.getY()); //line to corner
- cornerPoints.add(new Vector2Int(target.getX(), actual.getY())); //save corner
- path.lineTo(target.getX(), target.getY()); //line to next Point
-
- actual = target;
- }
- g.draw(path);
- //Draw the Points on the Corner that dont exist in Data but should be visual
- g.setColor(dotColor);
- for(Vector2Int p: cornerPoints)
- {
- drawDot(g, p);
- }
-
- }
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws the UnitGraph as BoolGraph in EditMode.
- * @param g2D to draw.
- */
- private void drawBoolGraphInEditMode(Graphics2D g) {
- LinkedList<Vector2Int> before = new LinkedList<Vector2Int>();
- LinkedList<Vector2Int> after = new LinkedList<Vector2Int>();
- for(UnitGraphPoint p: actualGraphPoints)
- {
- if(p.displayedPosition.getX() < editPosition.getX())
- before.add(p.displayedPosition);
- else
- after.add(p.displayedPosition);
- }
- g.setColor(Color.BLACK);
- drawBoolGraphFromList(g, before);
- g.setColor(Color.BLACK);
- drawBoolGraphFromList(g, after);
- //EditGraph
- LinkedList<Vector2Int> middle = new LinkedList<Vector2Int>();
- if(!before.isEmpty()) middle.add(before.getLast());
- middle.add(editPosition);
- if(!after.isEmpty()) middle.add(after.getFirst());
-
- g.setColor(editDotColor);
- drawBoolGraphFromList(g, middle);
- drawSnappingHint(g);
- }
-
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws a red Hint to signal the User the snapping of the hovered Point under the Cursor in EditMode.
- * @param g2D to draw.
- */
- private void drawSnappingHint(Graphics2D g)
- {
- //ColorHint
- g.setColor(Color.RED);
- //Threshhold Line
- final float dash1[] = {10.0f};
- final BasicStroke dashed =new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
- g.setStroke(dashed);
-
-
- int halfheight = border + heightWithBorder / 2;
- g.drawLine(0, halfheight , widthWithBorder + 2 * border, halfheight);
- //Threshhold Text
- g.drawString("Snapping Threshold", 10, halfheight - 2);
- }
-
-
-
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws a partial Graph from a Position List as BoolGraph.
- * @param g2D to draw.
- * @param list the PositionList to draw a BoolGraph
- */
- private void drawBoolGraphFromList(Graphics2D g, LinkedList<Vector2Int> list) {
- if(list.size() <= 1) return;
- ListIterator<Vector2Int> iter = list.listIterator();
- LinkedList<Vector2Int> cornerPoints = new LinkedList<Vector2Int>();
- Vector2Int actual = list.getFirst();
- Path2D.Double path = new Path2D.Double();
- path.moveTo(actual.getX(), actual.getY());
- while (iter.hasNext())
- {
- Vector2Int target = iter.next();
- //BooleanConnection
- path.lineTo(target.getX(), actual.getY()); //line to corner
- cornerPoints.add(new Vector2Int(target.getX(), actual.getY())); //save corner
- path.lineTo(target.getX(), target.getY()); //line to next Point
- actual = target;
- }
- g.draw(path);
- g.setColor(dotColor);
- for(Vector2Int p: cornerPoints)
- {
- drawDot(g, p);
- }
- }
-
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws the UnitGraph as DoubleGraph.
- * @param g2D to draw.
- */
- private void drawDoubleGraph(Graphics2D g) {
- if(actualGraphPoints.isEmpty()) throw new IndexOutOfBoundsException("A Graph Without Points is not supportet jet");
- ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
- Vector2Int actual = iter.next().displayedPosition;
- Path2D.Double path = this.initBezier(actual);
- while (iter.hasNext())
- {
- Vector2Int target = iter.next().displayedPosition;
- this.curveTo(path, actual, target);
- actual = target;
- }
- g.draw(path);
-
- }
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws the UnitGraph as DoubleGraph in EditMode.
- * @param g2D to draw.
- */
- private void drawDoubleGraphInEditMode(Graphics2D g) {
- LinkedList<Vector2Int> before = new LinkedList<Vector2Int>();
- LinkedList<Vector2Int> after = new LinkedList<Vector2Int>();
- for(UnitGraphPoint p: actualGraphPoints)
- {
- if(p.displayedPosition.getX() < editPosition.getX())
- before.add(p.displayedPosition);
- else
- after.add(p.displayedPosition);
- }
- drawUnitGraphFromList(g, before);
- drawUnitGraphFromList(g, after);
- //EditGraph
- LinkedList<Vector2Int> middle = new LinkedList<Vector2Int>();
- if(!before.isEmpty()) middle.add(before.getLast());
- middle.add(editPosition);
- if(!after.isEmpty()) middle.add(after.getFirst());
-
- g.setColor(editDotColor);
- drawUnitGraphFromList(g, middle);
- }
-
- /**
- * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
- * <p>
- * This Method draws a partial Graph from a Position List as DoubleGraph.
- * @param g2D to draw.
- * @param list the PositionList to draw a DoubleGraph
- */
- private void drawUnitGraphFromList(Graphics2D g, LinkedList<Vector2Int> list) {
- if(list.size() <= 1) return;
- ListIterator<Vector2Int> iter = list.listIterator();
- Vector2Int actual = list.getFirst();
- Path2D.Double path = this.initBezier(actual);
- while (iter.hasNext())
- {
- Vector2Int target = iter.next();
- curveTo(path, actual, target);
- actual = target;
- }
- g.draw(path);
- }
-
- //Under the hood functions to calculate and function the
- /**
- * A unitgraphpoint have a x and y position to store the data of a graph point.
- * Also it have a displayposition to store the Position of the GraphPoints on the Canvas.
- * e.g. when the canvas has 500 width and 200 height a GraphPoint with the X=0.5 and Y=1.0 should have a displayposition of (250,3) when border is 3.
- */
- private void updateRepresentativePositions()
- {
- for(UnitGraphPoint p : actualGraphPoints) {
- p.calcDisplayedPosition(border, widthWithBorder, heightWithBorder);
- }
- }
- /**
- * Takes a List of GraphPoints and convert it to the actual UnitGraphPoints with displayposition in the {@link #actualGraphPoints}
- * @param stateCurve the list of GraphPoint
- */
- private void overrideUnitGraph(LinkedList<Point2D.Double> stateCurve) {
- actualGraphPoints.clear();
- for(Point2D.Double p: stateCurve){
- actualGraphPoints.add(new UnitGraphPoint(p));
- }
- updateRepresentativePositions();
- }
-
- /**
- * When the PanelSize Change the width and height to calculate the drawings have to be adjusted.
- */
- private void calculateWidthHeight()
- {
- widthWithBorder = this.getWidth() - 2 * border;
- heightWithBorder = this.getHeight() - 2 * border;
- }
-
- /**
- * Save the actualGraphPoint List to the GraphEditable Element.
- */
- private void saveGraph() {
- LinkedList<Point2D.Double> actual = actualElement.getStateGraph();
- actual.clear();
- for(UnitGraphPoint p: actualGraphPoints)
- {
- actual.add(p.getPoint());
- }
- actualElement.sampleGraph();
- }
- /**
- * Remove a UnitGraphPoint from the UnitGraphPoint list ({@link #actualGraphPoints} when its near a given Position.
- * @param mPosition
- */
- private void removePointNearPosition(Vector2Int mPosition) {
- ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
- while (iter.hasNext())
- {
- if(near(mPosition,iter.next().displayedPosition))
- {
- iter.remove();
- break;
- }
- }
- }
-
-
- /**
- * Determine if the Point is a StartPoint , EndPoint or a NormalPoint a.k.a. in between Points.
- * @param mPosition The Position to check.
- */
- private void detectStartEndPoint(Vector2Int mPosition)
- {
- UnitGraphPoint first = actualGraphPoints.getFirst();
- UnitGraphPoint last = actualGraphPoints.getLast();
- if(near(mPosition, first.displayedPosition)) editPoint = pointType.StartPoint;
- else if(near(mPosition, last.displayedPosition)) editPoint = pointType.EndPoint;
- else editPoint = pointType.Normal;
- }
- /**
- * Determine if a Point is near the Cursor (depends on Mode what near means). To detect if it should grab the Point or create a new Point.
- * @param actual
- * @param target
- * @return
- */
- private boolean near(Vector2Int actual, Vector2Int target) {
- switch(actualGraphType)
- {
- case boolGraph: //Distance only with X
- int xDis = target.getX() - actual.getX();
- return xDis * xDis < clickThreshholdSquared;
- case doubleGraph:
- return actual.getSquaredDistance(target) < clickThreshholdSquared;
- default:
- return false;
- }
- }
-
- /**
- * When the Mouse Drag a Point it updates each time the position.
- * @param newPosition
- */
- private void updateEditPointPosition(Vector2Int newPosition) {
- //make it in the bounds of the UnitGraph no Point out of the Border
- Vector2Int currentPosition = setInBounds(newPosition);
- if(editPoint != pointType.Normal) attachToBorder(currentPosition);
- if(actualGraphType == Graphtype.boolGraph) snapBoolean(currentPosition);
- this.editPosition = currentPosition;
- }
-
-
- /**
- * No Point on the UnitGraph should exit the UnitGraph.
- * @param p the Position
- * @return the updated Position.
- */
- private Vector2Int setInBounds(Vector2Int p) {
- p.clampX(border, border + widthWithBorder);
- p.clampY(border, border + heightWithBorder);
- return p;
- }
-
-
-
- /**
- * For Switches the Point have to be Snap to the Top or the Bottem.
- * @param p the Position
- * @return the updated Position.
- */
- private Vector2Int snapBoolean(Vector2Int p)
- {
- if (p.getY() < border + heightWithBorder / 2) {
- p.setY(border);
- } else {
- p.setY(border + heightWithBorder);
- }
- return p;
- }
-
-
- /**
- * The First Point has to be at 0(LeftSide) and Last Point has to be at 1(RightSide).
- * @param p the Position
- * @return the updated Position.
- */
- private Vector2Int attachToBorder(Vector2Int p)
- {
- switch(editPoint)
- {
- case StartPoint:
- p.setX(border);
- break;
- case EndPoint:
- p.setX(border + widthWithBorder);
- break;
- default:
- break;
- }
- return p;
- }
- /**
- * Insert a Position in the UnitGraphList at the right order.
- * Its sorted based on the xValues.
- * @param pos The new UnitGraphPoints Position
- */
- private void insertNewGraphPoint(Vector2Int pos)
- {
- setInBounds(pos);
- ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
- while (iter2.hasNext())
- {
- Vector2Int tempPosition = iter2.next().displayedPosition;
- if(pos.getX() <= tempPosition.getX())
- {
- //previous to go back a position to make the new point before the the Position with greater X
- iter2.previous();
- iter2.add(generateUnitGraphPoint(pos));
- break;
- }
- }
- if(!iter2.hasNext()) //if behind last point
- {
- iter2.add(generateUnitGraphPoint(pos));
- }
- }
- /**
- * Generate a UnitGraphPoint from a normal Position in the UnitGraph.
- * @param pos the normal pos with xValues from 0..Width and yValues from 0..Height
- * @return a UnitGraphPoint
- */
- private UnitGraphPoint generateUnitGraphPoint(Vector2Int pos) {
- UnitGraphPoint temp = new UnitGraphPoint((double) (pos.getX() - border) / (double) widthWithBorder,
- 1 - (double) (pos.getY() - border) / (double) heightWithBorder, true);
- temp.displayedPosition = pos;
- return temp;
- }
- /**
- * Update the Point Position
- */
- @Override
- public void mouseDragged(MouseEvent e) {
- updateEditPointPosition(new Vector2Int(e.getPoint().x, e.getPoint().y));
- repaint();
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- /**
- * The First Step.
- * When LeftMouseButton its checks if a point is to grab under the cursor or create a new Point. Then enter EditMode.
- * When RightMouseButton its delete a point if its under the Curser.
- */
- @Override
- public void mousePressed(MouseEvent e) {
- Vector2Int mPosition = new Vector2Int(e.getPoint().x, e.getPoint().y);
- if (e.getButton() == MouseEvent.BUTTON3) {
- // RightMouseButtonEvent
- detectStartEndPoint(mPosition);
- if (editPoint == pointType.Normal) {
- removePointNearPosition(mPosition);
- repaint();
- }
- editMode = false;
- } else if (e.getButton() == MouseEvent.BUTTON1) {
- // LeftMouseButtonEvent
- detectStartEndPoint(mPosition);
- removePointNearPosition(mPosition);
- updateEditPointPosition(mPosition);
- editMode = true;
- repaint();
- }
- }
- /**
- * The last step to save the Changes.
- * Its insert the Hovering Point and exit EditMode.
- */
- @Override
- public void mouseReleased(MouseEvent e) {
- if(editMode)
- {
- this.insertNewGraphPoint(editPosition);
- editMode = false;
- repaint();
- }
- saveGraph();
- }
- /**
- * When the Component is Resized.
- *
- * @param e ComponentEvent
- */
- public void componentResized(ComponentEvent e) {
- calculateWidthHeight();
- updateRepresentativePositions();
- repaint();
- }
- @Override
- public void componentHidden(ComponentEvent e) {
- }
- @Override
- public void componentMoved(ComponentEvent e) {
- }
- @Override
- public void componentShown(ComponentEvent e) {
- }
- /**
- * Resets the graph to normal.
- */
- public void reset() {
- if(this.actualElement != null) {
- actualElement.reset();
- overrideUnitGraph(actualElement.getStateGraph());
- repaint();
- }
-
- }
-
- //LocalMode access methods...
- //To access a element from the GUI for the LocalMode
- public void setUseLocalPeriod(boolean state) {
- if(this.actualElement != null) {
- ((LocalMode) actualElement).setUseLocalPeriod(state);
- }
- }
- public void setLocalPeriod(int localLength) {
- if(this.actualElement != null) {
- ((LocalMode) actualElement).setLocalPeriod(localLength);
- }
- }
-
- public int getLocalPeriod() {
- if(this.actualElement != null) {
- return ((LocalMode) actualElement).getLocalPeriod();
- }
- return -1;
- }
-
- public boolean isUsingLocalPeriod() {
- if(this.actualElement != null) {
- return ((LocalMode) actualElement).isUsingLocalPeriod();
- }
- return false;
- }
-
-
- }
|