001 package breakout; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import java.io.*; 006 import javax.swing.*; 007 import javax.swing.border.EtchedBorder; 008 import javax.swing.filechooser.*; 009 010 /** 011 * class GameFrame is the main application window. It contains 012 * the GameBoards in which the user plays the game and the 013 * buttons that control them. 014 * 015 * @see breakout.WorldState 016 * @see breakout.GameBoard 017 * @see breakout.NetworkSettings 018 * 019 * @author benmv@olin.edu 020 * @version <tt>$Id: GameFrame.java,v 1.2 2004/03/26 20:39:33 gus Exp $</tt> 021 */ 022 public class GameFrame extends JFrame implements WindowListener, ActionListener { 023 GameBoard top,bottom; 024 WorldState local,remote; 025 JFileChooser chooser; 026 BoardLoader loader; 027 Connector connect; 028 029 030 public GameFrame() { 031 this(new NetworkSettings()); 032 } 033 034 public GameFrame(NetworkSettings settings) { 035 super("Breakout"); 036 037 addWindowListener(this); 038 Container con = getContentPane(); 039 con.setLayout(new BorderLayout()); 040 041 boolean networked = settings.getState() != NetworkSettings.NETWORK_NONE; 042 043 JPanel boards = new JPanel(); 044 GridLayout grid = new GridLayout(); 045 grid.setColumns(1); grid.setRows(networked ? 2 : 1); 046 grid.setVgap(0); 047 boards.setLayout(grid);//new FlowLayout()); 048 049 if (networked) { 050 remote = new WorldState(networked); 051 top = new GameBoard(remote,true); 052 remote.setGui(top); 053 boards.add(top); 054 } 055 056 local = new WorldState(networked); 057 bottom = new GameBoard(local,false); 058 local.setGui(bottom); 059 loader = new BoardLoader(local); 060 boards.add(bottom); 061 062 if (networked) { 063 connect = new StudentConnector(local,remote,settings); 064 } 065 066 // build initial bumper 067 Dimension bsize = new Dimension(20,5); 068 Point bloc = new Point(World.MAXX/2-bsize.width/2, 069 World.MAXY-bsize.height); 070 local.addMouseAware(new ShooterBumper(bloc,bsize,local,20)); 071 072 073 con.add(boards,BorderLayout.CENTER); 074 075 JPanel buttons = new JPanel(); 076 buttons.setBorder(new EtchedBorder()); 077 078 JButton but; 079 if (networked) { 080 buttons.add(new StatusComponent(local,connect)); 081 but = new JButton("Connect"); 082 but.addActionListener(new ActionListener() { 083 public void actionPerformed(ActionEvent event) { 084 connect.connect(); 085 } 086 }); 087 buttons.add(but); 088 } 089 but = new JButton("Load"); 090 but.addActionListener(this); 091 buttons.add(but); 092 but = new JButton("Start"); 093 but.addActionListener(this); 094 buttons.add(but); 095 but = new JButton("Clear"); 096 but.addActionListener(this); 097 buttons.add(but); 098 but = new JButton("Quit"); 099 but.addActionListener(this); 100 buttons.add(but); 101 102 con.add(buttons,BorderLayout.SOUTH); 103 104 chooser = new JFileChooser("."); 105 chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { 106 public boolean accept(File f) { 107 if (f.isDirectory()) return true; 108 else if (f.getName().indexOf(".brd") > 0) return true; 109 else return false; 110 } 111 public String getDescription() { return "Board files (*.brd)"; } 112 }); 113 114 pack(); 115 if (networked) 116 setSize(400,470); 117 else 118 setSize(400,270); 119 show(); 120 } 121 122 public void add(BreakoutComponent bc) { 123 local.add(bc); 124 } 125 126 private void loadFile() { 127 int val = chooser.showOpenDialog(this); 128 if (val == JFileChooser.APPROVE_OPTION) { 129 File choice = chooser.getSelectedFile(); 130 try { 131 loader.load(choice); 132 } catch (BadBoardFormatException e) { 133 JOptionPane.showMessageDialog(this,"BadBoard: "+e.getMessage()); 134 } 135 } 136 } 137 138 public void actionPerformed(ActionEvent eve) { 139 if ("Load".equals(eve.getActionCommand())) { 140 loadFile(); 141 } else if ("Start".equals(eve.getActionCommand())) { 142 local.startPlaying(); 143 } else if ("Clear".equals(eve.getActionCommand())) { 144 local.killAll(); 145 } else if ("Quit".equals(eve.getActionCommand())) { 146 System.exit(0); 147 } 148 } 149 150 public void windowActivated(WindowEvent eve) { 151 } 152 153 public void windowClosed(WindowEvent eve) { 154 } 155 156 public void windowClosing(WindowEvent eve) { 157 System.exit(0); 158 } 159 160 public void windowDeactivated(WindowEvent eve) { 161 } 162 163 public void windowDeiconified(WindowEvent eve) { 164 } 165 166 public void windowIconified(WindowEvent eve) { 167 } 168 169 public void windowOpened(WindowEvent eve) { 170 } 171 172 } 173