Java Program - Dice
Results 1 to 3 of 3

Thread: Java Program - Dice

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    590

    Post Java Program - Dice

    Hey folks, I thought I'd just post this simply little java program here, in the tutorial forum. It's basically a program that rolls a pair of dice...so yeah, you can use it instead of those real crappy monopoly dice.

    hehe...ok, maybe not...but it's good for an intro to graphics in java, even though it's basic.

    Here is the code:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Dice extends JApplet implements ActionListener
    {
    	private Container pane;
    	private JButton rollButton;
    	private boolean doRoll = true;
    
    	public void init()
    	{
    		pane = getContentPane();
    		pane.setLayout(new FlowLayout());
    
    		rollButton = new JButton("Roll Dice");
    
    		pane.add(rollButton);
    
    		rollButton.addActionListener(this);
    	}
    
    	public void paint(Graphics g)
    	{
    		super.paint(g);
    
    		//Sets the color to be used to black, then draws the outlines of the dice.
    		g.setColor(Color.black);
    		g.drawRoundRect(23, 50, 40, 40, 10, 10);
    		g.drawRoundRect(83, 50, 40, 40, 10, 10);
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		if(e.getSource() == rollButton)
    			rollAction();
    	}
    
    	public void rollAction()
    	{
    		if(doRoll)
    		{
    			//Here is my poor random 'dice number' generator, but for now, it'll do for me.
    			double dice1 = 6;
    			double dice2 = 6;
    
    			//Changes the current color to be used for the dots on the dice to blue.
    			Graphics g = getGraphics();
    			g.setColor(Color.blue);
    
    			while((dice1 > 5) || (dice2 > 5))
    			{
    				dice1 = Math.rint(Math.random()*10);
    				dice2 = Math.rint(Math.random()*10);
    			}
    
    			//NOTE: I probably should have coded this better, but basically, the number
    			//displayed on the dice is one greater then the one inside the 'if statements'.
    			if(dice1 == 5)
    			{
    				//Draws six dots on the dice
    				g.fillOval(28, 52, 10, 10);
    				g.fillOval(48, 52, 10, 10);
    				g.fillOval(28, 65, 10, 10);
    				g.fillOval(48, 65, 10, 10);
    				g.fillOval(28, 78, 10, 10);
    				g.fillOval(48, 78, 10, 10);
    			}
    			if(dice2 == 5)
    			{
    				//Draws six dots on the dice
    				g.fillOval(88, 52, 10, 10);
    				g.fillOval(108, 52, 10, 10);
    				g.fillOval(88, 65, 10, 10);
    				g.fillOval(108, 65, 10, 10);
    				g.fillOval(88, 78, 10, 10);
    				g.fillOval(108, 78, 10, 10);
    			}
    			if(dice1 == 4)
    			{
    				//Draws five dots on the dice
    				g.fillOval(28, 52, 10, 10);
    				g.fillOval(48, 52, 10, 10);
    				g.fillOval(38, 65, 10, 10);
    				g.fillOval(28, 78, 10, 10);
    				g.fillOval(48, 78, 10, 10);
    			}
    			if(dice2 == 4)
    			{
    				//Draws five dots on the dice
    				g.fillOval(88, 52, 10, 10);
    				g.fillOval(108, 52, 10, 10);
    				g.fillOval(98, 65, 10, 10);
    				g.fillOval(88, 78, 10, 10);
    				g.fillOval(108, 78, 10, 10);
    			}
    			if(dice1 == 3)
    			{
    				//Draws four dots on the dice
    				g.fillOval(28, 52, 10, 10);
    				g.fillOval(48, 52, 10, 10);
    				g.fillOval(28, 78, 10, 10);
    				g.fillOval(48, 78, 10, 10);
    			}
    			if(dice2 == 3)
    			{
    				//Draws four dots on the dice
    				g.fillOval(88, 52, 10, 10);
    				g.fillOval(108, 52, 10, 10);
    				g.fillOval(88, 78, 10, 10);
    				g.fillOval(108, 78, 10, 10);
    			}
    			if(dice1 == 2)
    			{
    				//Draws three dots on the dice
    				g.fillOval(28, 52, 10, 10);
    				g.fillOval(38, 65, 10, 10);
    				g.fillOval(48, 78, 10, 10);
    			}
    			if(dice2 == 2)
    			{
    				//Draws three dots on the dice
    				g.fillOval(88, 52, 10, 10);
    				g.fillOval(98, 65, 10, 10);
    				g.fillOval(108, 78, 10, 10);
    			}
    			if(dice1 == 1)
    			{
    				//Draws two dots on the dice
    				g.fillOval(28, 52, 10, 10);
    				g.fillOval(48, 78, 10, 10);
    			}
    			if(dice2 == 1)
    			{
    				//Draws two dots on the dice
    				g.fillOval(88, 52, 10, 10);
    				g.fillOval(108, 78, 10, 10);
    			}
    			if(dice1 == 0)
    			{
    				//Draws one dot on the dice
    				g.fillOval(38, 65, 10, 10);
    			}
    			if(dice2 == 0)
    			{
    				//Draws one dot on the dice
    				g.fillOval(98, 65, 10, 10);
    			}
    
    			rollButton.setText("Clear Dice");
    			doRoll = false;
    		}
    		else
    		{
    			repaint();
    			rollButton.setText("Roll Dice");
    			doRoll = true;
    		}
    	}
    }
    Hopefully my poor attempt at documentation throughout the program will help you understand what some statements do, but if not, feel free to ask any questions that you may have and hopefully I'll be able to explain.

    Cheers,
    Greg
    \"Do you know what people are most afraid of?
    What they don\'t understand.
    When we don\'t understand, we turn to our assumptions.\"
    -- William Forrester

  2. #2
    Member
    Join Date
    Oct 2001
    Posts
    84
    Thanx hot_ice. I'm always interested in how other people set out their programs.

  3. #3
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    Here, try this for a trowing-routine (for one die):
    Code:
            double throw = Math.random();
            dice1 = (int) Math.ceil((throw*(MAXIMUMEYES-MINIMUMEYES+1)+(MINIMUMEYES-1)));
    Where MAXIMUM and MINIMUM are the maximum and minimum number of eyes on the die, respectivly. For a normal die, these would be 6 and 1.

    If you feel up to it, you could even create a seperate die-class. In your program, you would create two instances of the class. The class could look something like this:

    Code:
    import java.lang.Math;
    
    public class Die
    {
        private static final int MINIMUMEYES = 1; // minimum number of eyes on the die
        private static final int MAXIMUMEYES = 6; // maximum number of eyes on the die
        
        private int eyes;  // Represents number of eyes trown
       
        public int getEyes()
        // value:  eyes
        {
            return eyes;
        }
    
        public void roll()
        // action: roll the die, and set any value out of {1,2,3,4,5,6} in eyes
        {
            double throw1 = Math.random();
            eyes = (int) Math.ceil((throw1*(MAXIMUMEYES-MINIMUMEYES+1)+(MINIMUMEYES-1)));
        }
    }
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides