|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-04-2008, 03:54 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 5
|
|
|
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.
//********************************************************************
//
//
//
//********************************************************************
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);
}
}
//********************************************************************
// 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);
}
}
}
}
|
|

04-04-2008, 08:39 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 644
|
|
Start with this:
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(200, 200);
frame.getContentPane().add(new Pigger2());
frame.setVisible(true);
}
}

__________________
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
|
|

04-04-2008, 09:13 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 644
|
|
|
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?
__________________
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
|
|

04-05-2008, 08:24 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 5
|
|
|
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.
|
|

04-05-2008, 08:38 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 5
|
|
|
Pics for the game
It completly slipped my mind but here are the pics for the die.....
|
|

04-05-2008, 08:39 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 5
|
|
|
Last pic
Last but not least 6
|
|

04-07-2008, 04:57 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 5
|
|
|
updates
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
//********************************************************************
// 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);
}
}
}
}
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);
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|