Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Exclamation 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.

    Java 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);
       }
    }
    Java 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);
    		 }
          }
       }
    }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Start with this:
    Java 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);
    [B]      frame.setSize(200, 200);[/B]
          frame.getContentPane().add(new Pigger2());
          frame.setVisible(true);
       }
    }
    :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    It appears you've got a lot of issues with this code.... after a few runs I ran into what I think you were running to... the labels and buttons started to disappear more and more on each run...

    I'm not entirely sure what you're trying to do with this game yet.. I haven't fully read the code.. what I did tho was comment out the paint(Graphics) method, which enables the buttons to appear.

    If I get the chance I'll examine your code more thoroughly... in the meantime reexamine those points I mentioned above, and could you give the basic strategy and game play of how the game is supposed to be played out?
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default Game Play

    The game, if you couldnt tell by the class name, is pigger. The idea behind the game is that you get to roll the dice as long as you want to. Each number that you roll is added up to your score. The only thing is that if you roll double ones, snake eyes, that you lose the entire game. The object is to get to 100 points but i havent gotten that far yet.

  5. #5
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default Pics for the game

    It completly slipped my mind but here are the pics for the die.....
    Attached Thumbnails Attached Thumbnails Cant get these lables to show up for some reaon. Can anyone help?-die1.gif   Cant get these lables to show up for some reaon. Can anyone help?-die2.gif   Cant get these lables to show up for some reaon. Can anyone help?-die3.gif   Cant get these lables to show up for some reaon. Can anyone help?-die4.gif   Cant get these lables to show up for some reaon. Can anyone help?-die5.gif  


  6. #6
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default Last pic

    Last but not least 6
    Attached Thumbnails Attached Thumbnails Cant get these lables to show up for some reaon. Can anyone help?-die6.gif  

  7. #7
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default updates

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.*;
    import java.util.*;
    
    public class Dice extends JPanel
    {
    	final int MID = 150;
        final int TOP = 50;
        int die1 = -1;
        int die2 = -1;
    
        //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);
        }
    
        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 skull if the random number returned is not on the die
    	  return Toolkit.getDefaultToolkit().getImage("Skull.jpeg");
        }
    
        public int rollDie()
        {
            Random rnd = new Random();
    
            int thisRandNum = rnd.nextInt(6) + 1;
            setAndGetDie1Value(thisRandNum);
            setAndGetDie2Value(thisRandNum);
    
            return thisRandNum;
        }
    
        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;
        }
    }
    This didnt really help to much. It actually made it a lil worse but i figures it would make it better in the end

    Java 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();
       Dice die1 = new Dice();
       Dice die2 = new Dice();
       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);
    
          repaint();
       }
    
       public void paint (Graphics page)
       {
    	  //this.add (player1score);
    	  drawThis = page;
       	  //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 skull 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);
    			 die1.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);
    		 }
          }
       }
    }
    Java 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.setSize(300, 200);
          frame.getContentPane().add(new Pigger2());
          frame.setVisible(true);
       }
    }
    Attached Thumbnails Attached Thumbnails Cant get these lables to show up for some reaon. Can anyone help?-skull.jpg  

Similar Threads

  1. Buttons to show new panels
    By Lehane_9 in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2008, 05:22 PM
  2. How to Show Calendar
    By praveen.kb in forum AWT / Swing
    Replies: 2
    Last Post: 02-09-2008, 09:23 AM
  3. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 07:20 AM
  4. show a RTF FORMAT
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 04:37 AM
  5. How to always show the last line in my JTextArea?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 02:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •