package uu.mal.skate.view; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; import uu.mal.skate.model.Agent; import uu.mal.skate.model.Options; import uu.mal.skate.model.Simulation; public class SkatingSurface extends JPanel { private static final long serialVersionUID = 1L; private final Options options; public static final Color BACKGROUND_COLOR = Color.WHITE; public static final Color GRID_COLOR = Color.LIGHT_GRAY; public static final double GRID_SIZE = 50; private Agent[] agents; public SkatingSurface(Simulation sim) { this.agents = sim.getAgents(); this.options = sim.getOptions(); setPreferredSize(new Dimension(options.torusWidth, options.torusHeight)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int w = options.torusWidth; int h = options.torusHeight; g.setColor(BACKGROUND_COLOR); g.clearRect(0, 0, w, h); g.setColor(GRID_COLOR); int nrOfVerticalLines = (int) (w / GRID_SIZE); int nrOfHorizontalLines = (int) (h / GRID_SIZE); for(int i = 1; i < nrOfHorizontalLines; i ++) { int x = (int) (i * GRID_SIZE); g.drawLine(0, x, w, x); } for(int i = 1; i < nrOfVerticalLines; i ++) { int y = (int) (i * GRID_SIZE); g.drawLine(y, 0, y, h); } if(agents != null) for(Agent a : agents) { a.draw(g); } } }