public static void main(String[] argv) { // Parse command-line args if (argv.length != 0) { usage(); // System.exit(-1); } Thread visThread = new Thread(new Visualizer()); visThread.start(); // Poll for the user to end the simulation Scanner in = new Scanner(System.in); while (visThread.isAlive()) { String input = in.nextLine(); if (input.equals("exit")) { finishUp(); System.exit(0); } else if (input.equals("spawn")) { input = in.nextLine(); for (int i = 0; i < Integer.parseInt(input); i++) fishtank.add(); } else if (input.equals("print")) { fishtank.printState(); } else if (input.equals("hyperspeed")) { fishtank.toggleHyperspeed(); } } }
private FishState drawFish() { FishState fishUnderMouse = null; for (FishState fs : state.getFish()) { if (fs.isAlive()) { Vector pos = fs.getPosition(); // Draw tail and outline according to speed int tailBrightness = (int) ((double) fs.getSpeed() / Rules.MAX_SPEED * 255); bufferGraphics.setColor(Color.BLACK); bufferGraphics.drawLine( (int) pos.x, (int) pos.y, (int) (pos.x - fs.getRudderVector().x * fs.getRadius() * 2), (int) (pos.y - fs.getRudderVector().y * fs.getRadius() * 2)); bufferGraphics.fillOval( (int) pos.x - fs.getRadius(), (int) pos.y - fs.getRadius(), 2 * fs.getRadius(), 2 * fs.getRadius()); bufferGraphics.setColor(fishtank.getColor(fs.fish_id)); bufferGraphics.fillOval( (int) pos.x - fs.getRadius() + 1, (int) pos.y - fs.getRadius() + 1, 2 * fs.getRadius() - 2, 2 * fs.getRadius() - 2); } if (fs.getPosition().minus(mousePosition).length() < fs.getRadius()) { fishUnderMouse = fs; } } return fishUnderMouse; }
public void run() { // Set up visualization window this.setVisible(true); this.setSize(buffer.getWidth(), buffer.getHeight()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Start the engine engineThread.start(); // Repeatedly display the simulation long nextState = 0; while (engineThread.isAlive()) { state = fishtank.getState(nextState); repaint(); nextState = state.seqID; } }
public static void finishUp() { fishtank.printFinalStats(); }