Roll Your Own

<< War Game | OtherProjectsTrailIndex | Practice ProblemWord Morph >>

...Die, that is! Click Here to see it in action

Die.java

import  java.awt.*;
/**
 * A six sided Die.
 * 
 * @author Chris Thiel, OFMCap
 * @version 21 Aug 2010
 */
public class Die
{
    // instance variables - 
    private int size;
    private int x; //top left corner
    private int y;
    private int pips;
    private Graphics g;
    private Color color;

    /**
     * Constructor for objects of class Die
     */
    public Die(Graphics g,  int x, int y, int size)
    {
        this.g=g;
        this.size=size;
        this.x=x;
        this.y=y;
        this.pips=(int)(Math.random()*6.0)+1;
        this.color=Color.WHITE;
    }

    public void setColor(Color c)
    {
        this.color=c;
    }

    public void draw()
    {
        g.setColor(color);
        g.fillRect(x,y,size,size);
        g.setColor(Color.BLACK);
        g.drawRect(x,y,size,size);

        g.setColor(Color.BLACK);
        int dotSize=size/7;
        if (pips%2==1){
            g.fillOval(x+3*dotSize, y+3*dotSize, dotSize,dotSize);
        }
        if (pips>1){
            g.fillOval(x+dotSize, y+dotSize, dotSize,dotSize);
            g.fillOval(x+5*dotSize, y+5*dotSize, dotSize,dotSize);
        }
        if (pips>3){
            g.fillOval(x+dotSize, y+5*dotSize, dotSize,dotSize);
            g.fillOval(x+5*dotSize, y+dotSize, dotSize,dotSize);
        }
        if (pips==6){
            g.fillOval(x+dotSize, y+3*dotSize, dotSize,dotSize);
            g.fillOval(x+5*dotSize, y+3*dotSize, dotSize,dotSize);
        }
    }
}

Roll.java


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
 * Class Roll - click to roll a single 6-sided die
 * 
 * @author Chris Thiel, OFMCap
 * @version 21 Aug 2010
 */
public class Roll extends Applet implements MouseListener
{
    // instance variables - replace the example below with your own
    private Die d;
    private Button rollButton;
    private int count;

    public void init()
    {
        addMouseListener(this);
        count=0;
    }




    /**
     * Paint method for applet.
     * 
     * @param  g   the Graphics object for this applet
     */
    public void paint(Graphics g)
    {
        // simple text displayed on applet
        g.setColor(new Color(150, 75, 25));
        g.fillRect(0, 0, 350, 250);
        g.setColor(Color.ORANGE);
        g.drawString("Click to", 20, 20);
        g.setColor(Color.YELLOW);
        g.drawString("Roll Dice", 20, 40);
        if (count >0)
        {
            count--;
            d = new Die (g, 100,50, 150 );
            d.setColor(Color.ORANGE);
            d.draw();
            repaint();
        }else{
            d = new Die (g, 100,50,150);
            d.draw();
        }
    }
    // 5 MouseListener methods that NEED to be implemented since our Applet implements the MouseListener interface
    public void mouseReleased(MouseEvent e)
    {
        count = (int)(Math.random()*100.0)+75;
        repaint();
    }

    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseClicked(MouseEvent e){}
}

now you try!

  1. Change Roll.java so the die is smaller
  2. Change Roll.java so the die is off to the left
  3. Make another instance of die (maybe call it d2), and draw it to the right of d