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

improved display

parent 5130f6f9
No related branches found
No related tags found
No related merge requests found
......@@ -34,17 +34,18 @@ public class SEIRS_SMA implements SMA{
private StatisticsCanvas statisticsCanvas;
private DisplaySquaredEnvironment display;
private Random r;
private FrameBuilder fb = new FrameBuilder();
private HashMap<String,Integer> stats;
private void initGraphics() {
statisticsCanvas = new StatisticsCanvas(500,500);
statisticsCanvas = new StatisticsCanvas(300,parameters.getSize());
display = new DisplaySquaredEnvironment(environment,agents);
FrameBuilder frameBuilder = new FrameBuilder();
frameBuilder.addComponent(display,FrameBuilder.TOP);
frameBuilder.addComponent(statisticsCanvas,FrameBuilder.RIGHT);
frameBuilder.buildWindow();
fb.setSimulationCanvas(display);
fb.setStatsCanvas(statisticsCanvas);
fb.buildWindow();
statisticsCanvas.updateValues(environment.getAgentsStatus());
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;
import utils.Pair;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class FrameBuilder {
public final static int LEFT = 0;
public final static int RIGHT = 1;
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;
private JPanel simulation;
private JPanel stats;
public FrameBuilder() {
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) {
var pair = new Pair<>(c,p);
public void setStatsCanvas(JPanel canvas) {
this.stats = canvas;
}
public void buildWindow() {
switch (p) {
case LEFT,RIGHT -> {
windowWidth+=c.getWidth();
if (c.getHeight()>windowHeight)
windowHeight = c.getHeight();
}
case TOP,BOTTOM -> {
windowHeight+=c.getHeight();
if (c.getWidth()>windowWidth)
windowWidth = c.getWidth();
}
if (simulation == null || stats == null) {
throw new IllegalStateException("Cannot create windows : one of the canvas is not initialized.");
}
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();
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);
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.setVisible(true);
}
}
......@@ -9,15 +9,11 @@ import java.util.HashMap;
public class StatisticsCanvas extends JPanel {
private final int canvasWidth;
private final int canvasHeight;
private HashMap<String,Integer> values;
private final int total;
public StatisticsCanvas(int width,int height) {
this.setDoubleBuffered(false);
canvasHeight = height;
canvasWidth = width;
values = new HashMap<>();
total = YamlReader.getParams().getPopulation();
setSize(width, height);
......@@ -43,15 +39,14 @@ public class StatisticsCanvas extends JPanel {
int start = 0;
g.clearRect(0,0,getWidth(),getHeight());
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);
g.setColor(stringToColor(state));
float height = ((float)value/total)*canvasHeight;
g.fillRect(10,start,canvasWidth/4,start+(int)height);
float height = ((float)value/total)*getHeight();
g.fillRect(10,start,getWidth()/4,start+(int)height);
start +=height;
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
infectionRate: 0.2
looseImmunityRate: 0.09
recoveryRate: 0.6
nbOfCycles: 300
nbOfPatientZero: 10
population: 5000
infectionRate: 0.75
looseImmunityRate: 0.008
recoveryRate: 0.1429
nbOfCycles: 2000
nbOfPatientZero: 1
population: 3000
seed: 120
size: 500
synchronousMode: true
size: 1000
synchronousMode: false
timeBetweenCycles: 0
infectionStacks : false
\ No newline at end of file
infectionStacks : true
\ 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