Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Draw Dice

843806Mar 3 2008 — edited Mar 3 2008
Hi wonder if you can help. I'm designing a dice throwing game. Ive come up with the code to produce a die value amount when a button is pressed and output this in a textfield. However i want to draw the die value, graphically, so represent the dots. But i'm stumped as how to do this, any suggestions would be most welcome!
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.Font;
import java.util.Random;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.event.KeyEvent;

public class diceQuestion extends JFrame {

	private static final long serialVersionUID = 1L;
	static Random rand = new Random();  //  @jve:decl-index=0:
	private int die1 = 1;
	private JPanel jContentPane = null;
	private JButton rollBtn = null;
	private JTextField dieValueTF = null;
	private JLabel dieLabel = null;
	private int top = 50, left = 100;

	public diceQuestion() {
		super();
		initialize();
	}

	private void initialize() {
		this.setSize(637, 316);
		this.setContentPane(getJContentPane());
		this.setTitle("Dice & Question");
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			dieLabel = new JLabel();
			dieLabel.setBounds(new Rectangle(147, 28, 67, 34));
			dieLabel.setText("Die Value:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(new Color(55, 119, 27));
			jContentPane.add(getRollBtn(), null);
			jContentPane.add(getDieValueTF(), null);
			jContentPane.add(dieLabel, null);
			displayDie();
		}
		return jContentPane;
	}

	private JButton getRollBtn() {
		if (rollBtn == null) {
			rollBtn = new JButton();
			rollBtn.setBounds(new Rectangle(31, 27, 114, 31));
			rollBtn.setFont(new Font("Verdana", Font.BOLD, 14));
			rollBtn.setForeground(new Color(153, 153, 0));
			rollBtn.setMnemonic(KeyEvent.VK_ENTER);
			rollBtn.setText("Roll Die!");
			rollBtn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					 displayDie();
					 roll();
				}
			});
		}
		return rollBtn;
	}

	private JTextField getDieValueTF() {
		if (dieValueTF == null) {
			dieValueTF = new JTextField();
			dieValueTF.setBounds(new Rectangle(217, 32, 26, 26));
			dieValueTF.setHorizontalAlignment(JTextField.CENTER);
		}
		return dieValueTF;
	}

	void displayDie(){
		dieValueTF.setText(String.valueOf(die1));
	}

    void roll() {
        die1 = rand.nextInt(6) + 1;
    }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 31 2008
Added on Mar 3 2008
1 comment
538 views