Skip to content
Snippets Groups Projects
Commit 1d98cae7 authored by AxelCarayon's avatar AxelCarayon
Browse files

improved display

parent 5130f6f9
Branches
Tags
No related merge requests found
...@@ -34,17 +34,18 @@ public class SEIRS_SMA implements SMA{ ...@@ -34,17 +34,18 @@ public class SEIRS_SMA implements SMA{
private StatisticsCanvas statisticsCanvas; private StatisticsCanvas statisticsCanvas;
private DisplaySquaredEnvironment display; private DisplaySquaredEnvironment display;
private Random r; private Random r;
private FrameBuilder fb = new FrameBuilder();
private HashMap<String,Integer> stats; private HashMap<String,Integer> stats;
private void initGraphics() { private void initGraphics() {
statisticsCanvas = new StatisticsCanvas(500,500); statisticsCanvas = new StatisticsCanvas(300,parameters.getSize());
display = new DisplaySquaredEnvironment(environment,agents); display = new DisplaySquaredEnvironment(environment,agents);
FrameBuilder frameBuilder = new FrameBuilder();
frameBuilder.addComponent(display,FrameBuilder.TOP); fb.setSimulationCanvas(display);
frameBuilder.addComponent(statisticsCanvas,FrameBuilder.RIGHT); fb.setStatsCanvas(statisticsCanvas);
frameBuilder.buildWindow();
fb.buildWindow();
statisticsCanvas.updateValues(environment.getAgentsStatus()); statisticsCanvas.updateValues(environment.getAgentsStatus());
statisticsCanvas.repaint(); statisticsCanvas.repaint();
} }
......
package utils;
public record Pair<A, B>(A first, B second) {
public int hashCode() {
int hashFirst = first != null ? first.hashCode() : 0;
int hashSecond = second != null ? second.hashCode() : 0;
return (hashFirst + hashSecond) * hashSecond + hashFirst;
}
public boolean equals(Object other) {
if (other instanceof Pair otherPair) {
return
((this.first == otherPair.first ||
(this.first != null && otherPair.first != null &&
this.first.equals(otherPair.first))) &&
(this.second == otherPair.second ||
(this.second != null && otherPair.second != null &&
this.second.equals(otherPair.second))));
}
return false;
}
public String toString() {
return "(" + first + ", " + second + ")";
}
public A getFirst() {
return first;
}
public B getSecond() {
return second;
}
}
package view; package view;
import utils.Pair;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class FrameBuilder { public class FrameBuilder {
public final static int LEFT = 0; private JPanel simulation;
public final static int RIGHT = 1; private JPanel stats;
public final static int TOP = 2;
public final static int BOTTOM = 3;
private int windowWidth = 0;
private int windowHeight = 0;
private List<Pair<JComponent,Integer>> components;
public FrameBuilder() { public FrameBuilder() {
resetWindow(); resetWindow();
} }
public void resetWindow() { public void resetWindow() {
components = new ArrayList<>(); simulation = null;
stats = null;
} }
public void setSimulationCanvas(JPanel canvas) {
this.simulation = canvas;
}
public void addComponent(JComponent c,int p) { public void setStatsCanvas(JPanel canvas) {
var pair = new Pair<>(c,p); this.stats = canvas;
}
public void buildWindow() {
switch (p) { if (simulation == null || stats == null) {
case LEFT,RIGHT -> { throw new IllegalStateException("Cannot create windows : one of the canvas is not initialized.");
windowWidth+=c.getWidth();
if (c.getHeight()>windowHeight)
windowHeight = c.getHeight();
}
case TOP,BOTTOM -> {
windowHeight+=c.getHeight();
if (c.getWidth()>windowWidth)
windowWidth = c.getWidth();
}
} }
components.add(pair); Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
}
if (simulation.getWidth()+stats.getWidth() > screenDimension.getWidth() ||
simulation.getHeight() > screenDimension.getHeight()) {
System.err.println("WARNING : screen size is smaller than the simulation window. The simulation will " +
"still work but won't display properly.");
}
public void buildWindow() {
JFrame frame = new JFrame(); JFrame frame = new JFrame();
frame.setLayout(new java.awt.GridLayout()); JSplitPane panel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, simulation, stats);
panel.setDividerLocation(simulation.getWidth());
panel.setDividerSize(0);
frame.setSize(simulation.getWidth()+stats.getWidth(),simulation.getHeight());
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (Pair<JComponent,Integer> pair : components ) {
switch (pair.getSecond()) {
case TOP -> frame.add(pair.getFirst(),BorderLayout.NORTH);
case BOTTOM -> frame.add(pair.getFirst(),BorderLayout.SOUTH);
case LEFT -> frame.add(pair.getFirst(),BorderLayout.WEST);
case RIGHT -> frame.add(pair.getFirst(),BorderLayout.EAST);
default -> throw new IllegalStateException("Wrong position value");
}
}
frame.setSize(windowWidth,windowHeight);
frame.setResizable(false); frame.setResizable(false);
frame.setVisible(true); frame.setVisible(true);
} }
} }
...@@ -9,15 +9,11 @@ import java.util.HashMap; ...@@ -9,15 +9,11 @@ import java.util.HashMap;
public class StatisticsCanvas extends JPanel { public class StatisticsCanvas extends JPanel {
private final int canvasWidth;
private final int canvasHeight;
private HashMap<String,Integer> values; private HashMap<String,Integer> values;
private final int total; private final int total;
public StatisticsCanvas(int width,int height) { public StatisticsCanvas(int width,int height) {
this.setDoubleBuffered(false); this.setDoubleBuffered(false);
canvasHeight = height;
canvasWidth = width;
values = new HashMap<>(); values = new HashMap<>();
total = YamlReader.getParams().getPopulation(); total = YamlReader.getParams().getPopulation();
setSize(width, height); setSize(width, height);
...@@ -43,15 +39,14 @@ public class StatisticsCanvas extends JPanel { ...@@ -43,15 +39,14 @@ public class StatisticsCanvas extends JPanel {
int start = 0; int start = 0;
g.clearRect(0,0,getWidth(),getHeight()); g.clearRect(0,0,getWidth(),getHeight());
for (int i=0 ; i <values.keySet().size();i++) { for (int i=0 ; i <values.keySet().size();i++) {
String state = (String) values.keySet().toArray()[i]; String state = values.keySet().toArray()[i].toString();
int value = values.get(state); int value = values.get(state);
g.setColor(stringToColor(state)); g.setColor(stringToColor(state));
float height = ((float)value/total)*canvasHeight; float height = ((float)value/total)*getHeight();
g.fillRect(10,start,canvasWidth/4,start+(int)height); g.fillRect(10,start,getWidth()/4,start+(int)height);
start +=height; start +=height;
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
g.drawString(state + " : "+value,canvasWidth/2,canvasHeight/values.keySet().size()*(1+i)-100); g.drawString(state + " : "+value,getWidth()/2,getHeight()/values.keySet().size()*(1+i)-100);
} }
} }
} }
src/main/resources/output.png

51.7 KiB | W: | H:

src/main/resources/output.png

59 KiB | W: | H:

src/main/resources/output.png
src/main/resources/output.png
src/main/resources/output.png
src/main/resources/output.png
  • 2-up
  • Swipe
  • Onion skin
graphicalMode: false graphicalMode: true
incubationRate: 0.3 incubationRate: 0.3
infectionRate: 0.2 infectionRate: 0.75
looseImmunityRate: 0.09 looseImmunityRate: 0.008
recoveryRate: 0.6 recoveryRate: 0.1429
nbOfCycles: 300 nbOfCycles: 2000
nbOfPatientZero: 10 nbOfPatientZero: 1
population: 5000 population: 3000
seed: 120 seed: 120
size: 500 size: 1000
synchronousMode: true synchronousMode: false
timeBetweenCycles: 0 timeBetweenCycles: 0
infectionStacks : false infectionStacks : true
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment