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