/* * @(#)DiceRollerApplet.java * * An applet for creating random numbers * Developed by Michael Sonntag */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; /** An applet to create random numbers. It allows to roll 1 to 20 dice of 2 to 100 sides and add a final bonus of 0 to 50. The last 4 rolls are displayed in a list. For standard rolls (1d4, 1d6, 1d8, 1d10, 1d12, 1d20, 1d100), buttons are provided for faster access. @author Michael Sonntag @version 1.0, 1.9.2000 */ public class DiceRollerApplet extends JApplet implements ActionListener, KeyListener { // The GUI components JLabel title = new JLabel(); JPanel dicePanel = new JPanel(); JTextField numberField = new JTextField(); JLabel dLabel = new JLabel(); JTextField diceField = new JTextField(); JTextField bonusField = new JTextField(); JButton doRoll = new JButton(); JList rollsList = new JList(); DefaultListModel lastRollsModel = new DefaultListModel(); JPanel buttonPanel = new JPanel(); JButton roll_1d4 = new JButton(); JButton roll_1d6 = new JButton(); JButton roll_1d8 = new JButton(); JButton roll_1d10 = new JButton(); JButton roll_1d12 = new JButton(); JButton roll_1d20 = new JButton(); JButton roll_1d100 = new JButton(); JToggleButton plusMinus = new JToggleButton(); /** Initialize the applet. */ public void init() { // This line prevents the "Swing: checked access to system event queue" message seen in some browsers. getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); getContentPane().setLayout(new GridBagLayout()); // Create GUI GridBagConstraints cons=new GridBagConstraints(); cons.gridx=0; cons.gridy=0; cons.gridwidth=6; cons.gridheight=1; cons.weightx=1.0; cons.weighty=0.0; cons.insets=new Insets(10,10,10,10); cons.anchor=GridBagConstraints.NORTH; cons.fill=GridBagConstraints.HORIZONTAL; cons.ipadx=0; cons.ipady=0; title.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); title.setText("Sunny's Dice Roller"); getContentPane().add(title,cons); title.setFont(new Font("Dialog", Font.BOLD, 16)); dicePanel.setLayout(new GridBagLayout()); cons.gridwidth=4; cons.gridy=1; cons.weightx=0.5; cons.anchor=GridBagConstraints.CENTER; cons.fill=GridBagConstraints.HORIZONTAL; cons.insets=new Insets(0,0,10,10); getContentPane().add(dicePanel,cons); numberField.setText("1"); numberField.setColumns(2); numberField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); numberField.setToolTipText("Number of dice to roll"); cons.gridwidth=1; cons.weightx=0.0; cons.anchor=GridBagConstraints.EAST; cons.fill=GridBagConstraints.NONE; cons.insets=new Insets(0,0,0,0); dicePanel.add(numberField,cons); dLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); dLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); dLabel.setText("d"); cons.gridx=1; cons.anchor=GridBagConstraints.CENTER; cons.insets=new Insets(0,5,0,5); cons.ipadx=0; dicePanel.add(dLabel,cons); diceField.setText("6"); diceField.setColumns(3); diceField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); diceField.setToolTipText("Sides of the dice to roll"); cons.gridx=2; cons.anchor=GridBagConstraints.WEST; cons.insets=new Insets(0,5,0,5); dicePanel.add(diceField,cons); plusMinus.setText("+"); plusMinus.setSelected(false); cons.gridx=3; cons.ipadx=5; cons.fill=GridBagConstraints.NONE; cons.anchor=GridBagConstraints.CENTER; dicePanel.add(plusMinus,cons); bonusField.setText("0"); bonusField.setColumns(2); bonusField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); bonusField.setToolTipText("Number to add to the roll"); cons.gridx=4; cons.ipadx=0; cons.anchor=GridBagConstraints.WEST; dicePanel.add(bonusField,cons); doRoll.setText("Roll the dice!"); doRoll.setToolTipText("Roll the dice!"); cons.gridx=0; cons.gridy=2; cons.gridwidth=5; cons.anchor=GridBagConstraints.CENTER; cons.insets=new Insets(10,10,10,10); cons.ipadx=0; getContentPane().add(doRoll,cons); buttonPanel.setLayout(new GridBagLayout()); cons.gridy=3; cons.gridwidth=6; cons.weightx=1.0; cons.weighty=1.0; cons.fill=GridBagConstraints.BOTH; cons.insets=new Insets(0,0,0,0); getContentPane().add(buttonPanel,cons); rollsList.setModel(lastRollsModel); rollsList.setAutoscrolls(false); rollsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); rollsList.setToolTipText("The previous rolls (last one at the end)"); rollsList.setVisibleRowCount(4); rollsList.setFixedCellWidth(90); lastRollsModel.addElement(" "); lastRollsModel.addElement(" "); lastRollsModel.addElement(" "); lastRollsModel.addElement(" "); cons.gridx=5; cons.gridy=1; cons.gridwidth=1; cons.gridheight=2; cons.weightx=0.0; cons.weighty=0.0; cons.fill=GridBagConstraints.NONE; cons.insets=new Insets(0,0,10,10); getContentPane().add(rollsList,cons); Border border=BorderFactory.createBevelBorder(BevelBorder.LOWERED); rollsList.setBorder(border); roll_1d4.setText("1d4+0"); roll_1d4.setToolTipText("Roll 1d4+0"); cons.gridx=0; cons.gridy=0; cons.gridwidth=1; cons.gridheight=1; cons.insets=new Insets(0,10,5,5); buttonPanel.add(roll_1d4,cons); roll_1d6.setText("1d6+0"); roll_1d6.setToolTipText("Roll 1d6+0"); cons.gridx=1; cons.insets=new Insets(0,5,5,5); buttonPanel.add(roll_1d6,cons); roll_1d8.setText("1d8+0"); roll_1d8.setToolTipText("Roll 1d8+0"); cons.gridx=2; cons.insets=new Insets(0,5,5,10); buttonPanel.add(roll_1d8,cons); roll_1d10.setText("1d10+0"); roll_1d10.setToolTipText("Roll 1d10+0"); cons.gridx=0; cons.gridy=1; cons.insets=new Insets(5,10,5,5); buttonPanel.add(roll_1d10,cons); roll_1d12.setText("1d12+0"); roll_1d12.setToolTipText("Roll 1d12+0"); cons.gridx=1; cons.insets=new Insets(5,5,5,5); buttonPanel.add(roll_1d12,cons); roll_1d20.setText("1d20+0"); roll_1d20.setToolTipText("Roll 1d20+0"); cons.gridx=2; cons.insets=new Insets(5,5,5,10); buttonPanel.add(roll_1d20,cons); roll_1d100.setText("1d100+0"); roll_1d100.setToolTipText("Roll 1d100+0"); cons.gridx=1; cons.gridy=2; cons.insets=new Insets(5,5,10,5); buttonPanel.add(roll_1d100,cons); // The listeners doRoll.addActionListener(this); roll_1d4.addActionListener(this); roll_1d6.addActionListener(this); roll_1d8.addActionListener(this); roll_1d10.addActionListener(this); roll_1d12.addActionListener(this); roll_1d20.addActionListener(this); roll_1d100.addActionListener(this); plusMinus.addActionListener(this); // Pressing enter in a field also initiates a roll numberField.addKeyListener(this); diceField.addKeyListener(this); bonusField.addKeyListener(this); getRootPane().setDefaultButton(doRoll); } /** When the user clicked a button. @param event only source used to identify the clicked component */ public void actionPerformed(ActionEvent event) { Object object = event.getSource(); // Hit a button? if (object == doRoll) doRoll_actionPerformed(event); else if (object == roll_1d4) makeRoll(1,4,0); else if (object == roll_1d6) makeRoll(1,6,0); else if (object == roll_1d8) makeRoll(1,8,0); else if (object == roll_1d10) makeRoll(1,10,0); else if (object == roll_1d12) makeRoll(1,12,0); else if (object == roll_1d20) makeRoll(1,20,0); else if (object == roll_1d100) makeRoll(1,100,0); else if (object == plusMinus) { if(plusMinus.isSelected()) plusMinus.setText("-"); else plusMinus.setText("+"); } } /** When a key is pressed (ignored here). @param e the key event */ public void keyPressed(KeyEvent e) { } /** When a key is released. If it was the enter key, the dice are rolled. @param e the key event */ public void keyReleased(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_ENTER) { // Enter pressed: roll the dice doRoll_actionPerformed(null); } } /** When a key is typed (ignored here). @param e the key event */ public void keyTyped(KeyEvent e) { } /** The random number generator. E. g. 2d6+1 (2 dice of 6 sides, add 1 afterwards) would result in numbers between 3 and 13. @param number the number of dice to roll @param dice the sides of the dice to roll @param bonus the number to add after rolling all the dice @return the random result */ public static int rollDice(int number,int dice,int bonus) { int total=bonus; for(int i=0;i20) { JOptionPane.showMessageDialog(this,"Only numbers between 1 and 20 allowed!","Wrong number of dice",JOptionPane.ERROR_MESSAGE); numberField.requestFocus(); return; } // Check sides of dice str=diceField.getText(); try { dice=Integer.parseInt(str); } catch(NumberFormatException e) { JOptionPane.showMessageDialog(this,"Only dice between 2 and 100 allowed!","Wrong dice",JOptionPane.ERROR_MESSAGE); diceField.requestFocus(); return; } if(dice<2 || dice>100) { JOptionPane.showMessageDialog(this,"Only dice between 2 and 100 allowed!","Wrong dice",JOptionPane.ERROR_MESSAGE); diceField.requestFocus(); return; } // Check bonus str=bonusField.getText(); try { bonus=Integer.parseInt(str); } catch(NumberFormatException e) { JOptionPane.showMessageDialog(this,"Only boni between 0 and 50 allowed!","Wrong bonus",JOptionPane.ERROR_MESSAGE); bonusField.requestFocus(); return; } if(bonus<0 || bonus>50) { JOptionPane.showMessageDialog(this,"Only boni between 0 and 50 allowed!","Wrong bonus",JOptionPane.ERROR_MESSAGE); bonusField.requestFocus(); return; } if(plusMinus.isSelected()) bonus=-bonus; // Display result lastRollsModel.remove(0); lastRollsModel.addElement(""+number+"d"+dice+(plusMinus.isSelected()?"":"+")+bonus+"="+DiceRollerApplet.rollDice(number,dice,bonus)); rollsList.setSelectedIndex(3); } /** Return author, version and copyright info on the applet. @return a string with author, version and copyright info */ public String getAppletInfo() { return "Sunny's Dice Roller\nby Michael Sonntag, 2000\nVersion 1.0\n\nPermission to use granted for private, non-commercial use."; } }