Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package rollem;
- import java.awt.Color;
- import javax.swing.JOptionPane;
- /**
- *
- * @author wayne
- */
- public class GameJFrame extends javax.swing.JFrame {
- //instantiations
- private int diceTotal = 12;
- private final int NUMBER_OF_DICE = 2;
- private int turn = 1;
- private int p1Wins = 0;
- private int p2Wins = 0;
- private DieLabel[] myDiceSet = new DieLabel[ NUMBER_OF_DICE + 1 ];
- Player P1 = new Player();
- Player P2 = new Player();
- String player1Name = JOptionPane.showInputDialog(this, "Player 1's Name:");
- String player2Name = JOptionPane.showInputDialog(this, "Player 2's Name:");
- /**
- * Creates new form GameJFrame
- */
- public GameJFrame() {
- initComponents();
- //set name labels to playernames
- P1NameLabel.setText(player1Name);
- P2NameLabel.setText(player2Name);
- //assign labels to spots in array
- myDiceSet[1] = dieLabel1;
- myDiceSet[2] = dieLabel2;
- //initial player text color set to blue
- P1NameLabel.setForeground(Color.blue);
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- P2WinLabel = new javax.swing.JLabel();
- P1WinLabel = new javax.swing.JLabel();
- jLabel7 = new javax.swing.JLabel();
- jLabel6 = new javax.swing.JLabel();
- P1NameLabel = new javax.swing.JLabel();
- P2NameLabel = new javax.swing.JLabel();
- CurrentRollLabel = new javax.swing.JLabel();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- P1GameScoreLabel = new javax.swing.JLabel();
- P2TurnScoreLabel = new javax.swing.JLabel();
- P2GameScoreLabel = new javax.swing.JLabel();
- P1TurnScoreLabel = new javax.swing.JLabel();
- RollButton = new javax.swing.JButton();
- StopButton = new javax.swing.JButton();
- jPanel1 = new javax.swing.JPanel();
- dieLabel1 = new rollem.DieLabel();
- dieLabel2 = new rollem.DieLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setMaximumSize(new java.awt.Dimension(1108, 800));
- setPreferredSize(new java.awt.Dimension(1108, 800));
- P2WinLabel.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- P2WinLabel.setText("0");
- P1WinLabel.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- P1WinLabel.setText("0");
- jLabel7.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- jLabel7.setText("Wins:");
- jLabel6.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- jLabel6.setText("Wins:");
- P1NameLabel.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- P1NameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- P1NameLabel.setText("Player 1 Name");
- P2NameLabel.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- P2NameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- P2NameLabel.setText("Player 2 Name");
- CurrentRollLabel.setText("Current Roll: 12");
- jLabel1.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
- jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- jLabel1.setText("Roll 'Em Pigs!");
- jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- jLabel2.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- jLabel2.setText("Turn Score:");
- jLabel3.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- jLabel3.setText("Game Score:");
- jLabel4.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- jLabel4.setText("Game Score:");
- jLabel5.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- jLabel5.setText("Turn Score:");
- P1GameScoreLabel.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- P1GameScoreLabel.setText("0");
- P2TurnScoreLabel.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- P2TurnScoreLabel.setText("0");
- P2GameScoreLabel.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- P2GameScoreLabel.setText("0");
- P1TurnScoreLabel.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- P1TurnScoreLabel.setText("0");
- RollButton.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- RollButton.setText("Roll");
- RollButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- RollButtonActionPerformed(evt);
- }
- });
- StopButton.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
- StopButton.setText("Stop");
- StopButton.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- StopButtonActionPerformed(evt);
- }
- });
- jPanel1.setLayout(new java.awt.GridLayout(1, 2));
- jPanel1.add(dieLabel1);
- jPanel1.add(dieLabel2);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(63, 63, 63)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(P1NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jLabel2)))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(P1GameScoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P1TurnScoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P1WinLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(14, 14, 14)))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(layout.createSequentialGroup()
- .addComponent(RollButton, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(61, 61, 61)
- .addComponent(StopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .addGap(65, 65, 65)
- .addComponent(P2NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addGroup(layout.createSequentialGroup()
- .addGap(444, 444, 444)
- .addComponent(jLabel5)
- .addGap(24, 24, 24))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGap(149, 149, 149)
- .addComponent(CurrentRollLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel4)
- .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)))
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(P2TurnScoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P2GameScoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P2WinLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))))
- .addGroup(layout.createSequentialGroup()
- .addGap(270, 270, 270)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 518, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(63, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(12, 12, 12)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(50, 50, 50)
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(CurrentRollLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(RollButton, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(StopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(30, 30, 30))
- .addGroup(layout.createSequentialGroup()
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(P2NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P1NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P2TurnScoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P2GameScoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel7)
- .addComponent(P2WinLabel)))
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P1TurnScoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P1GameScoreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(P1WinLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
- .addContainerGap(89, Short.MAX_VALUE))))
- );
- pack();
- }// </editor-fold>
- private void RollButtonActionPerformed(java.awt.event.ActionEvent evt) {
- if (turn == 1)
- {
- CurrentRollLabel.setText("Current Roll: " + P1.rollDie(myDiceSet[1], myDiceSet[2]));
- P1TurnScoreLabel.setText(P1.getTurnScore() + "");
- if (P1.isTurnScoreLost())
- {
- turn = 2;
- P1NameLabel.setForeground(Color.black);
- P2NameLabel.setForeground(Color.blue);
- return;
- }
- }
- if (turn == 2)
- {
- CurrentRollLabel.setText("Current Roll: " + P2.rollDie(myDiceSet[1], myDiceSet[2]) + "");
- P2TurnScoreLabel.setText(P2.getTurnScore() + "");
- if (P2.isTurnScoreLost())
- {
- turn = 1;
- P1NameLabel.setForeground(Color.blue);
- P2NameLabel.setForeground(Color.black);
- }
- }
- }
- private void StopButtonActionPerformed(java.awt.event.ActionEvent evt) {
- if (turn == 1)
- {
- if (P1.addTurnScoreToGameScore())
- {
- if (P1.hasWon())
- {
- int temp = JOptionPane.showConfirmDialog(this, player1Name + " wins! Play again?");
- if (temp == JOptionPane.NO_OPTION)
- {
- System.exit(1);
- }
- else
- {
- P1.reset();
- P2.reset();
- P1TurnScoreLabel.setText(P1.getTurnScore() + "");
- P2TurnScoreLabel.setText(P2.getTurnScore() + "");
- P1GameScoreLabel.setText(P1.getGameScore() + "");
- P2GameScoreLabel.setText(P2.getGameScore() + "");
- turn = 2;
- p1Wins++;
- P1WinLabel.setText(p1Wins + "");
- }
- return;
- }
- turn = 2;
- P1NameLabel.setForeground(Color.black);
- P2NameLabel.setForeground(Color.blue);
- CurrentRollLabel.setText("Current Roll: ");
- P1TurnScoreLabel.setText("");
- P1GameScoreLabel.setText(P1.getGameScore() + "");
- return;
- }
- }
- if (turn == 2)
- {
- if (P2.addTurnScoreToGameScore())
- {
- if (P2.hasWon())
- {
- int temp = JOptionPane.showConfirmDialog(this, player2Name + " wins! Play again?");
- if (temp == JOptionPane.NO_OPTION)
- {
- System.exit(1);
- }
- else
- {
- P1.reset();
- P2.reset();
- P1TurnScoreLabel.setText(P1.getTurnScore() + "");
- P2TurnScoreLabel.setText(P2.getTurnScore() + "");
- P1GameScoreLabel.setText(P1.getGameScore() + "");
- P2GameScoreLabel.setText(P2.getGameScore() + "");
- turn = 1;
- p2Wins++;
- P2WinLabel.setText(p2Wins + "");
- }
- return;
- }
- turn = 1;
- P1NameLabel.setForeground(Color.blue);
- P2NameLabel.setForeground(Color.black);
- CurrentRollLabel.setText("Current Roll: ");
- P2TurnScoreLabel.setText("");
- P2GameScoreLabel.setText(P2.getGameScore() + "");
- }
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(GameJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(GameJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(GameJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(GameJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new GameJFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JLabel CurrentRollLabel;
- private javax.swing.JLabel P1GameScoreLabel;
- private javax.swing.JLabel P1NameLabel;
- private javax.swing.JLabel P1TurnScoreLabel;
- private javax.swing.JLabel P1WinLabel;
- private javax.swing.JLabel P2GameScoreLabel;
- private javax.swing.JLabel P2NameLabel;
- private javax.swing.JLabel P2TurnScoreLabel;
- private javax.swing.JLabel P2WinLabel;
- private javax.swing.JButton RollButton;
- private javax.swing.JButton StopButton;
- private rollem.DieLabel dieLabel1;
- private rollem.DieLabel dieLabel2;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JLabel jLabel6;
- private javax.swing.JLabel jLabel7;
- private javax.swing.JPanel jPanel1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement