import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
/**
* Graphical user interface used to display a grid
*
* @author Brendon Taylor
* @version 1.0
* @since 10/09/2011
*/
public class Grid implements ActionListener {
/* Used to store our menu */
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem fileMenuNewMenu;
private JMenuItem fileMenuNewGliderOption;
private JMenuItem fileMenuNewBlinkerOption;
private JMenuItem fileMenuNewToadOption;
private JMenuItem fileMenuNewSpaceshipOption;
//private JMenuItem fileMenuNewBaconOption;
private JMenuItem fileMenuExitOption;
/** Used as our main window */
private JFrame mainFrame;
/** A custom panel we used to draw the grid */
private DrawPanel mainGrid;
/** The size of the grid (assume the grid is square */
private int size;
/**
* Inner class we use to overwrite the paintComponent(Graphics g) method
*/
@SuppressWarnings("serial")
protected class DrawPanel extends JPanel {
/** The size of the grid in pixels (assuming the width and height are the same) */
private int size;
/** The number of rows/columns in the grid */
private int cols;
/** Inner structure used to store our grid */
private boolean[][] cells;
/**
* @param initSize The size of the grid in pixels (assuming the width and height are the same)
* @param initCols The number of rows/columns in the grid
*/
public DrawPanel(int initSize, int initCols) {
size = (initSize >= 0 ? initSize : 1000);
cols = initCols;
cells = new boolean[cols][cols];
setPreferredSize(new Dimension(size, size)); }
/**
* Used to update our grid
* @param newCells The new grid
*/
public void updatePanel(boolean[][] newCells) {
for (int row = 0; row < newCells.length; row++) {
for (int col = 0; col < newCells[0].length; col++)
cells[row][col] = newCells[row][col];
}
repaint();
}
/**
* Paints our grid on the panel
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int rectSize = size / cols;
for (int row = 0; row < cols; row++) {
for (int col = 0; col < cols; col++) {
g.setColor(cells[row][col] ? Color.BLACK : Color.WHITE);
g.fillRect(col * rectSize, row * rectSize, rectSize, rectSize);
g.setColor(Color.DARK_GRAY);
g.drawRect(col * rectSize, row * rectSize, rectSize, rectSize);
}
}
}
}
/**
* @param initSize The size of the grid in pixels (assuming the width and height are the same)
* @param initCols The number of rows/columns in the grid
*/
public Grid(int initSize, int initCols) {
size = initSize;
mainFrame = new JFrame("Game of life");
// Put scrollbars around our panel
mainGrid = new DrawPanel(size, initCols);
JScrollPane scrollBar = new JScrollPane(mainGrid, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollBar.setPreferredSize(new Dimension(size / 2, size / 2));
mainFrame.add(scrollBar);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.pack();
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
fileMenuNewMenu = new JMenu("New");
// Construct our file menu
fileMenuNewGliderOption = new JMenuItem("Glider Gun", 'F');
fileMenuNewGliderOption.addActionListener(this);
fileMenuNewGliderOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.CTRL_MASK));
fileMenuNewBlinkerOption = new JMenuItem("Pulsar");
fileMenuNewBlinkerOption.addActionListener(this);
fileMenuNewBlinkerOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.CTRL_MASK));
fileMenuNewToadOption = new JMenuItem("Gun Shoots Pulsar");
fileMenuNewToadOption.addActionListener(this);
fileMenuNewToadOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.CTRL_MASK));
fileMenuNewSpaceshipOption = new JMenuItem("Random Start");
fileMenuNewSpaceshipOption.addActionListener(this);
fileMenuNewSpaceshipOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_4, ActionEvent.CTRL_MASK));
// Add the menu options to the file menu
fileMenuNewMenu.add(fileMenuNewGliderOption);
fileMenuNewMenu.add(fileMenuNewBlinkerOption);
fileMenuNewMenu.add(fileMenuNewToadOption);
fileMenuNewMenu.add(fileMenuNewSpaceshipOption);
//fileMenuNewMenu.add(fileMenuNewBaconOption);
fileMenuExitOption = new JMenuItem("Exit");
fileMenuExitOption.addActionListener(this);
fileMenuExitOption.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
fileMenu.add(fileMenuNewMenu);
fileMenu.addSeparator();
fileMenu.add(fileMenuExitOption);
menuBar.add(fileMenu);
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
}
/**
* Used to handle a menu click event
*/
@Override
public void actionPerformed(ActionEvent e) {
//GameOfLifeDriver.clear();
if (e.getActionCommand().equals("Glider Gun"))
GameOfLifeDriver.gliderGun();
else if (e.getActionCommand().equals("Pulsar"))
GameOfLifeDriver.pulsar();
else if (e.getActionCommand().equals("Gun Shoots Pulsar"))
GameOfLifeDriver.gunShootsPulsar();
else if (e.getActionCommand().equals("Random Start"))
GameOfLifeDriver.randomStart();
//else if (e.getActionCommand().equals("Bacon"))
//GameOfLifeDriver.bacon();
else if (e.getActionCommand().equals("Exit"))
System.exit(0);
}
/**
* @see DrawPanel#updatePanel(boolean[][])
*/
public void updateGrid(boolean[][] cells) {
mainGrid.updatePanel(cells);
}
}
- 1
- 2
前往页