Ver código fonte

Sort HolonBodys on HolonCanvas after size

jess 7 anos atrás
pai
commit
45b3e2a3df

+ 27 - 3
src/classes/HolonBody.java

@@ -1,6 +1,7 @@
 package classes;
 
 import java.awt.Color;
+import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Graphics2D;
 
@@ -16,8 +17,8 @@ public class HolonBody implements Comparable<HolonBody> {
 	public void setId(int id) {
 		this.id = id;
 	}
-	
-	public int getId(){
+
+	public int getId() {
 		return id;
 	}
 
@@ -40,7 +41,8 @@ public class HolonBody implements Comparable<HolonBody> {
 				(int) (2 * getRadius()), (int) (2 * getRadius()));
 		g2.setFont(new Font("TimesRoman", Font.PLAIN, (int) radius));
 		g2.setColor(Color.WHITE);
-		g2.drawString(info, position.getX() - g2.getFontMetrics().stringWidth(info)/2, position.getY() + g2.getFontMetrics().getHeight()/3 );
+		g2.drawString(info, position.getX() - g2.getFontMetrics().stringWidth(info) / 2,
+				position.getY() + g2.getFontMetrics().getHeight() / 3);
 	}
 
 	public void setRadius(float radius) {
@@ -142,4 +144,26 @@ public class HolonBody implements Comparable<HolonBody> {
 		}
 	}
 
+	public int compareSizeTo(HolonBody body) {
+		if (this.getRadius() > body.getRadius()) {
+			return 1;
+		} else if (this.getRadius() < body.getRadius()) {
+			return -1;
+		} else {
+			return 0;
+		}
+	}
+
+	public int compareDistTo(HolonBody body, Dimension center) {
+		float thisDist = (float) Math.sqrt(Math.pow(center.getWidth()-this.position.getX(),2)+Math.pow(center.getHeight()-this.position.getY(), 2));
+		float bodyDist = (float) Math.sqrt(Math.pow(center.getWidth()-body.position.getX(),2)+Math.pow(center.getHeight()-body.position.getY(), 2));
+		if (thisDist > bodyDist) {
+			return 1;
+		} else if (thisDist < bodyDist) {
+			return -1;
+		} else {
+			return 0;
+		}
+	}
+
 }

+ 75 - 18
src/ui/view/HolonCanvas.java

@@ -5,7 +5,6 @@ import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.RenderingHints;
-import java.awt.ScrollPane;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseEvent;
@@ -14,22 +13,17 @@ import java.awt.event.MouseWheelEvent;
 import java.awt.event.MouseWheelListener;
 import java.util.ArrayList;
 
+import javax.swing.JCheckBox;
+import javax.swing.JComboBox;
+import javax.swing.JLabel;
 import javax.swing.JPanel;
-import javax.swing.JScrollPane;
 
-import classes.AbstractCpsObject;
 import classes.Constants;
 import classes.HolonBody;
-import classes.HolonObject;
 import classes.SubNet;
+import classes.Vector2d;
 import ui.controller.Control;
 import ui.model.Model;
-import javax.swing.JTextArea;
-import java.awt.List;
-
-import javax.swing.JComboBox;
-import javax.swing.JMenuBar;
-import java.awt.Label;
 
 public class HolonCanvas extends JPanel implements MouseWheelListener, MouseListener {
 
@@ -61,17 +55,20 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 	private int maxX;
 	private int maxY;
 	private JComboBox<String> combo = new JComboBox<>();
-	private Label lblBodyInfo = new Label();
 	private int comboChoice = 0;
 	private String info;
 	private int bodyNr;
+	private final JLabel lblBodyInfo = new JLabel("Holon Info:");
+	private JCheckBox chckbxSort = new JCheckBox("sort by size");
+	private ArrayList<HolonBody> sortedSize = new ArrayList<>();
+	private ArrayList<HolonBody> sortedDist = new ArrayList<>();
+	private boolean sizeChange = false;
 
 	public HolonCanvas(Model mod, Control control) {
 		// Wire up Events
 		this.controller = control;
 		this.model = mod;
-		lblBodyInfo.setForeground(Color.BLACK);
-		lblBodyInfo.setText("Holon Info:");
+
 		add(lblBodyInfo);
 		this.add(combo);
 		subnets = controller.getSimManager().getSubNets();
@@ -93,6 +90,8 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 			}
 		});
 		this.addMouseListener(this);
+
+		add(chckbxSort);
 	}
 
 	// Start Render and Update Threads
@@ -106,6 +105,7 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 						(int) (Math.random() * 255)));
 			}
 		}
+		// check if subnets have changed
 		if (!controller.getSimManager().getSubNets().equals(subnets)) {
 			subnets = controller.getSimManager().getSubNets();
 			subCount = subnets.size();
@@ -113,6 +113,7 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 			maxX = center.width;
 			maxY = center.height;
 			addNewBodies(subCount);
+			sizeChange = true;
 		}
 
 		currentTime = System.currentTimeMillis();
@@ -125,7 +126,12 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 			totalElapsedTime = 0;
 		}
 
+		// check if HolonBodys should bes sorted after size
+		if (chckbxSort.isSelected() && sizeChange) {
+			rearrangeAfterSize();
+		}
 		updateBodies(elapsedTime / 1000f);
+
 		render(g);
 
 		try {
@@ -140,9 +146,29 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 		repaint();
 	}
 
+	// sort the HolonBodys after size
+	private void rearrangeAfterSize() {
+		sizeChange = false;
+		HolonBody tmp = null;
+		int j = 0;
+		sortedSize.addAll(bodies); 
+		insertionSizeSort(sortedSize);
+		sortedDist.addAll(bodies);
+
+		ArrayList<Vector2d> pos = insertionDistSort(sortedDist);
+		for (int i = 0; i < subCount; i++) {
+			for (j = 0; j < subCount; j++) {
+				if (sortedSize.get(subCount - 1 - i).getId() == bodies.get(j).getId()) {
+					bodies.get(j).position  = pos.get(i);
+					break;
+				}
+			}
+		}
+	}
+
 	// updates the bodies according to the changes of subnets
 	private void addNewBodies(int subCount) {
-
+		// find correct color for existing HolonBodys
 		ArrayList<HolonBody> newBodies = new ArrayList<>();
 		for (int i = 0; i < subCount; i++) {
 			for (int j = 0; j < bodies.size(); j++) {
@@ -162,6 +188,7 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 		}
 
 		bodies = newBodies;
+		// adding new HolonBodys
 		for (int i = bodies.size(); i < subCount; i++) {
 			float radius = (subnets.get(i).getObjects().size() * 5 + 10) * controller.getHolonBodyScale() / 100;
 			HolonBody temp = new HolonBody(maxX + 1, maxY + 1, radius,
@@ -174,7 +201,6 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 			if (bodies.get(i).position.getY() > maxY)
 				maxY = (int) newBodies.get(i).position.getY();
 		}
-
 	}
 
 	public void render(Graphics g) {
@@ -245,6 +271,36 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 		}
 	}
 
+	// Insertion sort for subnet size
+	private void insertionSizeSort(ArrayList<HolonBody> a) {
+		for (int p = 1; p < subCount; p++) {
+			HolonBody tmp = a.get(p);
+			int j = p;
+
+			for (; j > 0 && tmp.compareSizeTo(a.get(j - 1)) < 0; j--)
+				a.set(j, a.get(j - 1));
+
+			a.set(j, (HolonBody) tmp);
+		}
+	}
+
+	// Insertion sort for HolonBody distance
+	private ArrayList<Vector2d> insertionDistSort(ArrayList<HolonBody> a) {
+		ArrayList<Vector2d> pos = new ArrayList<>();
+		for (int p = 1; p < subCount; p++) {
+			HolonBody tmp = a.get(p);
+			int j = p;
+
+			for (; j > 0 && tmp.compareDistTo(a.get(j - 1), center) < 0; j--)
+				a.set(j, a.get(j - 1));
+
+			a.set(j, (HolonBody) tmp);
+		}
+		for (int i = 0; i < subCount; i++)
+			pos.add(a.get(i).position);
+		return pos;
+	}
+
 	public void checkCollisions() {
 		insertionSort(bodies);
 
@@ -268,12 +324,13 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 
 	}
 
+	// calc the center of the canvas
 	public void calcCenter() {
 		center = this.getSize();
 		center.height /= 2;
 		center.width /= 2;
 	}
-
+	
 	@Override
 	public void mouseWheelMoved(MouseWheelEvent e) {
 		controller.setHolonBodyScale(model.getHolonBodyScale() + (-e.getScrollAmount() * e.getWheelRotation()));
@@ -306,6 +363,7 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 			float cr = bodies.get(i).getRadius();
 			if (e.getX() - cr <= cx && e.getY() - cr <= cy && e.getX() + cr >= cx && e.getY() + cr >= cy) {
 				controller.addSelectedHolonBody(bodies.get(i).getId());
+				break;
 			} else {
 				controller.addSelectedHolonBody(-1);
 			}
@@ -316,5 +374,4 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
 	public void mouseReleased(MouseEvent e) {
 		// TODO Auto-generated method stub
 	}
-
-}
+}

+ 0 - 7
src/ui/view/StatisticGraph.java

@@ -4,18 +4,11 @@ import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
-import java.awt.Image;
 import java.awt.RenderingHints;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
 import java.awt.geom.GeneralPath;
 import java.util.ArrayList;
 
-import javax.swing.ImageIcon;
 import javax.swing.JPanel;
-import javax.swing.Timer;
-
-import com.sun.media.jfxmedia.track.Track;
 
 import classes.AbstractCpsObject;
 import classes.CpsUpperNode;