Jelajahi Sumber

Merge branch 'Ohne_Drag_and_Drop' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons into Ohne_Drag_and_Drop

Edgardo Palza 8 tahun lalu
induk
melakukan
9f63597802

+ 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;
+	}
 
 }

+ 19 - 9
src/ui/view/GUI.java

@@ -8,6 +8,7 @@ import javax.swing.JLabel;
 import javax.swing.JMenuBar;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
+import javax.swing.JSlider;
 import javax.swing.JOptionPane;
 import javax.swing.JTree;
 import javax.swing.border.LineBorder;
@@ -157,12 +158,12 @@ public class GUI implements CategoryListener {
 	private int yValueElements = 0;
 
 	private MyCanvas canvas;
-	private UnitGraph testgraph; // for testing, remove later
+	private UnitGraph unitGraph; // for testing, remove later
 	private final JSplitPane splitPane_3 = new JSplitPane();
 	private final JSlider sizeSlider = new JSlider();
 	private final JLabel lblImageSize = new JLabel("Image Size");
 	// Time Stuff
-	private final TimePanel timePanel;
+	private TimePanel timePanel;
 
 	/**
 	 * Create the application.
@@ -171,10 +172,9 @@ public class GUI implements CategoryListener {
 		this.controller = control;
 		this.model = control.getModel();
 		this.canvas = new MyCanvas(model, control);
-		this.testgraph = new UnitGraph(model, control); // for testing, remove
+		this.unitGraph = new UnitGraph(model, control); // for testing, remove
 
-		timePanel = new TimePanel(model, controller);
-		timePanel.setBorder(null);
+		
 
 		// later
 		control.initListener(this);
@@ -232,7 +232,7 @@ public class GUI implements CategoryListener {
 
 		mnHelp.add(aboutUs);
 
-		testgraph.setBackground(Color.WHITE);
+		unitGraph.setBackground(Color.WHITE);
 
 		canvas.setBackground(Color.WHITE);
 		canvas.setPreferredSize(new Dimension(10000, 10000));
@@ -285,7 +285,7 @@ public class GUI implements CategoryListener {
 		 */
 		scrollProperties.setViewportView(tableProperties);
 		tableHolonElementScrollPane.setViewportView(tableHolonElement);
-		scrollGraph.setViewportView(testgraph);
+		scrollGraph.setViewportView(unitGraph);
 		graphLabel.setLayout(new BorderLayout(0, 10));
 		graphLabel.add(maxGraph, BorderLayout.NORTH);
 		graphLabel.add(medGraph, BorderLayout.CENTER);
@@ -323,7 +323,7 @@ public class GUI implements CategoryListener {
 		resetGraphBtn.addActionListener(new ActionListener() {
 
 			public void actionPerformed(ActionEvent arg0) {
-				testgraph.reset();
+				unitGraph.reset();
 			}
 		});
 		toolBarHolonEl.add(btnDelHolEL);
@@ -347,7 +347,7 @@ public class GUI implements CategoryListener {
 				HolonElement ele = getActualHolonElement(obj, yValueElements);
 				if (ele != null) {
 					elementGraph.setText(ele.getEleName() + " ");
-					testgraph.repaintWithNewElement(ele);
+					unitGraph.repaintWithNewElement(ele);
 				} else {
 					elementGraph.setText("None ");
 				}
@@ -702,6 +702,16 @@ public class GUI implements CategoryListener {
 			}
 		});
 
+		timePanel = new TimePanel(model, controller);
+		timePanel.setBorder(null);
+		((JSlider)(timePanel.getComponent(1))).addChangeListener(new ChangeListener() {
+			
+			@Override
+			public void stateChanged(ChangeEvent e) {
+				unitGraph.repaint();
+			}
+		});
+		
 		splitPane.setRightComponent(splitPane_1);
 		splitPane.setDividerLocation(200);
 		splitPane_1.setDividerLocation(500);

+ 14 - 2
src/ui/view/TimePanel.java

@@ -13,6 +13,8 @@ import javax.swing.JPanel;
 import javax.swing.JSlider;
 import javax.swing.Timer;
 import javax.swing.border.LineBorder;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
 
 import ui.controller.Control;
 import ui.model.Model;
@@ -46,7 +48,8 @@ public class TimePanel extends JPanel {
 			@Override
 			public void actionPerformed(ActionEvent ae) {
 				timeSlider.setValue(timeSlider.getValue() + 1);
-				if (timeSlider.getValue() == model.getIterations()) {
+				controller.setCurIteration(timeSlider.getValue());
+				if (timeSlider.getValue() == model.getIterations()-1) {
 					running = false;
 					playBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/play.png"))
 							.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
@@ -64,9 +67,15 @@ public class TimePanel extends JPanel {
 		timeSlider.setMinorTickSpacing(1);
 		timeSlider.setMajorTickSpacing(5);
 		timeSlider.setToolTipText("Time Slider");
-		timeSlider.setMaximum(model.getIterations());
+		timeSlider.setMaximum(model.getIterations()-1);
 		timeSlider.setValue(0);
 		timeSlider.setBackground(Color.WHITE);
+		timeSlider.addChangeListener(new ChangeListener() {
+			@Override
+			public void stateChanged(ChangeEvent e) {
+				controller.setCurIteration(timeSlider.getValue());
+			}
+		});
 
 		// Panel
 		timeBtnPanel.setBorder(null);
@@ -106,6 +115,7 @@ public class TimePanel extends JPanel {
 		timeResetBtn.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent ae) {
 				timeSlider.setValue(timeSlider.getMinimum());
+				controller.setCurIteration(timeSlider.getValue());
 			}
 		});
 		timeForwardBtn.setToolTipText("Forward");
@@ -118,6 +128,7 @@ public class TimePanel extends JPanel {
 		timeForwardBtn.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent ae) {
 				timeSlider.setValue(timeSlider.getValue() + 1);
+				controller.setCurIteration(timeSlider.getValue());
 			}
 		});
 		timeBackwardBtn.setToolTipText("Backward");
@@ -130,6 +141,7 @@ public class TimePanel extends JPanel {
 		timeBackwardBtn.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent ae) {
 				timeSlider.setValue(timeSlider.getValue() - 1);
+				controller.setCurIteration(timeSlider.getValue());
 			}
 		});
 

+ 177 - 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,82 @@ 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());
+		}
+		g2.drawLine((model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), MAXIMUM, (model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1),
+				this.getHeight());
+
 	}
 
 	@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 +176,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 +260,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 double
+	 *            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();
+	}
+
 }