|
@@ -31,6 +31,7 @@ import classes.HolonObject;
|
|
import classes.SubNet;
|
|
import classes.SubNet;
|
|
import classes.Vector2d;
|
|
import classes.Vector2d;
|
|
import ui.controller.Control;
|
|
import ui.controller.Control;
|
|
|
|
+import ui.controller.HolonCanvasController;
|
|
import ui.model.Model;
|
|
import ui.model.Model;
|
|
|
|
|
|
public class HolonCanvas extends JPanel implements MouseWheelListener, MouseListener, MouseMotionListener {
|
|
public class HolonCanvas extends JPanel implements MouseWheelListener, MouseListener, MouseMotionListener {
|
|
@@ -57,27 +58,25 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
private int frameCount = 0;
|
|
private int frameCount = 0;
|
|
private Dimension center;
|
|
private Dimension center;
|
|
private ArrayList<SubNet> subnets;
|
|
private ArrayList<SubNet> subnets;
|
|
|
|
+ private HolonCanvasController hCController;
|
|
|
|
|
|
private Control controller;
|
|
private Control controller;
|
|
private Model model;
|
|
private Model model;
|
|
- private int maxX;
|
|
|
|
- private int maxY;
|
|
|
|
private JComboBox<String> combo = new JComboBox<>();
|
|
private JComboBox<String> combo = new JComboBox<>();
|
|
private int comboChoice = 0;
|
|
private int comboChoice = 0;
|
|
private String info;
|
|
private String info;
|
|
private int bodyNr;
|
|
private int bodyNr;
|
|
private final JLabel lblBodyInfo = new JLabel("Holon Info:");
|
|
private final JLabel lblBodyInfo = new JLabel("Holon Info:");
|
|
private JCheckBox chckbxSort = new JCheckBox("sort by size");
|
|
private JCheckBox chckbxSort = new JCheckBox("sort by size");
|
|
- private ArrayList<HolonBody> sortedSize = new ArrayList<>();
|
|
|
|
- private ArrayList<HolonBody> sortedDist = new ArrayList<>();
|
|
|
|
private boolean sizeChange = false;
|
|
private boolean sizeChange = false;
|
|
private HolonBody toDrag;
|
|
private HolonBody toDrag;
|
|
- private boolean beingDragged = false;
|
|
|
|
-
|
|
|
|
|
|
+
|
|
public HolonCanvas(Model mod, Control control) {
|
|
public HolonCanvas(Model mod, Control control) {
|
|
// Wire up Events
|
|
// Wire up Events
|
|
this.controller = control;
|
|
this.controller = control;
|
|
this.model = mod;
|
|
this.model = mod;
|
|
|
|
+
|
|
|
|
+ hCController = new HolonCanvasController(mod);
|
|
|
|
|
|
add(lblBodyInfo);
|
|
add(lblBodyInfo);
|
|
this.add(combo);
|
|
this.add(combo);
|
|
@@ -133,9 +132,7 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
subnets = controller.getSimManager().getSubNets();
|
|
subnets = controller.getSimManager().getSubNets();
|
|
subCount = subnets.size();
|
|
subCount = subnets.size();
|
|
calcCenter();
|
|
calcCenter();
|
|
- maxX = center.width;
|
|
|
|
- maxY = center.height;
|
|
|
|
- addNewBodies(subCount);
|
|
|
|
|
|
+ hCController.addNewBodies(subCount,subnets, center, controller.getHolonBodyScale());
|
|
sizeChange = true;
|
|
sizeChange = true;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -151,9 +148,10 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
|
|
|
|
// check if HolonBodys should bes sorted after size
|
|
// check if HolonBodys should bes sorted after size
|
|
if (chckbxSort.isSelected() && sizeChange) {
|
|
if (chckbxSort.isSelected() && sizeChange) {
|
|
- rearrangeAfterSize();
|
|
|
|
|
|
+ hCController.rearrangeAfterSize();
|
|
|
|
+ sizeChange = false;
|
|
}
|
|
}
|
|
- updateBodies(elapsedTime / 1000f);
|
|
|
|
|
|
+ hCController.updateBodies(elapsedTime / 1000f);
|
|
|
|
|
|
render(g);
|
|
render(g);
|
|
|
|
|
|
@@ -167,64 +165,7 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
previousTime = currentTime;
|
|
previousTime = currentTime;
|
|
frameCount++;
|
|
frameCount++;
|
|
repaint();
|
|
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++) {
|
|
|
|
- if (model.getSubNetColors().get(i) == bodies.get(j).getColor()) {
|
|
|
|
- bodies.get(j).setRadius(
|
|
|
|
- (subnets.get(i).getObjects().size() * 5 + 10) * controller.getHolonBodyScale() / 100);
|
|
|
|
- bodies.get(j).setMass((float) Math.pow((subnets.get(i).getObjects().size() + 1) * 5, 3));
|
|
|
|
- newBodies.add(bodies.get(j));
|
|
|
|
- newBodies.get(i).setId(i);
|
|
|
|
- if (newBodies.get(i).position.getX() > maxX)
|
|
|
|
- maxX = (int) newBodies.get(i).position.getX();
|
|
|
|
- if (newBodies.get(i).position.getY() > maxY)
|
|
|
|
- maxY = (int) newBodies.get(i).position.getY();
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- 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,
|
|
|
|
- (float) Math.pow((subnets.get(i).getObjects().size() + 1) * 5, 3), model.getSubNetColors().get(i));
|
|
|
|
- temp.setId(i);
|
|
|
|
- bodies.add(temp);
|
|
|
|
-
|
|
|
|
- if (bodies.get(i).position.getX() > maxX)
|
|
|
|
- maxX = (int) newBodies.get(i).position.getX();
|
|
|
|
- if (bodies.get(i).position.getY() > maxY)
|
|
|
|
- maxY = (int) newBodies.get(i).position.getY();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
public void render(Graphics g) {
|
|
public void render(Graphics g) {
|
|
this.g2 = (Graphics2D) g;
|
|
this.g2 = (Graphics2D) g;
|
|
@@ -234,6 +175,7 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
this.g2.setColor(Color.WHITE);
|
|
this.g2.setColor(Color.WHITE);
|
|
this.g2.fillRect(0, 0, getWidth(), getHeight());
|
|
this.g2.fillRect(0, 0, getWidth(), getHeight());
|
|
|
|
|
|
|
|
+ bodies = hCController.getBodies();
|
|
// Render Game Objects
|
|
// Render Game Objects
|
|
for (int i = 0; i < subCount; i++) {
|
|
for (int i = 0; i < subCount; i++) {
|
|
bodyNr = bodies.get(i).getId();
|
|
bodyNr = bodies.get(i).getId();
|
|
@@ -251,19 +193,19 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
info = "" + subnets.get(bodyNr).getSwitches().size();
|
|
info = "" + subnets.get(bodyNr).getSwitches().size();
|
|
break;
|
|
break;
|
|
case 4:
|
|
case 4:
|
|
- info = "" + getTotalProduction(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
|
|
|
|
+ info = "" + hCController.getTotalProduction(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
break;
|
|
break;
|
|
case 5:
|
|
case 5:
|
|
- info = "" + getTotalConsumption(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
|
|
|
|
+ info = "" + hCController.getTotalConsumption(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
break;
|
|
break;
|
|
case 6:
|
|
case 6:
|
|
- info = "" + getTotalElements(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
|
|
|
|
+ info = "" + hCController.getTotalElements(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
break;
|
|
break;
|
|
case 7:
|
|
case 7:
|
|
- info = "" + getTotalProducers(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
|
|
|
|
+ info = "" + hCController.getTotalProducers(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
break;
|
|
break;
|
|
case 8:
|
|
case 8:
|
|
- info = "" + getActiveElements(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
|
|
|
|
+ info = "" + hCController.getActiveElements(new ArrayList<AbstractCpsObject>(subnets.get(bodyNr).getObjects()));
|
|
break;
|
|
break;
|
|
default:
|
|
default:
|
|
info = "" + bodyNr;
|
|
info = "" + bodyNr;
|
|
@@ -275,177 +217,7 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private int getActiveElements(ArrayList<AbstractCpsObject> objects) {
|
|
|
|
- int val = 0;
|
|
|
|
- for (AbstractCpsObject obj : objects) {
|
|
|
|
- if (obj instanceof HolonObject) {
|
|
|
|
- for (HolonElement ele : ((HolonObject) obj).getElements()) {
|
|
|
|
- if (ele.getActive()) {
|
|
|
|
- val += 1;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } else if (obj instanceof CpsUpperNode) {
|
|
|
|
- val += getTotalProduction(((CpsUpperNode) obj).getNodes());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return val;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private int getTotalProducers(ArrayList<AbstractCpsObject> objects) {
|
|
|
|
- float val = 0;
|
|
|
|
- int prod = 0;
|
|
|
|
- int tStep = model.getCurIteration();
|
|
|
|
- for (AbstractCpsObject obj : objects) {
|
|
|
|
- if (obj instanceof HolonObject) {
|
|
|
|
- for (HolonElement ele : ((HolonObject) obj).getElements()) {
|
|
|
|
- if (ele.getEnergyAt()[tStep] > 0 && ele.getActive()) {
|
|
|
|
- val += ele.getEnergyAt()[tStep] * ele.getAmount();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (val > 0)
|
|
|
|
- prod += 1;
|
|
|
|
- } else if (obj instanceof CpsUpperNode) {
|
|
|
|
- val += getTotalProduction(((CpsUpperNode) obj).getNodes());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return prod;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private int getTotalElements(ArrayList<AbstractCpsObject> objects) {
|
|
|
|
- int val = 0;
|
|
|
|
- for (AbstractCpsObject obj : objects) {
|
|
|
|
- if (obj instanceof HolonObject) {
|
|
|
|
- val += ((HolonObject) obj).getElements().size();
|
|
|
|
- } else if (obj instanceof CpsUpperNode) {
|
|
|
|
- val += getTotalConsumption(((CpsUpperNode) obj).getNodes());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return val;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private float getTotalConsumption(ArrayList<AbstractCpsObject> objects) {
|
|
|
|
- float val = 0;
|
|
|
|
- int tStep = model.getCurIteration();
|
|
|
|
- for (AbstractCpsObject obj : objects) {
|
|
|
|
- if (obj instanceof HolonObject) {
|
|
|
|
- for (HolonElement ele : ((HolonObject) obj).getElements()) {
|
|
|
|
- if (ele.getEnergyAt()[tStep] < 0 && ele.getActive()) {
|
|
|
|
- val += ele.getEnergyAt()[tStep] * ele.getAmount();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } else if (obj instanceof CpsUpperNode) {
|
|
|
|
- val += getTotalConsumption(((CpsUpperNode) obj).getNodes());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return val;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private float getTotalProduction(ArrayList<AbstractCpsObject> objects) {
|
|
|
|
- float val = 0;
|
|
|
|
- int tStep = model.getCurIteration();
|
|
|
|
- for (AbstractCpsObject obj : objects) {
|
|
|
|
- if (obj instanceof HolonObject) {
|
|
|
|
- for (HolonElement ele : ((HolonObject) obj).getElements()) {
|
|
|
|
- if (ele.getEnergyAt()[tStep] > 0 && ele.getActive()) {
|
|
|
|
- val += ele.getEnergyAt()[tStep] * ele.getAmount();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } else if (obj instanceof CpsUpperNode) {
|
|
|
|
- val += getTotalProduction(((CpsUpperNode) obj).getNodes());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return val;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void updateBodies(float elapsedSeconds) {
|
|
|
|
-
|
|
|
|
- // step the position of movable objects based off their velocity/gravity
|
|
|
|
- // and elapsedTime
|
|
|
|
- calcCenter();
|
|
|
|
- for (int i = 0; i < subCount; i++) {
|
|
|
|
- if (!bodies.get(i).equals(toDrag) || (bodies.get(i).equals(toDrag)&& !beingDragged)) {
|
|
|
|
- bodies.get(i).position.setX(
|
|
|
|
- (float) (bodies.get(i).position.getX() + (bodies.get(i).velocity.getX() * (elapsedSeconds))
|
|
|
|
- - ((bodies.get(i).position.getX() - center.getWidth()) / (50 + subCount))));
|
|
|
|
- bodies.get(i).position.setY(
|
|
|
|
- (float) (bodies.get(i).position.getY() + (bodies.get(i).velocity.getY() * (elapsedSeconds))
|
|
|
|
- - ((bodies.get(i).position.getY() - center.getHeight()) / (50 + subCount))));
|
|
|
|
-
|
|
|
|
- if (Math.abs(bodies.get(i).velocity.getX()) < Constants.epsilon)
|
|
|
|
- bodies.get(i).velocity.setX(0);
|
|
|
|
- if (Math.abs(bodies.get(i).velocity.getY()) < Constants.epsilon)
|
|
|
|
- bodies.get(i).velocity.setY(0);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- checkCollisions();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // Insertion sort for Sweep and Prune
|
|
|
|
- public void insertionSort(ArrayList<HolonBody> a) {
|
|
|
|
- for (int p = 1; p < subCount; p++) {
|
|
|
|
- Comparable<HolonBody> tmp = a.get(p);
|
|
|
|
- int j = p;
|
|
|
|
-
|
|
|
|
- for (; j > 0 && tmp.compareTo(a.get(j - 1)) < 0; j--)
|
|
|
|
- a.set(j, a.get(j - 1));
|
|
|
|
-
|
|
|
|
- a.set(j, (HolonBody) tmp);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 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);
|
|
|
|
-
|
|
|
|
- for (int i = 0; i < subCount; i++) {
|
|
|
|
- // Ball to Ball collision
|
|
|
|
- for (int j = i + 1; j < subCount; j++) {
|
|
|
|
- if ((bodies.get(i).position.getX() + bodies.get(i).getRadius()) < (bodies.get(j).position.getX()
|
|
|
|
- - bodies.get(j).getRadius()))
|
|
|
|
- break;
|
|
|
|
-
|
|
|
|
- if ((bodies.get(i).position.getY() + bodies.get(i).getRadius()) < (bodies.get(j).position.getY()
|
|
|
|
- - bodies.get(j).getRadius())
|
|
|
|
- || (bodies.get(j).position.getY() + bodies.get(j).getRadius()) < (bodies.get(i).position.getY()
|
|
|
|
- - bodies.get(i).getRadius()))
|
|
|
|
- continue;
|
|
|
|
-
|
|
|
|
- bodies.get(i).resolveCollision(bodies.get(j));
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+
|
|
// calc the center of the canvas
|
|
// calc the center of the canvas
|
|
public void calcCenter() {
|
|
public void calcCenter() {
|
|
center = this.getSize();
|
|
center = this.getSize();
|
|
@@ -485,23 +257,25 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
float cr = bodies.get(i).getRadius();
|
|
float cr = bodies.get(i).getRadius();
|
|
if (e.getX() - cr <= cx && e.getY() - cr <= cy && e.getX() + cr >= cx && e.getY() + cr >= cy) {
|
|
if (e.getX() - cr <= cx && e.getY() - cr <= cy && e.getX() + cr >= cx && e.getY() + cr >= cy) {
|
|
controller.addSelectedHolonBody(bodies.get(i).getId());
|
|
controller.addSelectedHolonBody(bodies.get(i).getId());
|
|
|
|
+ hCController.setBodyToDrag(bodies.get(i).getId());
|
|
toDrag = bodies.get(i);
|
|
toDrag = bodies.get(i);
|
|
break;
|
|
break;
|
|
} else {
|
|
} else {
|
|
controller.addSelectedHolonBody(-1);
|
|
controller.addSelectedHolonBody(-1);
|
|
toDrag = null;
|
|
toDrag = null;
|
|
}
|
|
}
|
|
|
|
+ hCController.setBodyToDrag(-1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void mouseReleased(MouseEvent e) {
|
|
public void mouseReleased(MouseEvent e) {
|
|
- beingDragged = false;
|
|
|
|
|
|
+ hCController.setDrag(false);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void mouseDragged(MouseEvent e) {
|
|
public void mouseDragged(MouseEvent e) {
|
|
- beingDragged = true;
|
|
|
|
|
|
+ hCController.setDrag(true);
|
|
if (toDrag != null) {
|
|
if (toDrag != null) {
|
|
toDrag.position = new Vector2d(e.getX(), e.getY());
|
|
toDrag.position = new Vector2d(e.getX(), e.getY());
|
|
}
|
|
}
|
|
@@ -511,6 +285,5 @@ public class HolonCanvas extends JPanel implements MouseWheelListener, MouseList
|
|
@Override
|
|
@Override
|
|
public void mouseMoved(MouseEvent e) {
|
|
public void mouseMoved(MouseEvent e) {
|
|
// TODO Auto-generated method stub
|
|
// TODO Auto-generated method stub
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|