Browse Source

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 years ago
parent
commit
6917e3fbd9

BIN
res/Images/home-2.png


BIN
res/Images/power-plant.png


BIN
res/Images/switch-off.png


BIN
res/Images/switch-on.png


BIN
res/Images/transformer-1.png


+ 2 - 2
src/classes/CpsNode.java

@@ -10,8 +10,8 @@ public class CpsNode extends CpsObject {
 		super(objName);
 		this.setConnections(new ArrayList<CpsEdge>());
 		this.setImage("/Images/node.png");
-		this.setID(idCounter.nextId());
-		this.setStored("Canvas");
+		this.setSav("Canvas");
 	}
+	
 
 }

+ 5 - 5
src/classes/CpsObject.java

@@ -20,7 +20,7 @@ public abstract class CpsObject implements ComparableObject {
 	Position position;
 	/* Energy input and output of each object in the grid */
 	
-	String stored;
+	String sav;
 	
 	float energyIn;
 	float energyOut;
@@ -125,15 +125,15 @@ public abstract class CpsObject implements ComparableObject {
 	/**
 	 * @return the stored
 	 */
-	public String getStored() {
-		return stored;
+	public String getSav() {
+		return sav;
 	}
 
 	/**
 	 * @param stored the stored to set
 	 */
-	public void setStored(String stored) {
-		this.stored = stored;
+	public void setSav(String sav) {
+		this.sav = sav;
 	}
 
 	/* Getter and Setters for the energy input and output */

+ 23 - 5
src/classes/HolonElement.java

@@ -20,7 +20,9 @@ public class HolonElement {
 	/* +: for Consumers and -: Producers */
 	char sign;
 	
-	String stored;
+	String sav;
+	
+	String obj;
 	/*
 	 * Energy at each point of the graph with 50 predefined points. At the
 	 * beginning, it starts with all values at energy
@@ -45,6 +47,8 @@ public class HolonElement {
 		setActive(element.getActive());
 		setSign(element.getEnergy());
 		setEnergyAt(element.getEnergy());
+		setSav("Canvas");
+		setObj(element.getObj());
 	}
 
 	public float[] getEnergyAt() {
@@ -169,15 +173,29 @@ public class HolonElement {
 	/**
 	 * @return the stored
 	 */
-	public String getStored() {
-		return stored;
+	public String getSav() {
+		return sav;
 	}
 
 	/**
 	 * @param stored the stored to set
 	 */
-	public void setStored(String stored) {
-		this.stored = stored;
+	public void setSav(String sav) {
+		this.sav = sav;
+	}
+
+	/**
+	 * @return the obj
+	 */
+	public String getObj() {
+		return obj;
+	}
+
+	/**
+	 * @param obj the obj to set
+	 */
+	public void setObj(String obj) {
+		this.obj = obj;
 	}
 	
 	/**

+ 2 - 0
src/classes/HolonObject.java

@@ -2,6 +2,8 @@ package classes;
 
 import java.util.ArrayList;
 
+import ui.model.idCounter;
+
 public class HolonObject extends CpsObject {
 
 	/* Array of all consumers */

+ 4 - 0
src/ui/controller/CanvasController.java

@@ -1,5 +1,7 @@
 package ui.controller;
 
+import java.util.ArrayList;
+
 import Interfaces.CategoryListener;
 import Interfaces.ObjectListener;
 import classes.CpsEdge;
@@ -22,6 +24,8 @@ public class CanvasController {
 	 */
 	public void addObjectIntoCanvas(CpsObject object) {
 		String objName = object.getObjName();
+		object.setSav("Canvas");
+		object.setConnections(new ArrayList<>());
 		MODEL.getObjectsOnCanvas().add(object);
 		notifyObjListeners();
 		System.out.println("Added: " + objName);

+ 16 - 29
src/ui/controller/CategoryController.java

@@ -24,34 +24,17 @@ public class CategoryController {
 	 * init default category and objects
 	 */
 	public void initCategories() {
-		Category energy = new Category("Energy");
-		Category building = new Category("Building");
-		Category component = new Category("Component");
 
-		HolonObject powerp = new HolonObject("Power Plant");
-		powerp.setImage("/Images/power-plant.png");
-		powerp.setStored("Energy");
 		
-		HolonObject house = new HolonObject("House");
-		house.setImage("/Images/home.png");
-		house.setStored("Building");
-
-		HolonTransformer transformer = new HolonTransformer("Transformer");
-		transformer.setImage("/Images/transformer.png");
-		transformer.setStored("Component");
+		addNewCategory("Energy");
+		addNewCategory("Building");
+		addNewCategory("Component");
+		
+		addNewHolonObject(searchCatNode("Energy"), "Power Plant", new ArrayList<>(), "/Images/power-plant.png");
+		addNewHolonObject(searchCatNode("Building"), "House", new ArrayList<>(), "/Images/home-2.png");
+		addNewHolonTransformer(searchCatNode("Component"), "Transformer", "/Images/transformer-1.png");
+		addNewHolonSwitch(searchCatNode("Component"), "Switch", "/Images/switch-on.png");
 		
-		HolonSwitch sw = new HolonSwitch("Switch");
-		sw.setImage("/Images/switch-on.png");
-		sw.setStored("Component");
-
-		addObject(energy, powerp);
-		addObject(building, house);
-		addObject(component, transformer);
-		addObject(component, sw);
-
-		addCategory(energy);
-		addCategory(building);
-		addCategory(component);
 	}
 
 	/**
@@ -133,6 +116,7 @@ public class CategoryController {
 		HolonObject object = new HolonObject(objName);
 		object.setImage(image);
 		object.setElements(elements);
+		object.setSav(cat.getName());
 		addObject(cat, object);
 	}
 
@@ -144,9 +128,10 @@ public class CategoryController {
 	 * @param obj
 	 *            New Object Name
 	 */
-	public void addNewHolonTransformer(Category cat, String objName) {
+	public void addNewHolonTransformer(Category cat, String objName, String image) {
 		HolonTransformer transformer = new HolonTransformer(objName);
-		transformer.setImage("/Images/transformer.png");
+		transformer.setImage(image);
+		transformer.setSav(cat.getName());
 		addObject(cat, transformer);
 	}
 
@@ -158,9 +143,11 @@ public class CategoryController {
 	 * @param obj
 	 *            New Object Name
 	 */
-	public void addNewHolonSwitch(Category cat, String objName) {
+	public void addNewHolonSwitch(Category cat, String objName, String image) {
 		HolonSwitch holonSwitch = new HolonSwitch(objName);
-		holonSwitch.setImage("/Images/switch-on.png");
+
+		holonSwitch.setImage(image);
+		holonSwitch.setSav(cat.getName());
 		addObject(cat, holonSwitch);
 	}
 

+ 9 - 5
src/ui/controller/Control.java

@@ -33,7 +33,7 @@ public class Control {
 		this.objectController = new ObjectController(MODEL);
 		this.canvasController = new CanvasController(MODEL);
 		this.globalController = new GlobalController(MODEL);
-		this.loadStoreController = new LoadStoreController(MODEL);
+		this.loadStoreController = new LoadStoreController(MODEL, categoryController, canvasController, objectController);
 		
 	}
 
@@ -51,11 +51,11 @@ public class Control {
 	// }
 
 	public void addTransformer(Category cat, String objName) {
-		categoryController.addNewHolonTransformer(cat, objName);
+		categoryController.addNewHolonTransformer(cat, objName, "/Images/transformer-1.png");
 	}
 
 	public void addSwitch(Category cat, String objName) {
-		categoryController.addNewHolonSwitch(cat, objName);
+		categoryController.addNewHolonSwitch(cat, objName, "/Images/switch-on.png");
 	}
 
 	public Category searchCategory(String name) {
@@ -144,8 +144,12 @@ public class Control {
 	}
 	
 	/* Operations for Loading and Storing */
-	public void writeFile() throws IOException {
-		loadStoreController.writeJSONFile();
+	public void saveFile(String path) throws IOException {
+		loadStoreController.writeJSONFile(path);
+	}
+	
+	public void loadFile(String path) throws IOException {
+		loadStoreController.readJSON(path);
 	}
 	////////// etc
 	public void initListener(CategoryListener catLis) {

+ 257 - 27
src/ui/controller/LoadStoreController.java

@@ -5,85 +5,315 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+
 import classes.Category;
+import classes.CpsEdge;
+import classes.CpsNode;
 import classes.CpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.HolonTransformer;
 import ui.model.Model;
+import ui.model.idCounter;
 
 public class LoadStoreController {
+
 	private Model MODEL;
+	private CategoryController categoryController;
+	private CanvasController canvasController;
+	private ObjectController objectController;
 
-	public LoadStoreController(Model model) {
+	public LoadStoreController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj) {
 		this.MODEL = model;
+		this.categoryController = cg;
+		this.canvasController = cvs;
+		this.objectController = obj;
+
 	}
 
-	
-	
-	public void writeJSONFile() throws IOException {
+	/**
+	 * Writes a Savefile in .json
+	 * 
+	 * @throws IOException
+	 */
+	public void writeJSONFile(String path) throws IOException {
 
 		JSONObject json = new JSONObject();
 
-		
+		json.put("ID", idCounter.getCounter());
 		writeCategory(json);
-		writeCategoryObjects(json);
-		writeCategoryElements(json);
+		writeObjects(json);
+		writeElements(json);
+		writeEdges(json);
 
-		FileWriter writer = new FileWriter("//Users//zheng//Desktop//Tesst.json");
+		FileWriter writer = new FileWriter(path);
+		writer.write(json.toJSONString());
 		writer.flush();
 		writer.close();
 	}
 
-	public void writeCategory(JSONObject json) throws IOException {
+	/**
+	 * writes all Categories into a JSONObject
+	 * 
+	 * @param json
+	 * @throws IOException
+	 */
+	public void writeCategory(JSONObject json) {
 		JSONArray arr = new JSONArray();
-		
+
 		for (Category cat : MODEL.getCategories())
 			arr.add(cat.getName());
-		json.put("Category", arr);
+
+		json.put("CG", arr);
 	}
 
-	public void writeCategoryObjects(JSONObject json) throws IOException {
-		
+	/**
+	 * writes all Objects in Category into a JSONObject
+	 * 
+	 * @param json
+	 */
+	public void writeObjects(JSONObject json) {
+
 		JSONArray arr = new JSONArray();
 		int i = 1;
-		
+
 		for (Category cats : MODEL.getCategories())
 			for (CpsObject cps : cats.getObjects()) {
-				arr.add(cps.getStored());
+				arr.add(getObjectType(cps));
+				arr.add(cps.getSav());
 				arr.add(cps.getObjName());
-				arr.add(cps.getName());
 				arr.add(cps.getImage());
-				json.put("CategoryObject." + i++, arr);
+				json.put("CGO" + i++, arr);
 				arr = new JSONArray();
 			}
+
+		i = 1;
+		for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
+			arr.add(getObjectType(cps));
+			arr.add(cps.getObjName());
+			arr.add(cps.getID());
+			arr.add(cps.getImage());
+			arr.add(cps.getPosition().x);
+			arr.add(cps.getPosition().y);
+			json.put("CVSO" + i++, arr);
+			arr = new JSONArray();
+			;
+
+		}
 	}
 
-	public void writeCategoryElements(JSONObject json) throws IOException {
-		
+	/**
+	 * 
+	 * @param cps
+	 * @return
+	 */
+	public String getObjectType(CpsObject cps) {
+		if (cps instanceof HolonObject)
+			return "HolonObject";
+		if (cps instanceof HolonTransformer)
+			return "HolonTransformer";
+		if (cps instanceof HolonSwitch)
+			return "HolonSwitch";
+		else
+			return "CpsNode";
+	}
+
+	/**
+	 * writes all Elements in Objects in Category into a JSONObject
+	 * 
+	 * @param json
+	 */
+	public void writeElements(JSONObject json) {
 
 		JSONArray arr = new JSONArray();
 		int i = 1;
-		
+
 		for (Category cats : MODEL.getCategories())
 			for (CpsObject cps : cats.getObjects())
 				if (cps instanceof HolonObject)
 					for (HolonElement ele : ((HolonObject) cps).getElements()) {
-						arr.add(ele.getStored());
+						arr.add(ele.getSav());
+						arr.add(ele.getObj());
 						arr.add(ele.getEleName());
 						arr.add(ele.getAmount());
 						arr.add(ele.getEnergy());
-						json.put("CategoryElement." + i++, arr);
+						json.put("CGE" + i++, arr);
 						arr = new JSONArray();
 					}
+
+		i = 1;
+		for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
+			if (cps instanceof HolonObject)
+				for (HolonElement ele : ((HolonObject) cps).getElements()) {
+					arr.add(ele.getSav());
+					arr.add(cps.getID());
+					arr.add(ele.getEleName());
+					arr.add(ele.getAmount());
+					arr.add(ele.getEnergy());
+					json.put("CVSE" + i++, arr);
+					arr = new JSONArray();
+				}
+		}
 	}
 
-	public void readFromJSON(File jsonFile) throws IOException {
-		String line;
-		BufferedReader reader = new BufferedReader(new FileReader("textfile"));
-		while ((line = reader.readLine()) != null) {
-			// mach hier irgendwas mit der Gelesenen Zeile
+	/**
+	 * write all Edges into a JSONObject
+	 * 
+	 * @param json
+	 */
+	public void writeEdges(JSONObject json) {
+
+		JSONArray arr = new JSONArray();
+		int i = 1;
+
+		for (CpsEdge edge : MODEL.getEdgesOnCanvas()) {
+			arr.add(edge.getA().getID());
+			arr.add(edge.getB().getID());
+			arr.add(edge.getCapacity());
+			arr.add(edge.getFlow());
+			json.put("EDGE" + i++, arr);
+			arr = new JSONArray();
 		}
 	}
+
+	public void readJSON(String path) throws IOException {
+		JSONParser parser = new JSONParser();
+		MODEL.setCategories(new ArrayList<>());
+		MODEL.setObjectsOnCanvas(new ArrayList<>());
+
+		ArrayList<String> obj = new ArrayList<>();
+		ArrayList<String> ele = new ArrayList<>();
+		ArrayList<String> edge = new ArrayList<>();
+
+		try {
+
+			JSONObject json = (JSONObject) parser.parse(new FileReader(path));
+
+			for (Object key : json.keySet()) {
+
+				if (key.equals("CG"))
+					readCategory((JSONArray) json.get(key));
+				else if (key.toString().contains("CGO") || key.toString().contains("CVSO"))
+					obj.add(key.toString());
+				else if (key.toString().contains("CGE") || key.toString().contains("CVSE"))
+					ele.add(key.toString());
+				else if (key.toString().contains("EDGE"))
+					edge.add(key.toString());
+				else idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
+			}
+
+			dispatch(obj, json);
+			dispatch(ele, json);
+			dispatch(edge, json);
+
+		} catch (ParseException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+	}
+
+	public void dispatch(ArrayList<String> input, JSONObject json) {
+
+		for (String str : input) {
+			if (str.contains("CGO")) {
+				readCategoryObject((JSONArray) json.get(str));
+			} else if (str.contains("CVSO")) {
+				readCanvasObject((JSONArray) json.get(str));
+			} else if (str.contains("CGE") || str.contains("CVSE")) {
+				readElement((JSONArray) json.get(str));
+			} else if (str.contains("EDGE")) {
+				readEdge((JSONArray) json.get(str));
+			}
+		}
+
+	}
+
+	public void readCategory(JSONArray arr) {
+
+		Iterator<Object> i = arr.iterator();
+
+		while (i.hasNext()) {
+			categoryController.addNewCategory(i.next().toString());
+		}
+	}
+
+	public void readCategoryObject(JSONArray arr) {
+		Iterator<Object> i = arr.iterator();
+
+		String type = i.next().toString();
+
+		if (type.equals("HolonObject")) {
+			categoryController.addNewHolonObject(categoryController.searchCatNode(i.next().toString()),
+					i.next().toString(), new ArrayList<>(), i.next().toString());
+		}
+		if (type.equals("HolonTransformer")) {
+			categoryController.addNewHolonTransformer(categoryController.searchCatNode(i.next().toString()),
+					i.next().toString(), i.next().toString());
+		}
+		if (type.equals("HolonSwitch")) {
+			categoryController.addNewHolonSwitch(categoryController.searchCatNode(i.next().toString()),
+					i.next().toString(), i.next().toString());
+		}
+
+	}
+
+	public void readCanvasObject(JSONArray arr) {
+		Iterator<Object> i = arr.iterator();
+		CpsObject cps = null;
+
+		String type = i.next().toString();
+
+		if (type.equals("HolonObject")) {
+			cps = new HolonObject(i.next().toString());
+		}
+		if (type.equals("HolonTransformer")) {
+			cps = new HolonTransformer(i.next().toString());
+		}
+		if (type.equals("HolonSwitch")) {
+			cps = new HolonSwitch(i.next().toString());
+		}
+		if (type.equals("CpsNode")) {
+			cps = new CpsNode(i.next().toString());
+		}
+
+		cps.setID(Integer.parseInt(i.next().toString()));
+		cps.setImage(i.next().toString());
+		cps.setPosition(Integer.parseInt(i.next().toString()), Integer.parseInt(i.next().toString()));
+
+		canvasController.addObjectIntoCanvas(cps);
+	}
+
+	public void readElement(JSONArray arr) {
+		Iterator<Object> i = arr.iterator();
+
+		String sav = i.next().toString();
+		System.out.println(sav);
+
+		if (sav.equals("Canvas")) {
+			objectController.addNewElementIntoCanvasObject(Integer.parseInt(i.next().toString()), i.next().toString(),
+					Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
+		} else
+			objectController.addNewElementIntoCategoryObject(sav, i.next().toString(), i.next().toString(),
+					Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
+	}
+
+	public void readEdge(JSONArray arr) {
+		Iterator<Object> i = arr.iterator();
+
+		CpsEdge edge = new CpsEdge(objectController.searchByID(Integer.parseInt(i.next().toString())),
+				objectController.searchByID(Integer.parseInt(i.next().toString())));
+		edge.setCapacity(Float.parseFloat(i.next().toString()));
+		edge.setFlow(Float.parseFloat(i.next().toString()));
+
+		canvasController.addEdgeOnCanvas(edge);
+	}
 }

+ 5 - 1
src/ui/controller/ObjectController.java

@@ -43,6 +43,8 @@ public class ObjectController {
 	 * @param type
 	 */
 	public void addElementIntoCanvasObject(HolonObject object, HolonElement element) {
+		element.setSav("Canvas");
+		element.setObj(object.getObjName());
 		addElement(object, element);
 	}
 
@@ -75,7 +77,9 @@ public class ObjectController {
 				break;
 			}
 		}
-		element.setStored(object);
+		element.setSav(cat.getName());
+		element.setObj(object);
+		
 		addElement(searchHolonObject(object, cat.getObjects()), element);
 	}
 

+ 16 - 0
src/ui/model/idCounter.java

@@ -3,8 +3,24 @@ package ui.model;
 public class idCounter {
 	private static int counter = 1;
 
+
 	public static synchronized int nextId() {
 		return counter++;
 
 	}
+	
+	/**
+	 * @return the counter
+	 */
+	public static int getCounter() {
+		return counter;
+	}
+
+	/**
+	 * @param counter the counter to set
+	 */
+	public static void setCounter(int counter) {
+		idCounter.counter = counter;
+	}
+
 }

+ 35 - 4
src/ui/view/GUI.java

@@ -1,6 +1,7 @@
 package ui.view;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 
 import javax.swing.JFrame;
@@ -177,9 +178,8 @@ public class GUI implements CategoryListener {
 		this.controller = control;
 		this.model = control.getModel();
 		this.canvas = new MyCanvas(model, control);
-		this.unitGraph = new UnitGraph(model, control); // for testing, remove
-
-		// later
+		this.unitGraph = new UnitGraph(model, control);
+		
 		control.initListener(this);
 		initialize();
 		updateCategories(model.getCategories());
@@ -725,6 +725,7 @@ public class GUI implements CategoryListener {
 			@Override
 			public void actionPerformed(java.awt.event.ActionEvent evt) {
 				menuFileExitActionPerformed(evt);
+				
 			}
 
 			private void menuFileExitActionPerformed(java.awt.event.ActionEvent evt) {
@@ -732,7 +733,37 @@ public class GUI implements CategoryListener {
 				JFrame test = new JFrame();
 				if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
 					File file = fileChooser.getSelectedFile();
-					System.out.println("File Path is: " + file.toString());
+					
+					try {
+						controller.loadFile(file.getAbsolutePath());
+					} catch (IOException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
+				}
+			}
+		});
+		
+		mntmSave.addActionListener(new java.awt.event.ActionListener() {
+			@Override
+			public void actionPerformed(java.awt.event.ActionEvent evt) {
+				
+				menuSaveActionPerformed(evt);
+				
+			}
+
+			private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
+				JFileChooser fileChooser = new JFileChooser();
+				JFrame test = new JFrame();
+				if (fileChooser.showSaveDialog(test) == JFileChooser.APPROVE_OPTION) {
+					File file = fileChooser.getSelectedFile();
+					
+					try {
+						controller.saveFile(file.getAbsolutePath());
+					} catch (IOException e) {
+						// TODO Auto-generated catch block
+						e.printStackTrace();
+					}
 				}
 			}
 		});

+ 0 - 2
src/ui/view/Main.java

@@ -25,14 +25,12 @@ public class Main {
 		EventQueue.invokeLater(new Runnable() {
 			public void run() {
 				try {
-					idCounter ID = new idCounter();
 					Model MODEL = new Model();
 					Control CONTROL = new Control(MODEL);
 					GUI VIEW = new GUI(CONTROL);
 
 					VIEW.getFrmCyberPhysical().setVisible(true);
 
-					// CONTROL.writeFile();
 
 				} catch (Exception e) {
 					e.printStackTrace();

+ 1 - 0
src/ui/view/MyCanvas.java

@@ -414,6 +414,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 		// ins leere Gedragged
 		if (node) {
 			CpsNode n = new CpsNode("Node");
+			n.setID(idCounter.nextId());
 			n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
 			controller.addObjectCanvas(n);
 

+ 60 - 41
src/ui/view/UnitGraph.java

@@ -11,6 +11,7 @@ import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
 import java.awt.event.MouseMotionListener;
 import java.awt.geom.CubicCurve2D;
+import java.awt.geom.GeneralPath;
 import java.awt.geom.Line2D;
 import java.util.LinkedList;
 import java.awt.Point;
@@ -27,7 +28,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 	private static final long serialVersionUID = 1L;
 	private int ITERATIONS = 50;
-	private int MAXIMUM = 1;
+	private float MAXIMUM = 0;
 
 	private Point recSize = new Point(8, 8); // Point Size
 	private Graphics2D g2;
@@ -44,6 +45,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	private HolonElement tempElement;
 	private Model model;
 	private Control controller;
+	GeneralPath graphCurve = new GeneralPath();  
 
 	private boolean pointDrag = false;
 	private boolean init = false;
@@ -56,9 +58,9 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		this.controller = control;
 		this.model = model;
 		this.ITERATIONS = model.getIterations();
-
+		
 		this.pointList = new LinkedList<>();
-
+		
 		this.addMouseListener(this);
 		this.addMouseMotionListener(this);
 		this.addComponentListener(this);
@@ -72,23 +74,23 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 */
 	public void paintComponent(Graphics g) {
 		super.paintComponent(g);
-
-		if (arrayOfValue != null) {
-			for (int i = 0; i < arrayOfValue.length; i++) {
-				System.out.println(""+arrayOfValue[i]);
-				arrayOfValue[i] = getYValueAt((int) (i * width / (ITERATIONS - 1)));
-			}
-		}
-
 		g2 = (Graphics2D) g;
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 		g2.setStroke(new BasicStroke(1));
-
+		
+		graphCurve.reset();
+		
+		if (arrayOfValue != null) {
+			for (int i = 0; i < arrayOfValue.length; i++) {
+				arrayOfValue[i] = convertToValueY(getYValueAt((int) (i * width / (ITERATIONS - 1))));
+			}
+		}
+		
 		// Draw the Vertical Lines
 		g2.setColor(new Color(240, 240, 240));
 		for (int i = 0; i < ITERATIONS; i++) {
-			g2.drawLine((i) * this.getWidth() / (ITERATIONS - 1), MAXIMUM, (i) * this.getWidth() / (ITERATIONS - 1),
+			g2.drawLine((i) * this.getWidth() / (ITERATIONS - 1), 0, (i) * this.getWidth() / (ITERATIONS - 1),
 					this.getHeight());
 		}
 
@@ -96,14 +98,15 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			g2.drawLine(0, (i) * this.getHeight() / (ITERATIONS - 1), this.getWidth(),
 					(i) * this.getHeight() / (ITERATIONS - 1));
 		}
-
+		
 		// Draw the Lines
 		g2.setColor(Color.BLACK);
 		for (int i = 0; i < pointList.size() - 1; i++) {
 			c = buildCurve(pointList.get(i), pointList.get(i + 1));
-			g2.draw(c);
+			graphCurve.append(c, true);
 		}
-
+		g2.draw(graphCurve);
+		
 		// Draw the Points
 		g2.setColor(Color.BLUE);
 		for (int i = 0; i < pointList.size() - 0; i++) {
@@ -111,9 +114,21 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 					(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
 					(int) recSize.getY());
 		}
-		g2.drawLine((model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), MAXIMUM,
+		g2.drawLine((model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), 0,
 				(model.getCurIteration()) * this.getWidth() / (ITERATIONS - 1), this.getHeight());
 
+		//Actual Iteration Point Visualization
+		/*
+		g2.setColor(Color.RED);
+		if (arrayOfValue != null) {
+			for (int i = 0; i < arrayOfValue.length; i++) {
+				g2.fillOval((int) (i * width / (ITERATIONS - 1) * scaleX - recSize.getX() / 2),
+						(int) (convertToCanvasY((int) arrayOfValue[i]) * scaleY - recSize.getY() / 2), (int) recSize.getX(),
+						(int) recSize.getY());
+			}
+		}
+		*/
+		
 	}
 
 	@Override
@@ -124,8 +139,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			x = e.getX() / scaleX;
 			y = e.getY() / scaleY;
 			// y
-			if (e.getY() <= MAXIMUM) {
-				y = MAXIMUM / scaleY;
+			if (e.getY() <= 0) {
+				y = 0 / scaleY;
 			} else if (this.getHeight() <= e.getY()) {
 				y = this.getHeight() / scaleY;
 			}
@@ -186,8 +201,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				&& 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)));
+					if (e.getY() <= 0) {
+						pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
 					} else {
 						pointList.add(i, new Point((int) (x), (int) y));
 					}
@@ -215,7 +230,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 	public void componentResized(ComponentEvent e) {
 		if (init) {
-			MAXIMUM = (int) convertToCanvasY(MAXIMUM);
+			MAXIMUM = tempElement.getEnergy();
 			init = false;
 			// for scale
 			if (width == -1 && height == -1) {
@@ -227,8 +242,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			scaleY = this.getHeight() / height;
 
 			if (pointList.isEmpty()) {
-				pointList.addFirst(new Point(0, MAXIMUM));
-				pointList.addLast(new Point((int) (this.getWidth() / scaleX), MAXIMUM));
+				pointList.addFirst(new Point(0, 0));
+				pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
 			}
 		}
 		scaleX = this.getWidth() / width;
@@ -254,8 +269,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 */
 	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)));
+		pointList.addFirst(new Point(0,0));
+		pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
 		repaint();
 	}
 
@@ -264,18 +279,26 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * 
 	 * @param double
 	 *            d, the number to convert
-	 * @return the convertet number
+	 * @return the converted 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));
-		}
+	public double convertToCanvasY(float d) {
+		System.out.println(""+(height + (d*(height/MAXIMUM))));
+		return (height -(d*(height/MAXIMUM)));
+	}
+	
+	/**
+	 * converts the number to fit the value
+	 * 
+	 * @param double
+	 *            d, the number to convert
+	 * @return the converted number
+	 */
+	public float convertToValueY(double d){
+		return (float)((height - ( height * (d/height)))/(height/MAXIMUM));
 	}
 
 	/**
-	 * Viusalise the HolonElement on the Graph
+	 * Visualize the HolonElement on the Graph
 	 * 
 	 * @param HolonElement
 	 *            ele, which should be visualized
@@ -326,12 +349,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	public int getYValueAt(int xVal) {
 		for (int i = 0; i < pointList.size() - 1; i++) {
 			// get the Points
-			if (xVal > pointList.get(i + 1).getX()) {
-				x = pointList.get(i + 1).getX();
-				y = pointList.get(i).getX();
-
+			if (xVal < pointList.get(i + 1).getX()) {
 				// Curve erstellen
-				c = buildCurve(pointList.get(i), pointList.get(i + 1));
 				Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
 						pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
 				Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
@@ -353,8 +372,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			return null;
 		} else {
 			double z = (sx * (qy - py) + sy * (px - qx)) / det;
-			if (z == 0 || z == 1)
-				return null; // intersection at end point!
+			if (z < 0 || z > 1)
+				return new Point(0,0); // intersection at end point!
 			return new Point((int) (px + z * rx), (int) (py + z * ry));
 		}
 	} // end intersection line-line