import java.awt.*; import java.io.*; import java.util.*; import java.lang.Math; import java.awt.image.*; import java.awt.event.*; import javax.swing.*; class studentViewer extends JPanel { static int height, width; static int numParticles, numWalls; static double[] times; static int[][][] coordinates; static int[] walls; static Color[][] particleColors; static int timeStep = 0; static boolean running = false; static double lastTime = 0; //coordinates[current time step][current particle][x or y coordinate] static int dt = 1; BufferedImage img = new BufferedImage(1000,1000,BufferedImage.TYPE_INT_RGB); Graphics buffer = img.getGraphics(); public studentViewer() { height = img.getHeight(); width = img.getWidth(); String fileName = "data.txt"; read(fileName); buffer.setFont(new Font("Monospaced",Font.PLAIN,15)); addKeyListener(new KeyListener()); setFocusable(true); new javax.swing.Timer(0, new Listener() ).start(); } static void read(String fileName) { FileReader infile = null; try { infile = new FileReader(fileName); } catch(FileNotFoundException ex) { System.out.println("File does not exist."); } Scanner f = new Scanner(infile); numParticles = Integer.parseInt(f.next()); numWalls = Integer.parseInt(f.next()); int numTimeSteps = Integer.parseInt(f.next()); times = new double[numTimeSteps]; coordinates = new int[numTimeSteps][numParticles][2]; walls = new int[numWalls * 2]; particleColors = new Color[numTimeSteps][numParticles]; //setColors(); for(int x = 0; x < numWalls * 2; x ++) { walls[x] = Integer.parseInt(f.next()); } for(int x = 0; x < numTimeSteps; x++) { times[x] = Double.parseDouble(f.next()); for(int y = 0; y < numParticles; y++) { int clr = (int)(Double.parseDouble(f.next())); if(clr > 255) clr = 255; //System.out.println(x + " " + y + " " + clr); particleColors[x][y] = new Color(clr, 255-clr, 0); } for(int y = 0; y < numParticles; y++) { coordinates[x][y][0] = (int)(Double.parseDouble(f.next()) * width); } for(int y = 0; y < numParticles; y++) { coordinates[x][y][1] = height - (int)(Double.parseDouble(f.next()) * height); } } } static void setColors() { for(int loop = 0;loop < numParticles;loop++) { particleColors[0][loop] = Color.green; } } public class Listener implements ActionListener { public void actionPerformed(ActionEvent e) { if(timeStep > times.length - 1) timeStep = times.length - 1; if(running) { if(lastTime == 0) lastTime = System.currentTimeMillis(); else { if(timeStep < times.length - 1) { double millisSince = System.currentTimeMillis() - lastTime; double spts = 1 / 4; if(millisSince >= spts * 1000 * times[timeStep + 1] - times[timeStep]) lastTime = System.currentTimeMillis(); if(millisSince >= spts * 1000 * times[timeStep + 1] - times[timeStep])//spts * 1000 = millis per timestep { timeStep += dt; if(timeStep > times.length - 1) { timeStep = times.length - 1; dt = 0; } if(timeStep < 0) { timeStep = 0; dt = 0; } } } } } else lastTime = 0; drawStuff(); repaint(); } } public class KeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if(!running) { if(key == 37) { timeStep--; if(timeStep < 0) timeStep = 0; } if(key == 39) { timeStep++; if(timeStep > times.length - 1) timeStep = times.length - 1; } } if(key == 38) { dt++; } if(key == 40) { dt--; } if(key == 32) { running = !running; } } } public void drawStuff() { buffer.setColor(Color.white); buffer.fillRect(0,0,width,height); buffer.setColor(Color.black); for(int x = 0; x < numWalls * 2; x += 2) { buffer.fillRect(walls[x] * 10 - 5, walls[x + 1] * 10 - 5, 10, 10); } for(int a = 0; a < numParticles; a++) { buffer.setColor(particleColors[timeStep][a]); if(particleColors[timeStep][a].getRed() != 0) System.out.println("Blarg!"); buffer.fillRect(coordinates[timeStep][a][0]-5,coordinates[timeStep][a][1]-5, 10, 10); } fillGrid(); buffer.setColor(Color.white); buffer.fillRect(width - 210, 0, 210, 20); buffer.setColor(Color.black); buffer.drawString("Time Step = " + times[timeStep], width - 200, 15); } public void fillGrid() { buffer.setColor(Color.gray); for(int x = 5; x < 1000; x += 10) { buffer.drawLine(x, 0, x, 1000); buffer.drawLine(0, x, 1000, x); } } public void paint(Graphics g1) { Graphics2D g2 = (Graphics2D)g1; g2.drawImage(img,0,0,getWidth(),getHeight(),null); } } public class studentDriver { public static void main(String[] args) { JFrame frame = new JFrame("Student Traffic Simulation"); frame.setSize(1000,1000); frame.setLocation(0,3); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new studentViewer()); frame.setVisible(true); } }