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.awt.event.*;
import javax.swing.Timer;
/**
 * Class Roll - click to roll a single 6-sided die
 * 
 * @author Chris Thiel, OFMCap
 * @version 21 Aug 2010 Applet Version
 * @version 25 Sep 2024 Application Version
 */
public class Roll extends Frame implements MouseListener, ActionListener
{
    // instance variables - replace the example below with your own
    private Die d;
    private Button rollButton;
    private int count;
    private Timer timer;

    public Roll()
    {
        super("Roll version 1.0");
        this.setSize(500,500);
        this.addMouseListener(this);
        count = -1;
        timer = new Timer(800,this);
        timer.start();
        this.addWindowListener(new WindowListener() {
                public void windowClosing(WindowEvent e) {System.exit(0);}

                public void windowClosed(WindowEvent e) {}

                public void windowOpened(WindowEvent e) {}

                public void windowIconified(WindowEvent e) {}

                public void windowDeiconified(WindowEvent e) {}

                public void windowActivated(WindowEvent e) {}

                public void windowDeactivated(WindowEvent e) {}
            }); 
    }


    /**
     * Paint method 
     * 
     * @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, this.getWidth(), this.getHeight());
        g.setColor(Color.ORANGE);
        g.drawString("Click to", 20, 40);
        g.setColor(Color.YELLOW);
        g.drawString("Roll Dice", 20, 60);
         if (count > 0)
        {
            count--;
            d = new Die (g, 100,50, 150 );
            d.setColor(Color.ORANGE);
            d.draw();
            g.drawString("pips = "+ d.getPips(), 20, 80);
            g.setColor(Color.YELLOW);
            repaint();
        }else{
            d = new Die (g, 100,50,150);
            d.draw();
            g.setColor(Color.CYAN);
            g.drawString("pips = "+ d.getPips(), 20, 80);

        }
    }
    // 5 MouseListener methods that NEED to be implemented since our Applet implements the MouseListener interface
    public void mouseReleased(MouseEvent e)
    {
        count = (int)(Math.random()*3500.0)+1500;
        repaint();
    }

    public void mouseEntered(MouseEvent e){}

    public void mouseExited(MouseEvent e){}

    public void mousePressed(MouseEvent e){}

    public void mouseClicked(MouseEvent e){}
    // Method required to implement the Action Listener 
    // for the timer
    public void actionPerformed(ActionEvent e) {
        if (count > 0){

            repaint();
            timer.start();
        } else 
          timer.stop();
    }

    public static void main(String[] args)
    {
        Roll app= new Roll();
        app.setVisible(true);
    }
}

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