// UserInterface.java // Description: User Interface for Terriers and Squirrels // Author: Chris Wilcox // Date: 4/28/2015 package myPackage; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import java.awt.Image; import java.util.Timer; import java.util.TimerTask; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.border.Border; import javax.swing.border.LineBorder; public class UserInterface extends JFrame { private static final long serialVersionUID = 1L; // User interface private JPanel topPanel; // Points area private JPanel bottomPanel; // Game area private Image iconTerrier, iconSquirrel, iconMunch; private JLabel message; private JButton buttons[][]; private Font font; private static Timer timer; // Game related private static String filename; private static GameEngine game; private static int counter = 0; private static int height; private static int width; private static char board[][]; public UserInterface(int height, int width) { // Look and feel try { UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } // Setup message font = new Font("Arial", Font.PLAIN, 24); message = new JLabel(""); message.setFont(font); message.setForeground(new Color(0xFFFFFF)); // Top panel for message topPanel = new JPanel(); topPanel.add(message); topPanel.setBackground(new Color(0x0076A3)); // Bottom panel for board bottomPanel = new JPanel(); bottomPanel.setLayout(new GridLayout(height, width, 0, 0)); add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); // Split panel JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT); sp.setEnabled(false); sp.setDividerSize(0); sp.add(topPanel); sp.add(bottomPanel); add(sp, BorderLayout.CENTER); // Load images ImageIcon icon0 = new ImageIcon("iconTerrier.png"); Image image0 = icon0.getImage(); iconTerrier = image0.getScaledInstance(480/width, 480/width, Image.SCALE_DEFAULT); ImageIcon icon1 = new ImageIcon("iconSquirrel.png"); Image image1 = icon1.getImage(); iconSquirrel = image1.getScaledInstance(480/width, 480/width, Image.SCALE_DEFAULT); ImageIcon icon2 = new ImageIcon("iconMunch.png"); Image image2 = icon2.getImage(); iconMunch = image2.getScaledInstance(480/width, 480/width, Image.SCALE_DEFAULT); // Build panel of buttons buttons = new JButton[height][width]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { // Initialize and add button JButton button = new JButton(); buttons[row][col] = button; Border border = new LineBorder(Color.black, 1); button.setOpaque(true); button.setBackground(new Color(0x006600)); button.setBorder(border); button.setName(row+","+col); bottomPanel.add(button); } } // Window setup setSize(530, 530); setTitle("Terriers and Squirrels"); setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setAlwaysOnTop(true); } public static void main(String[] args) { filename = args[0]; SwingUtilities.invokeLater(new Runnable() { public void run() { // Instantiate user interface game = new GameEngine(filename); board = game.data(); height = board.length; width = board[0].length; UserInterface ui = new UserInterface(height, width); ui.setVisible(true); ui.redrawBoard(); timer = new Timer(); timer.schedule(ui.new RedrawTask(), 1000, 1000); // every 1.0 seconds } }); } public void redrawBoard() { // Update Generation message.setText("Terriers and Squirrels, Move: " + counter++); // Redraw board for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { switch (board[i][j]) { case 'T': buttons[i][j].setIcon(new ImageIcon(iconTerrier)); break; case 'S': buttons[i][j].setIcon(new ImageIcon(iconSquirrel)); break; case 'A': buttons[i][j].setIcon(new ImageIcon(iconMunch)); board[i][j] = 'T'; // make it a terrier again break; default: buttons[i][j].setIcon(null); } } } } // Scheduled task to redraw screen class RedrawTask extends TimerTask { public void run() { // Advance game boolean done = game.next(); // Redraw board redrawBoard(); // Stop timer, if done if (done) timer.cancel(); } } }