001 package antichess.viewgui; 002 003 import java.awt.Container; 004 import java.awt.GridBagConstraints; 005 import java.awt.GridBagLayout; 006 import java.awt.GridLayout; 007 import java.awt.Image; 008 import java.awt.Insets; 009 import java.awt.event.ActionEvent; 010 import java.awt.event.ActionListener; 011 import java.util.ArrayList; 012 013 import javax.imageio.ImageIO; 014 import javax.swing.BorderFactory; 015 import javax.swing.ButtonGroup; 016 import javax.swing.JButton; 017 import javax.swing.JComboBox; 018 import javax.swing.JFrame; 019 import javax.swing.JLabel; 020 import javax.swing.JPanel; 021 import javax.swing.JRadioButton; 022 023 import antichess.ControllerMaster; 024 import antichess.GameDescriptor; 025 import antichess.Player; 026 027 /** 028 * NewGameWindow handles the creation of new games. 029 * 030 * A NewGameWindow is an extension of JFrame that contains widgets for 031 * selecting the options that can describe a New Game of Chess or Antichess. 032 * This options include: 033 * Game Type: Chess or Antichess 034 * Player Type for Black and White: Human or AI 035 * Player Time for Black and White: Positive values or untimed 036 * 037 * From these the NewGameWindow tells a ControllerMaster to start a new game. 038 * 039 * @author nlharr 040 * @specfield master //the controller master that starts the new game 041 * @specfield gameType 042 * @specfield whiteType 043 * @specfield blackType 044 * @specfield whiteTime 045 * @specfield blackTime 046 */ 047 048 049 public class NewGameWindow extends JFrame 050 implements ActionListener { 051 //Fields 052 private ControllerMaster master; 053 054 //Abstraction Function 055 //master = master 056 //gameType is stored in chessButton and antichessButton 057 //whiteTime is stored in whiteTime 058 //blackTime is stored in blackTime 059 //whiteType is stored in whitePlayerList 060 //blackType is stored in blackPlayerList 061 062 //Representation Invariants 063 //master != null 064 065 private void checkRep(){ 066 if (master == null) throw new RuntimeException("master is null"); 067 } 068 069 070 private static final String[] timeList = {"Untimed", "0:30", "1:00", "2:00", "4:00", "5:00", "10:00", "15:00"}; 071 072 073 //private constructor so no public one exists 074 private NewGameWindow(String name, ControllerMaster master){ 075 super(name); 076 this.master = master; 077 078 if (master == null){ 079 throw new RuntimeException("master is null"); 080 } 081 } 082 083 /** 084 * Creates and displays a NewGameWindow with a master specified by 085 * master. 086 */ 087 public static void createNewGameWindow(ControllerMaster master){ 088 NewGameWindow window = new NewGameWindow("New Game", master); 089 window.setContentPane(window.getContent()); 090 window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 091 window.setVisible(true); 092 //window.setSize(220, 260); 093 try{ 094 Image mainIcon = ImageIO.read(ControllerMaster.class.getResource("images/antichessicon.bmp")); 095 window.setIconImage(mainIcon); 096 }catch (Exception ex){} 097 window.pack(); 098 window.setResizable(true); 099 window.setVisible(true); 100 } 101 102 103 //Some fields we need to get information from 104 JButton createNewGameButton; 105 JButton cancelButton; 106 107 JComboBox whiteTime; 108 JComboBox blackTime; 109 JComboBox whitePlayerList; 110 JComboBox blackPlayerList; 111 112 JRadioButton antichessButton; 113 JRadioButton chessButton; 114 private Container getContent(){ 115 //initializes panels that contain the options for white player 116 117 JLabel whiteLabel = new JLabel("<html><Strong><b>White: </b></Strong></html>"); 118 119 JPanel gameType = new JPanel(); 120 121 antichessButton = new JRadioButton("Antichess"); 122 chessButton = new JRadioButton("Chess"); 123 ButtonGroup typeGroup = new ButtonGroup(); 124 typeGroup.add(antichessButton); 125 typeGroup.add(chessButton); 126 antichessButton.setSelected(true); 127 128 gameType.add(antichessButton); 129 gameType.add(chessButton); 130 131 String[] playerStrings = { "Human", "AI"}; 132 whitePlayerList = new JComboBox(playerStrings); 133 whitePlayerList.setSelectedIndex(0); 134 whitePlayerList.addActionListener(this); 135 136 137 138 whiteTime = new JComboBox(timeList); 139 140 JPanel whitePlayerInfo = new JPanel(); 141 //adds all the options for the White player 142 whitePlayerInfo.add(whiteLabel); 143 whitePlayerInfo.add(whitePlayerList); 144 whitePlayerInfo.add(whiteTime); 145 146 whitePlayerInfo.setBorder(BorderFactory.createEtchedBorder()); 147 148 149 //initializes panels that contain the options for black player 150 JPanel blackPlayerInfo = new JPanel(); 151 152 JLabel blackLabel = new JLabel("<html><Strong><b>Black: </b></Strong></html>"); 153 154 blackPlayerList = new JComboBox(playerStrings); 155 blackPlayerList.setSelectedIndex(0); 156 blackPlayerList.addActionListener(this); 157 158 blackTime = new JComboBox(timeList); 159 160 161 //adds all the options for the black player 162 blackPlayerInfo.add(blackLabel); 163 blackPlayerInfo.add(blackPlayerList); 164 blackPlayerInfo.add(blackTime); 165 166 blackPlayerInfo.setBorder(BorderFactory.createEtchedBorder()); 167 168 169 //creates the buttons to either cancel or accept a new game 170 createNewGameButton = new JButton("New Game"); 171 createNewGameButton.addActionListener(this); 172 173 cancelButton = new JButton("Cancel"); 174 cancelButton.addActionListener(this); 175 176 //creates the button panel 177 JPanel buttonPanel = new JPanel(new GridLayout(1,2,20,3)); 178 buttonPanel.add(createNewGameButton); 179 buttonPanel.add(cancelButton); 180 181 GridBagConstraints c; 182 JPanel gameTypePanel = new JPanel(new GridBagLayout()); 183 c = new GridBagConstraints(); 184 185 c.weightx = .5; 186 c.weighty = .5; 187 c.gridwidth = GridBagConstraints.RELATIVE; 188 c.gridx = 0; 189 c.gridy = 0; 190 gameTypePanel.add(new JLabel(" Game Type:")); 191 192 c.gridwidth = GridBagConstraints.REMAINDER; 193 c.gridx = 1; 194 195 gameTypePanel.add(gameType); 196 gameTypePanel.setBorder(BorderFactory.createEtchedBorder()); 197 198 199 200 JPanel labelPane = new JPanel(new GridLayout(1,4)); 201 labelPane.add(new JLabel("")); 202 labelPane.add(new JLabel("Player")); 203 labelPane.add(new JLabel("")); 204 labelPane.add(new JLabel("Time")); 205 206 JPanel playerGameGrid = new JPanel(new GridLayout(2,1, 5, 5)); 207 playerGameGrid.add(whitePlayerInfo); 208 playerGameGrid.add(blackPlayerInfo); 209 210 211 212 JPanel playerPanel = new JPanel(new GridBagLayout()); 213 c = new GridBagConstraints(); 214 c.gridwidth = GridBagConstraints.REMAINDER; 215 c.gridx = 0; 216 c.gridy = 0; 217 playerPanel.add(labelPane,c ); 218 c.gridy = 1; 219 playerPanel.add(playerGameGrid); 220 221 playerPanel.setBorder(BorderFactory.createTitledBorder("Player Info")); 222 223 224 225 226 JPanel newGamePanel = new JPanel(new GridBagLayout()); 227 c = new GridBagConstraints(); 228 c.insets = new Insets(5,5,5,5); 229 c.gridwidth = GridBagConstraints.REMAINDER; 230 c.weighty = .5; 231 newGamePanel.add(playerPanel,c); 232 c.gridy = 1; 233 newGamePanel.add(gameTypePanel,c); 234 c.gridy = 2; 235 newGamePanel.add(buttonPanel,c); 236 newGamePanel.setBorder(BorderFactory.createEtchedBorder()); 237 238 whitePlayerInfo.setPreferredSize(gameTypePanel.getPreferredSize()); 239 blackPlayerInfo.setPreferredSize(gameTypePanel.getPreferredSize()); 240 241 return newGamePanel; 242 } 243 244 //Generates an appropriate game descriptor from the fields 245 private GameDescriptor generateNewGameDescriptor() throws Exception{ 246 ArrayList<Player> playerList = new ArrayList<Player>(); 247 playerList.add(Player.WHITE); 248 playerList.add(Player.BLACK); 249 250 ArrayList<String> descriptions = new ArrayList<String>(); 251 descriptions.add((String)whitePlayerList.getSelectedItem()); 252 descriptions.add((String)blackPlayerList.getSelectedItem()); 253 254 long wTime; 255 long bTime; 256 257 258 if (((String)whiteTime.getSelectedItem()).equals("Untimed")){ 259 wTime = 0; 260 }else{ 261 String[] whiteSplit = ((String)whiteTime.getSelectedItem()).split(":"); 262 if (whiteSplit.length != 2) throw new RuntimeException("Improper size for input"); 263 wTime = 60000*Long.valueOf(whiteSplit[0])+1000*Long.valueOf(whiteSplit[1]); 264 } 265 if (((String)blackTime.getSelectedItem()).equals("Untimed")){ 266 bTime = 0; 267 }else{ 268 String[] blackSplit = ((String)blackTime.getSelectedItem()).split(":"); 269 if (blackSplit.length != 2) throw new RuntimeException("Improper size for input"); 270 bTime = 60000*Long.valueOf(blackSplit[0])+1000*Long.valueOf(blackSplit[1]); 271 } 272 273 long[] times = {wTime,bTime}; 274 if (chessButton.isSelected()){ 275 return new GameDescriptor((String)"Chess", playerList, descriptions, 276 times, false); 277 }else { 278 return new GameDescriptor((String)"Antichess", playerList, descriptions, 279 times, false); 280 } 281 } 282 283 //Tells the master to start a new game 284 private void startNewGame(){ 285 try{ 286 master.startNewGame(this.generateNewGameDescriptor()); 287 }catch (Exception ex){} 288 } 289 290 291 //receives information from button clicks 292 public void actionPerformed(ActionEvent event){ 293 if (event.getSource().equals(cancelButton)){ 294 this.dispose(); 295 }else if (event.getSource().equals(createNewGameButton)) 296 { 297 this.startNewGame(); 298 this.dispose(); 299 } 300 } 301 302 private static final long serialVersionUID = 3100271582525677608L; 303 }