/** * WinEnv.java * * @author Zuo Yan * @version 1.2 */ package world; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A WinEnv contains windowing and panel code.
* */ public class WinEnv { private JFrame frame; // The window private SimPanel panel; // The main panel private JPanel buttons; // Panel holding the buttons World theWorld; // The World used to handle callbacks /** * Create our window and buttons and registers the world. * * @param world the World that this WinEnv is used for */ public WinEnv(World world) { this.theWorld = world; frame = new JFrame("CSE143 Aerial View"); // Create a frame with given title frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Automatically exit on close // Create buttons and add it to the frame buttons = createButtons(); frame.getContentPane().add(buttons, BorderLayout.EAST); // Create the main panel for drawing panel = new SimPanel(this.theWorld); panel.addKeyListener(this.theWorld); frame.getContentPane().add(panel, BorderLayout.CENTER); // Set the default size of the entire frame (buttons panel + main panel) frame.setSize(600, 500); frame.setResizable(false); // Display our frame with the main panel in focus frame.show(); panel.requestFocus(); } /** * Gets the size of the main panel * * @return the size of the work panel */ public Dimension getPanelSize() { return panel.getSize(); } /** * Gets the main panel * * @return the work panel */ public JPanel getPanel() { return panel; } /** * Redraws the entire main content panel */ public void redraw() { panel.repaint(0, 0, 0, panel.getSize().width, panel.getSize().height); } /** * Creates all the buttons and puts them in a panel. * Buttons events are handled by the world * * @return the panel with buttons created */ private JPanel createButtons() { JPanel jpane = new JPanel(); jpane.setLayout(new BoxLayout(jpane, BoxLayout.Y_AXIS)); JButton button = null; // Start button button = new JButton("Start"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { WinEnv.this.theWorld.start(); panel.requestFocus(); // Give the focus to the main panel } }); jpane.add(button); button.setAlignmentX(Component.CENTER_ALIGNMENT); // Stop button button = new JButton("Stop"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { WinEnv.this.theWorld.stop(); } }); jpane.add(button); button.setAlignmentX(Component.CENTER_ALIGNMENT); // Exit button button = new JButton("Exit"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); button.setAlignmentX(Component.CENTER_ALIGNMENT); jpane.add(button); return jpane; } /** * Our main panel class simply contains a white panel that * always requests to have the focus */ private class SimPanel extends JPanel implements FocusListener{ final Color bg = Color.white; private World aWorld; /** * Creates the panel * * @param world the world that tells us what to draw */ public SimPanel(World world) { super(); setBackground(bg); this.aWorld = world; } /** Ask the world to draw itself. */ public void paintComponent(Graphics g) { super.paintComponent(g); this.aWorld.draw((Graphics2D)g); } /** Nothing to do, but required by the interface. */ public void focusGained(FocusEvent e){} /** Lost the focus -- try to get it back. */ public void focusLost(FocusEvent e){ if (!e.isTemporary()) requestFocus(); } } }