View Single Post
  #1 (permalink)  
Old 04-04-2008, 03:54 AM
wrestlingod302 wrestlingod302 is offline
Member
 
Join Date: Apr 2008
Posts: 5
wrestlingod302 is on a distinguished road
Cant get these lables to show up for some reaon. Can anyone help?
I cant get these darn JLables to show up to save my life. Im also having little issues like the app starting out almost minimized. if anyone could help it would be greatly appreciated.

Code:
//******************************************************************** // // // //******************************************************************** import javax.swing.JFrame; public class PiggerLoader { //----------------------------------------------------------------- // Creates the main program frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Pigger Loader"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new Pigger2()); frame.setVisible(true); } }
Code:
//******************************************************************** // Pigger2.java Authors: Lorenzo Wells // // Plays the game Pigger with two people //******************************************************************** import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.lang.*; import java.util.*; public class Pigger2 extends JPanel { private int count; private JButton rollTheDice = new JButton(); private JButton switchPlayers = new JButton(); private JLabel player1score = new JLabel(); private JLabel player2score = new JLabel(); private JLabel whichPlayer = new JLabel(); Graphics drawThis; //keeps the position of each die on the screen when drawn final int MID = 150; final int TOP = 50; //keeps the value of each die int die1 = -1; int die2 = -1; //----------------------------------------------------------------- // Constructor: Sets up the GUI. //----------------------------------------------------------------- public Pigger2 () { count = 50; rollTheDice = new JButton ("Roll Dice"); switchPlayers = new JButton ("End Turn"); player1score = new JLabel ("Player1 score: "); player2score = new JLabel ("Player2 score: "); whichPlayer = new JLabel ("Player: "); player1score.setText("IM HERE!!!!"); ButtonListener listener = new ButtonListener(); rollTheDice.addActionListener (listener); switchPlayers.addActionListener (listener); this.add (player1score); this.add (player2score); this.add (rollTheDice); this.add (switchPlayers); rollTheDice.setToolTipText("Roll the dice for another turn."); switchPlayers.setToolTipText("End your turn and pass to the next player."); player1score.setToolTipText("Player1's game score."); player2score.setToolTipText("Player2's game score."); this.setPreferredSize (new Dimension(300, 40)); this.setBackground (Color.BLUE); } public void paint (Graphics page) { this.add (player1score); DrawDice (page); } //Draws the dice images at the specified locations. In order to get the image it calls a program(WhichSide) that calls a program //(setAndGetDie1Value1 and setAndGetDie2Value) that calls a program(rollDie). public void DrawDice (Graphics dice) { dice.drawImage(WhichSide(setAndGetDie1Value(rollDie())),MID,TOP,Color.WHITE,this); dice.drawImage(WhichSide(setAndGetDie2Value(rollDie())),MID-60,TOP,Color.WHITE,this); } //Calculates weather a player has lost durring a givin roll public void didILose() { if(die1 == die2) { drawThis.drawString("You Lose",MID,TOP); remove(rollTheDice); } } //Takes an integer paramiter to retun the image that will become the side of the die that is shown public Image WhichSide (int whichSide) { switch(whichSide) { case 1: return Toolkit.getDefaultToolkit().getImage("Die1.gif"); case 2: return Toolkit.getDefaultToolkit().getImage("Die2.gif"); case 3: return Toolkit.getDefaultToolkit().getImage("Die3.gif"); case 4: return Toolkit.getDefaultToolkit().getImage("Die4.gif"); case 5: return Toolkit.getDefaultToolkit().getImage("Die5.gif"); case 6: return Toolkit.getDefaultToolkit().getImage("Die6.gif"); } //retuns a skul if the random number returned is not on the die return Toolkit.getDefaultToolkit().getImage("Skull.jpeg"); } //Creates a random number to be used for the sides public int rollDie() { Random rnd = new Random(); int thisRandNum = rnd.nextInt(6) + 1; setAndGetDie1Value(thisRandNum); setAndGetDie2Value(thisRandNum); return thisRandNum; } //sets the values for die1 to be used in calculating the score public int setAndGetDie1Value(int thisInt) { die1 = thisInt; return die1; } //sets the values for die2 to be used in calculating the score public int setAndGetDie2Value(int thisInt) { die2 = thisInt; return die2; } //***************************************************************** // Represents a listener for both buttons push (action) events. //***************************************************************** private class ButtonListener implements ActionListener { //-------------------------------------------------------------- // Updates the counter and label when the button is pushed. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { boolean player = true; if (event.getSource() == rollTheDice) { //DrawDice (drawThis); repaint(); didILose(); if(player) { //updates the player one score //player1score.setText("Player1 score: " + (die1 + die2)); } else { //updates the player2 score //player2score.setText("Player2 score " + (die1 + die2)); } } if (event.getSource() == switchPlayers) { //sets the player boolean to equal the oposite of itself player =! player; add(rollTheDice); } } } }
Reply With Quote
Sponsored Links