Roll Dice

Download

.jar file
source code

Java Code

Main.java
package dice;

/**
 * Main.java
 * Created by Stijn Strickx on Feb. 16 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */
public class Main {

    public static void main(String[] args) {
        Dice dice = new Dice();
        dice.start();
    }

}
ThrowDice.java
package dice;

import java.util.Random;
import java.util.TimerTask;
import javax.swing.Icon;
import javax.swing.JLabel;

/**
 * ThrowDice.java
 * Created by Stijn Strickx on Feb. 16 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */
class ThrowDice extends TimerTask {
    private JLabel dice1;
    private JLabel dice2;
    private JLabel text;
    private Random rg = new Random();
    private IconGetter getter;
    private int count;

    public ThrowDice(JLabel dice1, JLabel dice2, JLabel text) {
        this.dice1 = dice1;
        this.dice2 = dice2;
        this.text = text;
        count = 25;
        getter = new IconGetter();
    }
    public void run(){
        if(count > 0){
            count --;
            int num1 = rg.nextInt(6);
            int num2 = rg.nextInt(6);
            Icon icon1 = getter.getIcon("stone" + (num1+1) + ".gif");
            Icon icon2 = getter.getIcon("stone" + (num2+1) + ".gif");
            dice1.setIcon(icon1);
            dice2.setIcon(icon2);
            text.setText("Total: " + (num1+num2+2));
        }
        else{
            this.cancel();
        }
    }
}
Dice.java
package dice;

import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * Dice.java
 * Created by Stijn Strickx on Feb. 16 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */
class Dice {
    private IconGetter getter;

    public Dice(){
        getter = new IconGetter();
    }

    public void start() {
        
        JLabel dice1 = new JLabel(getter.getIcon("stone1.gif"));
        JLabel dice2 = new JLabel(getter.getIcon("stone1.gif"));
        JButton button = new JButton("Throw");
        JLabel text = new JLabel("Total: 2");
        JFrame window = new JFrame("throw dice (c) Stijn Strickx");
        Container cp = window.getContentPane();
        cp.setLayout(new FlowLayout());
        cp.add(dice1);
        cp.add(dice2);
        cp.add(button);
        cp.add(text);
        
        button.addActionListener(new ButtonListener(dice1, dice2, text));
        
        window.pack();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
}
IconGetter.java
package dice;

import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 * IconGetter.java
 * Created by Stijn Strickx on Feb. 16 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */
public class IconGetter {
    
    public Icon getIcon(String name){
        return new ImageIcon(Dice.class.getResource("/dice/images/" + name));
    }
}
ButtonListener.java
package dice;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import javax.swing.JLabel;

/**
 * ButtonListener.java
 * Created by Stijn Strickx on Feb. 16 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */
class ButtonListener implements ActionListener {
    
    private JLabel dice1;
    private JLabel dice2;
    private JLabel text;


    public ButtonListener(JLabel dice1, JLabel dice2, JLabel text) {
        this.dice1 = dice1;
        this.dice2 = dice2;
        this.text = text;
    }

    public void actionPerformed(ActionEvent e) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new ThrowDice(dice1, dice2, text), 0, 100);
    }
}

Home | Code | Learn
© 2007-2008 ProgLogic, all rights reserved. | ProgLogic.com is created by Stijn Strickx. | e-mail