Created
March 29, 2013 15:23
-
-
Save MarkyC/5271514 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package name.marcocirillo; | |
| import java.awt.BorderLayout; | |
| import java.awt.GridLayout; | |
| import java.awt.HeadlessException; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import javax.imageio.ImageIO; | |
| import javax.swing.ImageIcon; | |
| import javax.swing.JButton; | |
| import javax.swing.JFrame; | |
| import javax.swing.JLabel; | |
| import javax.swing.JPanel; | |
| import javax.swing.UIManager; | |
| public class DiceGame extends JFrame { | |
| private static final long serialVersionUID = -4687588888946630999L; | |
| static ImageIcon[] dice = new ImageIcon[6]; | |
| JLabel[] diceLabel = new JLabel[6]; | |
| JButton[] reRoll = new JButton[6]; | |
| public DiceGame(String title) throws HeadlessException { | |
| super(title); | |
| this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); | |
| try { | |
| // Set OS look and feel | |
| UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
| } catch (Exception e) {} | |
| try { | |
| dice[0] = createIcon("img/dice1.png"); | |
| dice[1] = createIcon("img/dice2.png"); | |
| dice[2] = createIcon("img/dice3.png"); | |
| dice[3] = createIcon("img/dice4.png"); | |
| dice[4] = createIcon("img/dice5.png"); | |
| dice[5] = createIcon("img/dice6.png"); | |
| } catch (IOException e) { | |
| System.out.println("Failed to load image."); | |
| e.printStackTrace(); | |
| System.exit(1); | |
| } | |
| GridLayout layout = new GridLayout(1,6); | |
| JPanel top = new JPanel(); | |
| top.setLayout(layout); | |
| JPanel bottom = new JPanel(); | |
| bottom.setLayout(layout); | |
| for (int i = 0; i < diceLabel.length; i++) { | |
| diceLabel[i] = new JLabel(roll()); | |
| top.add(diceLabel[i]); | |
| } | |
| for (int i = 0; i < reRoll.length; i++) { | |
| reRoll[i] = new JButton("Reroll Die"); | |
| reRoll[i].addActionListener(new ActionListener() { | |
| @Override | |
| public void actionPerformed(ActionEvent e) { | |
| JButton source = (JButton) e.getSource(); | |
| if (source == reRoll[0]){ | |
| diceLabel[0].setIcon(roll()); | |
| } else if (source == reRoll[1]){ | |
| diceLabel[1].setIcon(roll()); | |
| } else if (source == reRoll[2]){ | |
| diceLabel[2].setIcon(roll()); | |
| } else if (source == reRoll[3]){ | |
| diceLabel[3].setIcon(roll()); | |
| } else if (source == reRoll[4]){ | |
| diceLabel[4].setIcon(roll()); | |
| } else if (source == reRoll[5]){ | |
| diceLabel[5].setIcon(roll()); | |
| } | |
| } | |
| }); | |
| bottom.add(reRoll[i]); | |
| } | |
| this.add(top, BorderLayout.NORTH); | |
| this.add(bottom, BorderLayout.SOUTH); | |
| this.pack(); | |
| } | |
| public static ImageIcon roll() { | |
| return dice[(int) (Math.random() * 6)]; | |
| } | |
| public static ImageIcon createIcon(String path) throws IOException { | |
| ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | |
| InputStream input = classLoader.getResourceAsStream(path); | |
| return new ImageIcon(ImageIO.read(input)); | |
| } | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| new DiceGame("My Dice Game").setVisible(true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment