001 package viewguitest; 002 import java.awt.Container; 003 import java.awt.GridLayout; 004 import java.awt.event.ActionEvent; 005 import java.awt.event.ActionListener; 006 007 import javax.swing.JButton; 008 import javax.swing.JFrame; 009 import javax.swing.JPanel; 010 011 import antichess.GameClock; 012 import antichess.Player; 013 import antichess.viewgui.TimerLabel; 014 015 /** 016 * 017 * The TimerLabel test tests the TimerLabel class and the ChessClock class 018 * which in turn tests the methods in the GameTimer class that ChessClock relies on 019 * 020 * 021 * 022 * Because of the difficulty of writing automated tests for timers this is 023 * all implemented in a GUI. 024 * 025 * 026 * @author nlharr 027 * 028 * 029 * 030 */ 031 032 public class TimerLabelTest implements ActionListener { 033 034 035 036 /** 037 * @return the content plane for the GUI 038 */ 039 private GameClock tempClock; 040 private JButton switchButton; 041 private JButton stopButton; 042 private JButton startWhiteButton; 043 private JButton startBlackButton; 044 private JButton setWhiteButton5minutes; 045 private JButton setBlackButton5minutes; 046 private JButton setWhiteButton15minutes; 047 private JButton setBlackButton15minutes; 048 private JButton setWhiteButton30seconds; 049 private JButton setBlackButton30seconds; 050 051 052 public Container getContentPane(){ 053 Player[] playerList = new Player[2]; 054 playerList[0] = Player.WHITE; 055 playerList[1] = Player.BLACK; 056 long[] times = new long[2]; 057 058 times[0] = 50000; 059 times[1] = 120000; 060 061 tempClock = new GameClock(playerList, times); 062 tempClock.startTimer(Player.WHITE); 063 JPanel jp = new JPanel(new GridLayout(0,5)); 064 for (int i=0; i<10; i++){ 065 jp.add(new TimerLabel(tempClock, Player.WHITE)); 066 jp.add(new TimerLabel(tempClock, Player.BLACK)); 067 jp.add(new TimerLabel(tempClock, Player.RED)); 068 069 } 070 stopButton = new JButton("stop"); 071 stopButton.addActionListener(this); 072 jp.add(stopButton); 073 074 startWhiteButton = new JButton("startWhite"); 075 startWhiteButton.addActionListener(this); 076 jp.add(startWhiteButton); 077 078 startBlackButton = new JButton("startBlack"); 079 startBlackButton.addActionListener(this); 080 jp.add(startBlackButton); 081 082 083 setWhiteButton5minutes = new JButton("Set White 5 minutes"); 084 setWhiteButton5minutes.addActionListener(this); 085 jp.add(setWhiteButton5minutes); 086 087 setBlackButton5minutes = new JButton("Set Black 5 minutes"); 088 setBlackButton5minutes.addActionListener(this); 089 jp.add(setBlackButton5minutes); 090 091 setBlackButton15minutes = new JButton("Set Black 15 minutes"); 092 setBlackButton15minutes.addActionListener(this); 093 jp.add(setBlackButton15minutes); 094 095 setWhiteButton15minutes = new JButton("Set White 15 minutes"); 096 setWhiteButton15minutes.addActionListener(this); 097 jp.add(setWhiteButton15minutes); 098 099 setBlackButton30seconds = new JButton("Set Black 30 seconds"); 100 setBlackButton30seconds.addActionListener(this); 101 jp.add(setBlackButton30seconds); 102 103 setWhiteButton30seconds = new JButton("Set White 30 seconds"); 104 setWhiteButton30seconds.addActionListener(this); 105 jp.add(setWhiteButton30seconds); 106 107 108 109 return jp; 110 } 111 112 113 /** 114 * Handles events for the TimerLabelTest 115 */ 116 public void actionPerformed(ActionEvent e) { 117 if (e.getSource().equals(stopButton)){ 118 tempClock.stopTimer(); 119 }else if (e.getSource().equals(startWhiteButton)){ 120 tempClock.startTimer(Player.WHITE); 121 }else if (e.getSource().equals(startBlackButton)){ 122 tempClock.startTimer(Player.BLACK); 123 }else if (e.getSource().equals(setBlackButton5minutes)){ 124 tempClock.setTime(300000, Player.BLACK); 125 }else if (e.getSource().equals(setWhiteButton5minutes)){ 126 tempClock.setTime(300000, Player.WHITE); 127 }else if (e.getSource().equals(setBlackButton15minutes)){ 128 tempClock.setTime(900000, Player.BLACK); 129 }else if (e.getSource().equals(setWhiteButton15minutes)){ 130 tempClock.setTime(900000, Player.WHITE); 131 }else if (e.getSource().equals(setBlackButton30seconds)){ 132 tempClock.setTime(30000, Player.BLACK); 133 }else if (e.getSource().equals(setWhiteButton30seconds)){ 134 tempClock.setTime(30000, Player.WHITE); 135 } 136 } 137 138 139 /** 140 * Create the GUI and shows it. 141 */ 142 143 public static void createAndShowGUI() { 144 //Create and set up the window 145 JFrame.setDefaultLookAndFeelDecorated(true); 146 JFrame frame = new JFrame("AntiChess"); 147 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 148 149 //Create and set up the content pane and menu bar 150 TimerLabelTest gui = new TimerLabelTest(); 151 frame.setContentPane(gui.getContentPane()); 152 //Display the window 153 frame.setSize(800, 600); 154 frame.setVisible(true); 155 } 156 157 //used in testing 158 public static void main(String args[]){ 159 160 javax.swing.SwingUtilities.invokeLater(new Runnable() { 161 public void run() { 162 createAndShowGUI(); 163 } 164 }); 165 } 166 167 }