Hi I'm really new to Java, as in I've been doing it solidly for a couple of weeks now. I think I'm starting to get a handle on things (the basics, anyway) & of course like every good beginner, am writing a game
The problem I'm having is that my button clicks are getting consumed! Nothing happens! Now my understanding of ActionEvents & such is basically zip. I am using arrays of Icons attached to different JLabels to simulate parts of the screen. Then, to test my button functionality, I would like my test click to change the first 'dice' pic.... It doesn't! I don't know if it's because of where the actionPerformed method is (at the very end of the file)?
Here is the code... If it's impossible to see what I'm trying to do, I'm sorry. I'm trying to write the best //notes I can...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class CrapsGame extends JFrame implements ActionListener {
// initializing variables used in game
Random random = new Random();
// --- BUTTONS ---
private JButton newGameBtn, // creates new game
placeBetBtn, // places player bet amount
resetAllBtn, // restarts game
instructBtn, // instructions obv
rollDiceBtn; // rolls the virtual dice
// --- PANELS ---
private JPanel headPnl,
head1Pnl,
mainPnl,
main1Pnl,
main2Pnl,
bottomPnl; // this is my button panel
// --- STRINGS ---
private String betNumber; // used to capture player bet as a string then parse to int
private String txtBankroll = "CURRENT BANKROLL: $";
private String txtPoint = "CURRENT POINT: ";
private String txtBet = "CURRENT BET AMOUNT: $";
// A way to load all my images used in game to icons ---
private String rolledPics[] = {"rolled2.png",
"rolled3.png",
"rolled4.png",
"rolled5.png",
"rolled6.png",
"rolled7.png",
"rolled8.png",
"rolled9.png",
"rolled10.png",
"rolled11.png",
"rolled12.png"};
// --- ICONS ---
private Icon spacerPic = new ImageIcon(getClass().getResource("spacer.png"));
private Icon headPic = new ImageIcon(getClass().getResource("header.png"));
private Icon roll = new ImageIcon(getClass().getResource("diceBtnPic.png") );
private Icon rolledIcons[] = {
new ImageIcon(getClass().getResource(rolledPics[0])),
new ImageIcon(getClass().getResource(rolledPics[1])),
new ImageIcon(getClass().getResource(rolledPics[2])),
new ImageIcon(getClass().getResource(rolledPics[3])),
new ImageIcon(getClass().getResource(rolledPics[4])),
new ImageIcon(getClass().getResource(rolledPics[5])),
new ImageIcon(getClass().getResource(rolledPics[6])),
new ImageIcon(getClass().getResource(rolledPics[7])),
new ImageIcon(getClass().getResource(rolledPics[8])),
new ImageIcon(getClass().getResource(rolledPics[9])),
new ImageIcon(getClass().getResource(rolledPics[10]))};
private Icon diceIcons[] = { // the pics that represent the 'screen'
new ImageIcon(getClass().getResource("1.png")),
new ImageIcon(getClass().getResource("2.png")),
new ImageIcon(getClass().getResource("2.png")),
new ImageIcon(getClass().getResource("4.png")),
new ImageIcon(getClass().getResource("5.png")),
new ImageIcon(getClass().getResource("6.png"))};
private Icon wonLostIcons[] = {
new ImageIcon(getClass().getResource("rollAgain.png")) ,
new ImageIcon(getClass().getResource("youWin.png")),
new ImageIcon(getClass().getResource("youLose.png"))};
// --- BOOLEANS ---
boolean isGame; // is a game being played?
boolean isBroke; // does player have > 0?
boolean isBet; // has player placed bet amount?
// --- INTEGERS ---
int initialMoney = 1000; // player starting money
int currentMoney; // current player money
int betAmt; // player bet size
int betAmt1; // used to parse player input of bet amt to int
int roll1; // calculates a dice roll (dice 1)
int roll2; // calculates a dice roll (dice 2)
int point = 4; // used as the 'point'... play the game!
int answer; // used to match rolls against the point
// --- LABELS ---
private JLabel header; // holds TITLE graphic
private JLabel scrn1; // LEFT side of the dice screen
private JLabel scrn2; // RIGHT side of the dice screen
private JLabel rolled; // bottom 'in game graphic' panel eg. 'you rolled 9'
private JLabel winLose; // 2nd ingame graphic, 'roll again' or 'you win' etc
// --- JTEXTAREA
private JTextArea playerStatusBar; // player info panel
public CrapsGame() { // beginning frame constructor
super("Craps Game");
// ------------------------------ CREATING & ADDING EVERYTHING TO THE GUI -----
// creating labels & adding Icons
JLabel spacer = new JLabel(spacerPic);
JLabel header = new JLabel(headPic);
JLabel scrn1 = new JLabel(diceIcons[0]);
JLabel scrn2 = new JLabel(diceIcons[0]);
JLabel scrnBottom = new JLabel(rolledIcons[0]);
JLabel winLose = new JLabel(wonLostIcons[0]);
JLabel rollBtnLbl = new JLabel(roll);
// creating buttons, adding actionListeners
JButton newGameBtn = new JButton("new game");
newGameBtn.addActionListener(this);
JButton placeBetBtn = new JButton("place bet");
placeBetBtn.addActionListener(this);
JButton resetAllBtn = new JButton("reset all");
resetAllBtn.addActionListener(this);
JButton instructBtn = new JButton("instructions");
instructBtn.addActionListener(this);
JButton rollDiceBtn = new JButton(roll);
rollDiceBtn.addActionListener(this);
// creating JTextArea i guess...
JTextArea playerStatusBar = new JTextArea(" " +txtBankroll
+currentMoney +"\n\n "
+txtPoint +point +"\n\n "
+txtBet +betAmt,5,20);
playerStatusBar.setBackground(new Color(40,50,50));
playerStatusBar.setForeground(Color.WHITE);
playerStatusBar.setEditable(false);
// creating panels
JPanel headPnl = new JPanel();
JPanel mainScrnPnl = new JPanel();
JPanel mainScrnTopPnl = new JPanel();
JPanel mainRoll1Pnl = new JPanel();
JPanel mainRoll2Pnl = new JPanel();
JPanel mainRollReportPnl = new JPanel();
JPanel bottomPnl = new JPanel();
JPanel topScrnPnl = new JPanel();
// setting backgrounds/colors + putting the pieces together
headPnl.setBackground(new Color(40,50,50));
headPnl.add(header);
headPnl.add(instructBtn);
mainRollReportPnl.setBackground(new Color(40,50,50));
mainScrnPnl.setBackground(Color.BLACK);
mainScrnPnl.add(spacer);
mainScrnPnl.add(scrn1);
mainScrnPnl.add(scrn2);
mainScrnPnl.add(scrnBottom);
mainScrnPnl.add(winLose);
bottomPnl.setBackground(new Color(40,50,50));
bottomPnl.add(rollDiceBtn);
bottomPnl.add(playerStatusBar);
bottomPnl.add(placeBetBtn);
bottomPnl.add(newGameBtn);
add(headPnl, BorderLayout.NORTH);
add(mainScrnPnl);
add(bottomPnl, BorderLayout.SOUTH);
} // end Craps() constructor
// ------------------------- METHODS -------------------------------------
public void newGame() { // method to begin a new game
scrn1.setIcon(diceIcons[3]);
scrn2.setIcon(diceIcons[5]);
} // end newGame method
public int getBet() { // gets the bet amt
betNumber = JOptionPane.showInputDialog( "Enter Bet Amount:");
betAmt1 = Integer.parseInt( betNumber );
return betAmt1;
} // end getBet method
// ------------------------ACTION EVENTS----------------------------------
// handle button events
public void actionPerformed(ActionEvent event) {
if (event.getSource() == placeBetBtn) {
betAmt = getBet();
} // end IF
if (event.getSource() == newGameBtn) {
newGame();
} // end if
} // end actionperformed method
} // end CRAPS class
Again thanks if u actually took the time to sift through my beginner code, I know it's probably ridiculous. Any help u can offer is much appreciated!
- Unscripted_1
There are currently 1 users browsing this thread. (0 members and 1 guests)