Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Random;
- public class DiceSimulator extends JFrame{
- private JPanel dicePanel;
- private JPanel buttonPanel;
- private JButton button;
- private JLabel dice1;
- private JLabel dice2;
- private String[] dice = {"C:\\Users\\Nicolas\\Pictures\\Dice\\die1.jpg",
- "C:\\Users\\Nicolas\\Pictures\\Dice\\die2.jpg",
- "C:\\Users\\Nicolas\\Pictures\\Dice\\die3.jpg",
- "C:\\Users\\Nicolas\\Pictures\\Dice\\die4.jpg",
- "C:\\Users\\Nicolas\\Pictures\\Dice\\die5.jpg",
- "C:\\Users\\Nicolas\\Pictures\\Dice\\die6.jpg"};
- public DiceSimulator()
- {
- super("Dice Simulator");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
- buildDicePanel();
- buildButtonPanel();
- add(dicePanel, BorderLayout.CENTER);
- add(buttonPanel, BorderLayout.SOUTH);
- pack();
- setVisible(true);
- }
- public void buildDicePanel()
- {
- dicePanel = new JPanel();
- dice1 = new JLabel("");
- dice2 = new JLabel("");
- dice1.setIcon(new ImageIcon(dice[0]));
- dice2.setIcon(new ImageIcon(dice[0]));
- dicePanel.add(dice1);
- dicePanel.add(dice2);
- }
- public void buildButtonPanel()
- {
- buttonPanel = new JPanel();
- button = new JButton("Roll the Dice");
- button.addActionListener(new ButtonListener());
- buttonPanel.add(button);
- }
- private class ButtonListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- Random rand = new Random();
- int roll1 = rand.nextInt(6);
- int roll2 = rand.nextInt(6);
- dice1.setIcon(new ImageIcon(dice[roll1]));
- dice2.setIcon(new ImageIcon(dice[roll2]));
- pack();
- }
- }
- public static void main(String[] args)
- {
- new DiceSimulator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement