I want to create a Dice Rolling Simulator. This is what I've gotten so far.
public class RollDice {
private static class rollDice extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
// custom draw code goes here
}
}
public static void main(String[] args) {
JLabel message = new JLabel ("\nRoll the Die!\n", JLabel.CENTER);
message.setForeground(Color.BLACK);
message.setBackground(Color.WHITE);
message.setFont(new Font("Courier", Font.PLAIN, 25));
message.setOpaque(true);
JButton roll = new JButton("ROLL");
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(message, BorderLayout.NORTH);
content.add(roll, BorderLayout.SOUTH);
JFrame window = new JFrame("Roll Dice");
window.setContentPane(content);
window.setSize(250,300);
window.setLocation(300,300);
window.setVisible(true);
}
}
I have gotten a JFrame, JLabel, and the Button which says roll, the simple things.
What I am trying to figure out is how to create two dice in the JPanel, and also how to make it roll when the button "ROLL" is clicked, using math.Random
and Graphics.
I would appreciate if it was as simple as possible, since I am not very advanced in the programming world and recently started. I would appreciate it if you would try to explain as detailed as possible before you give me the answer, so that I have a chance to try and figure it out myself beforehand.
Thanks!