/** * @author Kristof Friedrichsen * @author Ryan Mann * @author Bob Free * @author Pat O'Neill * @version December 15, 2006 **/ package chutesladder; import javax.swing.JPanel; import javax.swing.JButton; import java.util.Random; import java.util.Date; import java.awt.Color; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class GamePlayPanel extends JPanel { static private SpinnerPanel spinnerPanel; static public JButton btSpin; static private int spinnerValue=1; /** Creates a new instance of MainPanel */ public GamePlayPanel() { spinnerPanel = new SpinnerPanel(); btSpin = new JButton("Spin"); spinnerPanel.setBackground(Color.BLACK); BorderLayout gamePlayLayout = new BorderLayout(); this.setLayout(gamePlayLayout); add(BorderLayout.CENTER, spinnerPanel); add(BorderLayout.SOUTH, btSpin); btSpin.addActionListener(new SpinListener()); } public int getSpinnerValue() { return spinnerValue; } static public void SetButtonFalse() { btSpin.setEnabled(false); System.out.println("Button set to false."); } static public void SetButtonTrue() { btSpin.setEnabled(true); System.out.println("Button set to true."); } public class SpinListener implements ActionListener { public void actionPerformed(ActionEvent event) { Date time = new Date(); Random rand = new Random(time.getTime()); //This prevents from spinning zero times int spinNum = rand.nextInt(18)+6; // but we will spin from 0 to the random number for (int i=0; i < spinNum; i++) { for (int j=0;j<6;j++) { spin(i); } spinnerValue++; if(spinnerValue > 6) spinnerValue = 1; } System.out.println("Spinner should be on " + spinnerValue); CaL.movePlayer(spinnerValue,CaL.myNumber); } } public static void forceSpin() { Date time = new Date(); Random rand = new Random(time.getTime()); //This prevents from spinning zero times //number between 30 and 50 int spinNum = rand.nextInt(20)+2; // but we will spin from 0 to the random number for (int i=0; i < spinNum; i++) { for (int j=0;j<6;j++) { spin(i); } spinnerValue++; if(spinnerValue > 6) spinnerValue = 1; } //System.out.println("Spinner should be on " + spinnerValue); try { CaL.movePlayer(spinnerValue,CaL.myNumber); } catch (NullPointerException exp) {} } static private void spin(int x) { // Changes the speed of the spin by changing the sleep // time between each spin int sleepTime = 0; try { Thread.sleep(sleepTime); spinnerPanel.changeAngle(-10); //System.out.println("Update Spinner " + x); spinnerPanel.update(spinnerPanel.getGraphics()); } catch (InterruptedException e) { System.out.println(e); } } }