KillianO92

CrapsGame

Dec 19th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class CrapsGame extends JFrame
  6. {
  7.     private static final int Width = 400;
  8.     private static final int Height = 300;
  9.  
  10.     //JFrame Componenets
  11.     private JTextArea jtaGame;
  12.  
  13.     private JButton jbtnPlay, jbtnClear, jbtnExit;
  14.  
  15.     private PlayButtonHandler playHandler;
  16.     private ClearButtonHandler  clearHandler;
  17.     private ExitButtonHandler   exitHandler;
  18.  
  19.     private JScrollPane scrollingResult;
  20.  
  21.     private JPanel jpnlTop = new JPanel();
  22.     private JPanel jpnlBottom = new JPanel();
  23.  
  24.     public CrapsGame()
  25.     {
  26.         //Set the title and size
  27.         setTitle("Craps Game");
  28.         setSize(WIDTH, HEIGHT);
  29.  
  30.         //Instantiate JTextArea + Scrollabe
  31.         jtaGame = new JTextArea(10,1);
  32.         scrollingResult = new JScrollPane(jtaGame);
  33.  
  34.         // Instantiate and register the Play button for clicks events:
  35.         jbtnPlay = new JButton("Play");
  36.         playHandler = new PlayButtonHandler();
  37.         jbtnPlay.addActionListener(playHandler);
  38.  
  39.         // Instantiate and register the Clear button for clicks events:
  40.         jbtnClear = new JButton("Clear");
  41.         clearHandler = new ClearButtonHandler();
  42.         jbtnClear.addActionListener(clearHandler);
  43.  
  44.         // Instantiate and register the Exit button for clicks events:
  45.         jbtnExit = new JButton("Exit");
  46.         exitHandler = new ExitButtonHandler();
  47.         jbtnExit.addActionListener(exitHandler);
  48.  
  49.         //Assemble JPanels
  50.         jpnlTop.setLayout(new GridLayout(1, 1));
  51.         jpnlTop.add(scrollingResult);
  52.  
  53.         jpnlBottom.setLayout(new GridLayout(1, 3));
  54.         jpnlBottom.add(jbtnPlay);
  55.         jpnlBottom.add(jbtnClear);
  56.         jpnlBottom.add(jbtnExit);
  57.  
  58.         // Start to add the components to the JFrame:
  59.         Container pane = getContentPane();
  60.         pane.setLayout(new BorderLayout());
  61.  
  62.         pane.add(jpnlTop, BorderLayout.NORTH);
  63.         pane.add(jpnlBottom, BorderLayout.SOUTH);
  64.  
  65.         // Show the JFrame and set code to respond to the user clicking on the X:
  66.         setVisible(true);
  67.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  68.  
  69.         jpnlTop.setLayout(new GridLayout(1, 1));
  70.         jpnlTop.add(scrollingResult);
  71.  
  72.         jpnlBottom.add(jbtnPlay);
  73.         jpnlBottom.add(jbtnClear);
  74.         jpnlBottom.add(jbtnExit);
  75.  
  76.         // Show the JFrame and set code to respond to the user clicking on the X:
  77.         setVisible(true);
  78.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  79.     }//End Constructor
  80.  
  81.  
  82.  
  83.     public int rollDice()
  84.     {
  85.         int iDie1;= (int)(Math.random()*6) +1;
  86.         int iDie2 = (int)(Math.random()*6) +1;
  87.         int iDieTotal = iDie1 + iDie2;
  88.    
  89.         return iDieTotal;
  90.     }
  91.    
  92.     public void writeMessage(String sOutput)
  93.     {
  94.         jtaGame.append(sOutput);
  95.     }
  96.        
  97.        
  98.  
  99.     private class PlayButtonHandler implements ActionListener
  100.     {
  101.  
  102.         public void actionPerformed(ActionEvent e)
  103.         {
  104.             rollDice();
  105.            
  106.             int iDice = new rollDice();
  107.             int iPoint = 0;
  108.            
  109.            
  110.  
  111.         }
  112.  
  113.     }
  114.  
  115.  
  116.  
  117.  
  118.     private class ExitButtonHandler implements ActionListener
  119.     {
  120.         public void actionPerformed(ActionEvent e)
  121.         {
  122.             System.exit(0);
  123.         }
  124.     }//end ExitButtonHandler
  125.  
  126.  
  127.     private class ClearButtonHandler implements ActionListener
  128.     {
  129.         public void actionPerformed(ActionEvent e)
  130.         {
  131.             jtaGame.setText("");
  132.          }
  133.     } // end ClearButtonHandler
  134.  
  135.  
  136.     public static void main(String args[])
  137.     {
  138.         // The only thing this main does is Instantiate the JFrame:
  139.         // Note the name of the object is the name of the class from above.
  140.         CrapsGame crGame = new CrapsGame();
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment