//Tasha Wallage //Period 7 import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.util.*; import javax.swing.Timer; import java.awt.image.*; import javax.swing.JOptionPane; public class Sim extends JPanel implements ActionListener { private JOptionPane pane; private int worldSize = 300; private BufferedImage bfi; private Graphics g; private JFrame frame; private Timer t; private World wd; private Graph graph; private JLabel time; private JMenuItem pause, unpause, start, end, exit, changeSpeed; private int opop = 0, fpop = 0, ppop = 0, count=0, speed=100; public Sim(JFrame frame) { this.frame = frame; frame.setLocation(50, 50); frame.setSize(worldSize, worldSize); pane = new JOptionPane(); JFrame gFrame= new JFrame("Graph"); gFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); graph = new Graph(gFrame); gFrame.setContentPane(graph); gFrame.setVisible(true); bfi = new BufferedImage(worldSize, worldSize, BufferedImage.TYPE_INT_RGB); g = bfi.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, worldSize, worldSize); wd = new World(); wd.setLimit(worldSize); t = new Timer(speed, this); createLayout(); g.setColor(Color.black); g.fillRect(0, 0, worldSize, worldSize); } //calls step() on the World and then draws the Species at their new locations public void actionPerformed(ActionEvent e) { if(e.getSource() == t) { count++; double year = (double)count/365.25; time.setText(year+""); graph.add(count,(int)(wd.defAve()*50)-50); HashMap specLocs = wd.getHMS(); if(specLocs.size()==0) { t.stop(); pane.showMessageDialog(null, "ALL SPECIES DEAD!\n\nLife present for "+((double)count)/365.25+"years."); return; } g.setColor(Color.black); g.fillRect(0, 0, worldSize, worldSize); wd.step(); Iterator it = specLocs.keySet().iterator(); g.setColor(Color.green.darker()); while(it.hasNext()){ Location loc = (Location)it.next(); Species s = specLocs.get(loc); String t = s.getName(); if(t.equals("Food")){ g.setColor(Color.green.darker()); g.fill3DRect(loc.getX(), loc.getY(), 10, 10, true); } else if(t.equals("Organism")){ g.setColor(Color.blue); g.fillOval(loc.getX(), loc.getY(), 8, 8); } else if(t.equals("Predator")){ g.setColor(Color.red.darker()); g.fillOval(loc.getX(), loc.getY(), 10, 10); } } repaint(); } else if(e.getSource() == start) { starts(); } else if(e.getSource() == pause) { if(opop==0 && ppop==0 && fpop==0){ pane.showMessageDialog(null, "No simulation to unpause.\nSelect Start to begin a simulation."); return; } t.stop(); } else if(e.getSource() == unpause) { if(opop==0 && ppop==0 && fpop==0){ pane.showMessageDialog(null, "No simulation to unpause.\nSelect Start to begin a simulation."); return; } t.start(); this.requestFocus(); } else if(e.getSource() == end) { if(opop==0 && ppop==0 && fpop==0){ pane.showMessageDialog(null, "No simulation to unpause.\nSelect Start to begin a simulation."); return; } t.stop(); int x = pane.showConfirmDialog(null, "Are you sure you want to quit?", "Terminating Program", pane.YES_OPTION); if(x!=0) { t.start(); return; } pane.showMessageDialog(null, "Simulation Terminated\n\nLife present for "+((double)count)/365.25+" years." +"\n\nSpecies Remaining:\nFood: "+wd.getF()+"\nOrganisms: " +wd.getO()+"\nPredators: "+wd.getP()); opop = fpop = ppop = 0; count = 0; time.setText("0"); wd = new World(); graph.reset(); g.setColor(Color.black); g.fillRect(0, 0, worldSize, worldSize); repaint(); } else if(e.getSource() == exit) { System.exit(0); } else if(e.getSource() == changeSpeed) { changeSpeed(); } } //adds the menu bar at the top that can pause and unpause the simulation private void createLayout() { setLayout(new BorderLayout()); JMenuBar menuBar = new JMenuBar(); menuBar.setOpaque(true); menuBar.setBackground(Color.black); menuBar.setForeground(Color.red); JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); file.setOpaque(true); file.setBackground(Color.black); file.setForeground(Color.red); menuBar.add(file); exit = new JMenuItem("Exit Simulator"); file.setMnemonic(KeyEvent.VK_Q); exit.addActionListener(this); exit.setOpaque(true); exit.setBackground(Color.black); exit.setForeground(Color.red); file.add(exit); JMenu sim = new JMenu("Simulation"); sim.setMnemonic(KeyEvent.VK_S); sim.setOpaque(true); sim.setBackground(Color.black); sim.setForeground(Color.red); menuBar.add(sim); start = new JMenuItem("Start"); sim.setMnemonic(KeyEvent.VK_B); start.addActionListener(this); start.setOpaque(true); start.setBackground(Color.black); start.setForeground(Color.red); sim.add(start); end = new JMenuItem("End"); sim.setMnemonic(KeyEvent.VK_E); end.addActionListener(this); end.setOpaque(true); end.setBackground(Color.black); end.setForeground(Color.red); sim.add(end); pause = new JMenuItem("Pause"); sim.setMnemonic(KeyEvent.VK_P); pause.addActionListener(this); pause.setOpaque(true); pause.setBackground(Color.black); pause.setForeground(Color.red); sim.add(pause); unpause = new JMenuItem("Unpause"); sim.setMnemonic(KeyEvent.VK_U); unpause.addActionListener(this); unpause.setOpaque(true); unpause.setBackground(Color.black); unpause.setForeground(Color.red); sim.add(unpause); changeSpeed = new JMenuItem("Change Speed"); sim.setMnemonic(KeyEvent.VK_C); changeSpeed.addActionListener(this); changeSpeed.setOpaque(true); changeSpeed.setBackground(Color.black); changeSpeed.setForeground(Color.red); sim.add(changeSpeed); sim.setOpaque(true); sim.setBackground(Color.black); sim.setForeground(Color.red); menuBar.add(sim); JMenuBar timeCounter = new JMenuBar(); timeCounter.setOpaque(true); timeCounter.setBackground(Color.black); timeCounter.setForeground(Color.red); JLabel label = new JLabel("Year: "); label.setOpaque(true); label.setBackground(Color.black); label.setForeground(Color.red); timeCounter.add(label); time = new JLabel("0"); time.setOpaque(true); time.setBackground(Color.black); time.setForeground(Color.red); timeCounter.add(time); this.add(timeCounter, BorderLayout.SOUTH); this.add(menuBar, BorderLayout.NORTH); } private void changeSpeed() { String s = ""; while(!isNum(s)) s = pane.showInputDialog("Input new Timer delay (in milliseconds)."); int spd = Integer.parseInt(s); t.setDelay(spd); } public void starts() { fpop = setFood(); opop = setOrg(); ppop = setPred(); for(int k = 0; k< opop; k++) { wd.add(new Organism(wd, worldSize)); } for(int k = 0; k< fpop; k++) { wd.add(new Food(wd, worldSize)); } for(int k = 0; k< ppop; k++) { wd.add(new Predator(wd, worldSize)); } t.start(); } public int setFood() { return getNum("much food"); } public int setOrg() { return getNum("many organisms"); } public int setPred() { return getNum("many predators"); } public int getNum(String str) { String s = ""; while(!isNum(s)) s = pane.showInputDialog("Start with how "+str+"?"); int x = Integer.parseInt(s); return x; } public boolean isNum(String s) { if(s=="" || s==null) return false; for(int k=0; k