Quellcode durchsuchen

new Improved UnitGtaph

Kevin Trometer vor 8 Jahren
Ursprung
Commit
f7285e0173

+ 20 - 1
src/classes/HolonElement.java

@@ -1,5 +1,8 @@
 package classes;
 
+import java.awt.Point;
+import java.util.LinkedList;
+
 public class HolonElement {
 
 	/* Name of the gadget */
@@ -22,7 +25,9 @@ public class HolonElement {
 	 * Energy at each point of the graph with 50 predefined points. At the
 	 * beginning, it starts with all values at energy
 	 */
-	float[] energyAt = new float[49];
+	float[] energyAt = new float[50];
+	//Points on the UnitGraph
+	LinkedList<Point> graphPoints = new LinkedList<>();
 
 	public HolonElement(String eleName, float energy, int amount) {
 		setEleName(eleName);
@@ -174,5 +179,19 @@ public class HolonElement {
 	public void setStored(String stored) {
 		this.stored = stored;
 	}
+	
+	/**
+	 * @return the Graph Points
+	 */
+	public LinkedList<Point> getGraphPoints() {
+		return graphPoints;
+	}
+
+	/**
+	 * @param points, the Graph points
+	 */
+	public void setGraphPoints(LinkedList<Point> points) {
+		this.graphPoints = points;
+	}
 
 }

+ 3 - 0
src/ui/controller/Control.java

@@ -135,6 +135,9 @@ public class Control {
 		globalController.setScale(s);
 	}
 	
+	public void setCurIteration(int cur_it) {
+		globalController.setCurIteration(cur_it);
+	}
 	
 	/* Operations for Loading and Storing */
 	public void writeFile() throws IOException {

+ 9 - 0
src/ui/controller/GlobalController.java

@@ -36,5 +36,14 @@ public class GlobalController {
 	public void setScale(int s){
 		MODEL.setScale(s);
 	}
+	
+	/**
+	 * sets the current Iteration
+	 * 
+	 * @param cur_it, the current Iteration
+	 */
+	public void setCurIteration(int cur_it) {
+		MODEL.setCurIteration(cur_it);
+	}
 
 }

+ 19 - 1
src/ui/model/Model.java

@@ -19,7 +19,7 @@ public class Model {
 	private static int SCALE = 50; // Picture Scale
 	private static int SCALE_DIV2 = SCALE / 2;
 	private static final int ITERATIONS = 50;
-
+	private int CUR_ITERATION = 0;
 	// ID of the Selected Object
 	private CpsObject selectedCpsObject;
 	private HolonElement selectedHolonElement;
@@ -238,5 +238,23 @@ public class Model {
 	public int getIterations() {
 		return ITERATIONS;
 	}
+	
+	/**
+	 * sets the current Iteration
+	 * 
+	 * @param cur_it, the current Iteration
+	 */
+	public void setCurIteration(int cur_it) {
+		this.CUR_ITERATION = cur_it;
+	}
+
+	/**
+	 * Returns CUR_ITERATIONS
+	 * 
+	 * @return CUR_ITERATIONS
+	 */
+	public int getCurIteration() {
+		return CUR_ITERATION;
+	}
 
 }

+ 176 - 99
src/ui/view/UnitGraph.java

@@ -11,76 +11,55 @@ import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
 import java.awt.geom.CubicCurve2D;
+import java.util.LinkedList;
 import java.awt.Point;
 
+import javax.swing.JButton;
 import javax.swing.JPanel;
 
+import classes.HolonElement;
 import ui.controller.Control;
 import ui.model.Model;
-import java.awt.BorderLayout;
-import javax.swing.JButton;
-import java.awt.event.ActionListener;
-import java.awt.event.ActionEvent;
-import java.awt.FlowLayout;
-import javax.swing.SwingConstants;
 
-import classes.HolonElement;
-import javax.swing.JLabel;
 import java.awt.Cursor;
 
 class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
 
 	private static final long serialVersionUID = 1L;
-	private Control controller;
-	private Model model;
+	private int ITERATIONS = 50;
+	private int MAXIMUM = 1;
+
+	private Point recSize = new Point(8, 8); // Point Size
 	private Graphics2D g2;
 	private CubicCurve2D c = new CubicCurve2D.Double();
-	private Point[] pointList;
+	private LinkedList<Point> pointList;
+	private double scaleX;
+	private double scaleY;
+
+	private double width = -1;
+	private double height = -1;
+
+	private HolonElement tempElement;
+	private Model model;
+	private Control controller;
 
-	private HolonElement temp = null;
 	private boolean pointDrag = false;
-	private int tempP;
+	private boolean init = false;
+	private Point tempP = null;
+	private double x = 0, y = 0;
 	private int x1, x2, y1, y2, ctrlx1, ctrly1, ctrlx2, ctrly2;
-	// private final JButton resetButton = new JButton("Reset Graph");
-	// private final JLabel lblName = new JLabel("Name");
 
 	public UnitGraph(final Model model, Control control) {
 		setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
 		this.controller = control;
 		this.model = model;
-		this.pointList = new Point[model.getIterations()];
-		for (int i = 0; i < pointList.length; i++) {
-			pointList[i] = new Point(0, 0);
-		}
+		this.ITERATIONS = model.getIterations();
+
+		this.pointList = new LinkedList<>();
 
 		this.addMouseListener(this);
 		this.addMouseMotionListener(this);
 		this.addComponentListener(this);
-
-		// lblName.setText("None");
-		// resetButton.setToolTipText("Resets the Graph");
-		// resetButton.setBackground(Color.WHITE);
-		//
-		// resetButton.addActionListener(new ActionListener() {
-		// public void actionPerformed(ActionEvent e) {
-		// reset();
-		// }
-		// });
-		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
-
-		// add(lblName);
-		//
-		// add(resetButton);
-	}
-
-	/*
-	 * Resets the Graph
-	 */
-	public void reset() {
-		for (int i = 0; i < pointList.length; i++) {
-			pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), this.getHeight() / 3);
-		}
-		repaint();
 	}
 
 	/**
@@ -97,57 +76,81 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		g2.setRenderingHints(rh);
 		g2.setStroke(new BasicStroke(1));
 
+		// Draw the Vertical Lines
 		g2.setColor(new Color(240, 240, 240));
-		for (int i = 0; i < pointList.length; i++) {
-			g2.drawLine((int) pointList[i].getX(), 0, (int) pointList[i].getX(), this.getHeight());
+		for (int i = 0; i < ITERATIONS; i++) {
+			g2.drawLine((i) * this.getWidth() / (ITERATIONS - 1), MAXIMUM, (i) * this.getWidth() / (ITERATIONS - 1),
+					this.getHeight());
 		}
+		g2.drawLine(0, MAXIMUM, this.getWidth(), MAXIMUM);
 
+		// Draw the Lines
 		g2.setColor(Color.BLACK);
-		g2.setStroke(new BasicStroke(2));
-		for (int i = 0; i < pointList.length - 1; i++) {
-			x1 = (int) pointList[i].getX();
-			y1 = (int) pointList[i].getY();
-			x2 = (int) pointList[i + 1].getX();
-			y2 = (int) pointList[i + 1].getY();
-			ctrlx1 = (int) pointList[i].getX() + ((int) pointList[i + 1].getX() - (int) pointList[i].getX()) / 2;
-			ctrlx2 = (int) pointList[i + 1].getX() - ((int) pointList[i + 1].getX() - (int) pointList[i].getX()) / 2;
+		for (int i = 0; i < pointList.size() - 1; i++) {
+			/*
+			 * g2.drawLine((int) (pointList.get(i).getX() * scaleX), (int)
+			 * (pointList.get(i).getY() * scaleY), (int) (pointList.get(i +
+			 * 1).getX() * scaleX), (int) (pointList.get(i + 1).getY() *
+			 * scaleY));
+			 */
+			x1 = (int) pointList.get(i).getX();
+			y1 = (int) pointList.get(i).getY();
+			x2 = (int) pointList.get(i + 1).getX();
+			y2 = (int) pointList.get(i + 1).getY();
+			ctrlx1 = (int) pointList.get(i).getX()
+					+ ((int) pointList.get(i + 1).getX() - (int) pointList.get(i).getX()) / 2;
+			ctrlx2 = (int) pointList.get(i + 1).getX()
+					- ((int) pointList.get(i + 1).getX() - (int) pointList.get(i).getX()) / 2;
 			if (y1 < y2) {
-				ctrly1 = (int) pointList[i].getY() + ((int) pointList[i + 1].getY() - (int) pointList[i].getY()) / 10;
-				ctrly2 = (int) pointList[i + 1].getY()
-						- ((int) pointList[i + 1].getY() - (int) pointList[i].getY()) / 10;
+				ctrly1 = (int) pointList.get(i).getY()
+						+ ((int) pointList.get(i + 1).getY() - (int) pointList.get(i).getY()) / 10;
+				ctrly2 = (int) pointList.get(i + 1).getY()
+						- ((int) pointList.get(i + 1).getY() - (int) pointList.get(i).getY()) / 10;
 			} else {
-				ctrly1 = (int) pointList[i].getY() - ((int) pointList[i].getY() - (int) pointList[i + 1].getY()) / 10;
-				ctrly2 = (int) pointList[i + 1].getY()
-						+ ((int) pointList[i].getY() - (int) pointList[i + 1].getY()) / 10;
+				ctrly1 = (int) pointList.get(i).getY()
+						- ((int) pointList.get(i).getY() - (int) pointList.get(i + 1).getY()) / 10;
+				ctrly2 = (int) pointList.get(i + 1).getY()
+						+ ((int) pointList.get(i).getY() - (int) pointList.get(i + 1).getY()) / 10;
 			}
 
-			c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
-
-			// draw the curve
+			c.setCurve(x1 * scaleX, y1 * scaleY, ctrlx1 * scaleX, ctrly1 * scaleY, ctrlx2 * scaleX, ctrly2 * scaleY,
+					x2 * scaleX, y2 * scaleY);
 			g2.draw(c);
 		}
+
+		// Draw the Points
+		g2.setColor(Color.BLUE);
+		for (int i = 0; i < pointList.size() - 0; i++) {
+			g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
+					(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
+					(int) recSize.getY());
+
+		}
+
 	}
 
 	@Override
 	public void mouseDragged(MouseEvent e) {
-		if (temp != null) {
-			if (pointDrag && e.getY() >= this.getHeight() / 3 && tempP != -1) {
-				pointList[tempP].setLocation(pointList[tempP].getX(), e.getY());
-				if (tempP != 0 && pointList[tempP - 1].getY() < pointList[tempP].getY()
-						- (pointList[tempP].getY() - this.getHeight() / 3) / 2) {
-					pointList[tempP - 1].setLocation(pointList[tempP - 1].getX(),
-							pointList[tempP].getY() - (pointList[tempP].getY() - this.getHeight() / 3) / 2);
-				}
-				if (tempP != model.getIterations() - 1 && pointList[tempP + 1].getY() < pointList[tempP].getY()
-						- (pointList[tempP].getY() - this.getHeight() / 3) / 2) {
-					pointList[tempP + 1].setLocation(pointList[tempP + 1].getX(),
-							pointList[tempP].getY() - (pointList[tempP].getY() - this.getHeight() / 3) / 2);
-				}
-			} else if (tempP != -1) {
-				pointList[tempP].setLocation(pointList[tempP].getX(), this.getHeight() / 3);
+		if (pointDrag && tempP != null) {
+			// Out of Bounds verhindern
+			int i = pointList.indexOf(tempP);
+			x = e.getX() / scaleX;
+			y = e.getY() / scaleY;
+			// y
+			if (e.getY() <= MAXIMUM) {
+				y = MAXIMUM / scaleY;
+			} else if (this.getHeight() <= e.getY()) {
+				y = this.getHeight() / scaleY;
+			}
+			// x
+			if (tempP.getX() == 0 || tempP.getX() == this.getWidth() / scaleX || pointList.get(i + 1).getX() <= x
+					|| pointList.get(i - 1).getX() >= x) {
+				x = tempP.getX();
 			}
-			repaint();
+			tempP.setLocation(x, y);
 		}
+
+		repaint();
 	}
 
 	@Override
@@ -172,40 +175,74 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 	@Override
 	public void mousePressed(MouseEvent e) {
-		for (int i = 0; i < pointList.length; i++) {
-			if (e.getX() - this.getWidth() / model.getIterations() / 2 <= pointList[i].getX()
-					&& e.getX() + this.getWidth() / model.getIterations() / 2 >= pointList[i].getX()
-					&& e.getY() - 10 < pointList[i].getY() && e.getY() + 10 > pointList[i].getY()) {
-				tempP = i;
-				pointDrag = true;
+		boolean added = false;
+		boolean deletePoint = false;
+
+		double x = e.getX() / scaleX;
+		double y = e.getY() / scaleY;
+
+		// Click on Point
+		tempP = null;
+		for (Point p : pointList) {
+			if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
+					&& x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
+				if (e.getButton() == MouseEvent.BUTTON3) {
+					tempP = p;
+					deletePoint = true;
+				} else {
+					pointDrag = true;
+					tempP = p;
+				}
 			}
 		}
+		if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
+				&& e.getX() != this.getWidth() / scaleX) {
+			for (int i = 0; i < pointList.size(); i++) {
+				if (x < pointList.get(i).getX() && !added) {
+					if (e.getY() <= MAXIMUM) {
+						pointList.add(i, new Point((int) (x), (int) (MAXIMUM / scaleY)));
+					} else {
+						pointList.add(i, new Point((int) (x), (int) y));
+					}
+					added = true;
+					pointDrag = true;
+					tempP = pointList.get(i);
+				}
+			}
+		}
+		if (deletePoint && tempP.getX() != 0
+				&& (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
+			pointList.remove(tempP);
+		}
+
+		repaint();
 	}
 
 	@Override
 	public void mouseReleased(MouseEvent e) {
 		if (pointDrag) {
 			pointDrag = false;
-			tempP = -1;
+			tempP = null;
 		}
 	}
 
-	public void repaintWithNewElement(HolonElement ele) {
-		reset();
-		float[] arrayOfValue = ele.getEnergyAt();
-		temp = ele;
-		// TODO
-	}
-
-	// resize listener
 	public void componentResized(ComponentEvent e) {
-		if (pointList[0].getY() != 0) {
-			reset();
-		} else {
-			for (int i = 0; i < pointList.length; i++) {
-				pointList[i] = new Point((i) * this.getWidth() / (model.getIterations() - 1), this.getHeight() / 3);
+		if (init) {
+			MAXIMUM = (int) convertToCanvasY(MAXIMUM);
+			init = false;
+			// for scale
+			if (width == -1 && height == -1) {
+				width = this.getWidth();
+				height = this.getHeight();
+			}
+
+			if (pointList.isEmpty()) {
+				pointList.addFirst(new Point(0, MAXIMUM));
+				pointList.addLast(new Point(this.getWidth(), MAXIMUM));
 			}
 		}
+		scaleX = this.getWidth() / width;
+		scaleY = this.getHeight() / height;
 		repaint();
 	}
 
@@ -222,4 +259,44 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	public void componentShown(ComponentEvent e) {
 	}
 
+	/*
+	 * Resets the Graph
+	 */
+	public void reset() {
+		pointList.removeAll(pointList);
+		pointList.addFirst(new Point(0, (int) (MAXIMUM / scaleY)));
+		pointList.addLast(new Point((int) (this.getWidth() / scaleX), (int) (MAXIMUM / scaleY)));
+		repaint();
+	}
+
+	/**
+	 * converts the number to fit the canvas
+	 * 
+	 * @param int
+	 *            d, the number to convert
+	 * @return the convertet number
+	 */
+	public double convertToCanvasY(int d) {
+		if ((this.getHeight() - (((((double) this.getHeight() * 3) / 4) / MAXIMUM) * d)) <= 0) {
+			return 1;
+		} else {
+			return (this.getHeight() - (((((double) this.getHeight())) / MAXIMUM) * d));
+		}
+	}
+
+	/**
+	 * Viusalise the HolonElement on the Graph
+	 * 
+	 * @param HolonElement
+	 *            ele, which should be visualized
+	 */
+	public void repaintWithNewElement(HolonElement ele) {
+		float[] arrayOfValue = ele.getEnergyAt();
+		tempElement = ele;
+		pointList = ele.getGraphPoints();
+		init = true;
+		componentResized(null);
+		repaint();
+	}
+
 }