package uu.mal.skate.view; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.filechooser.FileNameExtensionFilter; import org.apache.commons.math3.linear.RealMatrix; import uu.mal.skate.model.Options; import uu.mal.skate.model.Simulation; public class ViewPort { private final JFrame frame; private SkatingSurface surface; private ChartPane charts; private Simulation sim; private JMenuItem startSimMenuItem, exportSimMenuItem, pauseSimMenuItem, resumeSimMenuItem; private Options options; public ViewPort() { JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); menuBar.add(fileMenu); JMenu simMenu = new JMenu("Simulation"); menuBar.add(simMenu); JMenuItem exitMenuItem = new JMenuItem("Exit"); exitMenuItem.addActionListener(new ExitAction()); fileMenu.add(exitMenuItem); JMenuItem newSimMenuItem = new JMenuItem("New"); newSimMenuItem.addActionListener(new NewSimAction()); simMenu.add(newSimMenuItem); startSimMenuItem = new JMenuItem("Start"); startSimMenuItem.setEnabled(false); startSimMenuItem.addActionListener(new StartSimAction()); simMenu.add(startSimMenuItem); pauseSimMenuItem = new JMenuItem("Pause"); pauseSimMenuItem.setEnabled(false); pauseSimMenuItem.addActionListener(new PauseSimAction()); simMenu.add(pauseSimMenuItem); resumeSimMenuItem = new JMenuItem("Resume"); resumeSimMenuItem.setEnabled(false); resumeSimMenuItem.addActionListener(new ResumeSimAction()); simMenu.add(resumeSimMenuItem); exportSimMenuItem = new JMenuItem("Export"); exportSimMenuItem.setEnabled(false); exportSimMenuItem.addActionListener(new ExportAction()); simMenu.add(exportSimMenuItem); frame = new JFrame("Skating simulator"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setJMenuBar(menuBar); frame.pack(); frame.setVisible(true); } public void update(long iteration, RealMatrix propensities) { surface.repaint(); charts.update(iteration, propensities); } public void setupSim() { startSimMenuItem.setEnabled(true); exportSimMenuItem.setEnabled(false); pauseSimMenuItem.setEnabled(false); resumeSimMenuItem.setEnabled(false); if(surface != null) frame.getContentPane().remove(surface); if(charts != null) frame.getContentPane().remove(charts); surface = new SkatingSurface(sim); charts = new ChartPane(sim); frame.getContentPane().setLayout(new BorderLayout()); frame.getContentPane().add(surface, BorderLayout.LINE_START); frame.getContentPane().add(charts, BorderLayout.LINE_END); frame.pack(); } public static void main(String[] args) { new ViewPort(); } private class ExportAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { final JFileChooser fc = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG", "png"); fc.setFileFilter(filter); if (fc.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) { BufferedImage im = new BufferedImage(charts.getWidth(), charts.getHeight(), BufferedImage.TYPE_INT_ARGB); charts.paint(im.getGraphics()); File fileToSave = fc.getSelectedFile(); if(!fileToSave.getName().endsWith(".png")){ fileToSave = new File(fileToSave.toString() + ".png"); } try { ImageIO.write(im, "png", fileToSave); } catch (IOException e1) { e1.printStackTrace(); } } } } private class ExitAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if(sim != null) { sim.stop(); } frame.dispose(); System.exit(0); } } private class StartSimAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if(sim != null) { sim.start(); startSimMenuItem.setEnabled(false); pauseSimMenuItem.setEnabled(true); exportSimMenuItem.setEnabled(true); } } } private class PauseSimAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if(sim != null) { sim.pause(); resumeSimMenuItem.setEnabled(true); pauseSimMenuItem.setEnabled(false); } } } private class ResumeSimAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if(sim != null) { sim.resume(); resumeSimMenuItem.setEnabled(false); pauseSimMenuItem.setEnabled(true); } } } private class NewSimAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { OptionsDialog od = new OptionsDialog(frame, options); if(od.getAction() == OptionsDialog.ACTION_START) { if(sim != null) { sim.stop(); } sim = null; options = od.getOptions(); sim = new Simulation(options, ViewPort.this); setupSim(); } } } }