Best Projects

Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)

Die Roller


Description: Generates random numbers, especially useful for games requiring rolling lots of dice. Really cool, pretty useful. Especially good source for beginners.



//Dieroll.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;

class DieRoll extends JFrame implements ActionListener, KeyListener, ItemListener {

int addend, rolls, sides, total, pos;
boolean addon = false;
boolean condense = true;
String str;
Random rn = new Random();

JPanel row1 = new JPanel();
JRadioButton d2 = new JRadioButton("d2");
JRadioButton d3 = new JRadioButton("d3");
JRadioButton d4 = new JRadioButton("d4");
JRadioButton d6 = new JRadioButton("d6");
JRadioButton d8 = new JRadioButton("d8");

JPanel row2 = new JPanel();
JRadioButton d10 = new JRadioButton("d10");
JRadioButton d12 = new JRadioButton("d12");
JRadioButton d14 = new JRadioButton("d14");
JRadioButton d20 = new JRadioButton("d20", true);
JRadioButton d100 = new JRadioButton("d%");

JPanel row3 = new JPanel();
JLabel lblRolls = new JLabel("Rolls:");
JTextField txtRolls = new JTextField("1",4);

JPanel row4 = new JPanel();
JPanel row41 = new JPanel();
JPanel row42 = new JPanel();
JLabel lblAddend = new JLabel("Addend:");
JLabel lblPM = new JLabel("+");
JTextField txtAddend = new JTextField("0",5);
JButton aPlus = new JButton("+");
JButton aMinus = new JButton("-");

JPanel row5 = new JPanel();
JLabel lblTotal = new JLabel("Final: 1d20 + 0");

JPanel row6 = new JPanel();
JButton generate = new JButton("Generate");
JButton clear = new JButton("Clear");

JLabel theTotal = new JLabel("Total: ");
Font BigFont = new Font("SansSerif", Font.BOLD, 24);

JTextArea output = new JTextArea();
JScrollPane scroller = new JScrollPane(output, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JLabel StatusBar = new JLabel(" ");

JCheckBox chkAddOn = new JCheckBox("Check to Append Data", addon);
JCheckBox chkCondense = new JCheckBox("Check to Condense Ouput", condense);

public static void main(String[] arguments) {
DieRoll frame1 = new DieRoll();
}

public DieRoll() {
super("Java DieRoll Version 2.1");
setSize(450,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout grid6 = new GridLayout(6,1,10,10);
GridLayout grid1 = new GridLayout(1,1,10,10);
GridLayout yo4 = new GridLayout(1,4,10,10);
GridLayout yo2 = new GridLayout(1,2,10,10);
GridLayout yo5 = new GridLayout(1,5,10,10);
GridLayout yo22 = new GridLayout(2,1,10,10);
BorderLayout yoyo = new BorderLayout();
FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER);
FlowLayout layout3 = new FlowLayout(FlowLayout.LEFT);
Container bigPane = getContentPane();
bigPane.setLayout(new BorderLayout());
JPanel pane = new JPanel();
JPanel side1 = new JPanel();
JPanel side2 = new JPanel();
pane.setLayout(yo2);
side1.setLayout(grid6);
side2.setLayout(yoyo);

row2.setLayout(layout2);
row2.add(d100);
row2.add(d20);
row2.add(d14);
row2.add(d12);
row2.add(d10);
side1.add(row2);

row1.setLayout(layout2);
row1.add(d8);
row1.add(d6);
row1.add(d4);
row1.add(d3);
row1.add(d2);
side1.add(row1);

row4.setLayout(layout2);
row4.add(lblRolls);
row4.add(txtRolls);
row4.add(lblAddend);
row4.add(txtAddend);
side1.add(row4);

row3.setLayout(new FlowLayout(FlowLayout.CENTER));
row3.add(lblTotal);
side1.add(row3);

row6.setLayout(yo2);
row6.add(clear);
row6.add(generate);
side1.add(row6);
side1.add(theTotal);

JPanel boxes = new JPanel();
boxes.setLayout(yo22);
boxes.add(chkAddOn);
boxes.add(chkCondense);
side2.add(boxes, BorderLayout.NORTH);
side2.add(scroller, BorderLayout.CENTER);

ButtonGroup dieSides = new ButtonGroup();
dieSides.add(d2);
dieSides.add(d3);
dieSides.add(d4);
dieSides.add(d6);
dieSides.add(d8);
dieSides.add(d10);
dieSides.add(d12);
dieSides.add(d14);
dieSides.add(d20);
dieSides.add(d100);


d2.addActionListener(this);
d3.addActionListener(this);
d4.addActionListener(this);
d6.addActionListener(this);
d8.addActionListener(this);
d10.addActionListener(this);
d12.addActionListener(this);
d14.addActionListener(this);
d20.addActionListener(this);
d100.addActionListener(this);
aPlus.addActionListener(this);
aMinus.addActionListener(this);
generate.addActionListener(this);
clear.addActionListener(this);
txtAddend.addKeyListener(this);
txtRolls.addKeyListener(this);
chkAddOn.addItemListener(this);
chkCondense.addItemListener(this);

// make color
Color theColor = Color.WHITE;
d2.setBackground(theColor);
d3.setBackground(theColor);
d4.setBackground(theColor);
d6.setBackground(theColor);
d8.setBackground(theColor);
d10.setBackground(theColor);
d12.setBackground(theColor);
d14.setBackground(theColor);
d20.setBackground(theColor);
d100.setBackground(theColor);
aPlus.setBackground(theColor);
aMinus.setBackground(theColor);
generate.setBackground(theColor);
clear.setBackground(theColor);
txtAddend.setBackground(theColor);
txtRolls.setBackground(theColor);
chkAddOn.setBackground(theColor);
chkCondense.setBackground(theColor);
boxes.setBackground(theColor);
row1.setBackground(theColor);
row2.setBackground(theColor);
row3.setBackground(theColor);
row4.setBackground(theColor);
row5.setBackground(theColor);
row6.setBackground(theColor);
bigPane.setBackground(theColor);
pane.setBackground(theColor);
side1.setBackground(theColor);
side2.setBackground(theColor);

pane.add(side1);
pane.add(side2);
bigPane.add(pane, BorderLayout.CENTER);
bigPane.add(StatusBar, BorderLayout.SOUTH);
setContentPane(bigPane);
setVisible(true);

theTotal.setFont(BigFont);
output.setLineWrap(true);
output.setWrapStyleWord(true);
output.setMargin(new Insets(5,5,5,5));
sides = 20;
rolls = 1;
pos = 1;
str = "Final: 1d20 + 0";
}

public int randInt(int lo, int hi) {
int n = hi - lo + 1;
int i = rn.nextInt() % n;
if (i < 0)
i = -i;
return lo + i;
}

public void actionPerformed(ActionEvent event) {
String theEvent = event.getActionCommand();
Object source = event.getSource();
if (theEvent.equals("Clear"))
output.setText("");
if (theEvent.equals("Generate"))
rollDice();
if (theEvent.equals("d2"))
sides = 2;
if (theEvent.equals("d3"))
sides = 3;
if (theEvent.equals("d4"))
sides = 4;
if (theEvent.equals("d6"))
sides = 6;
if (theEvent.equals("d8"))
sides = 8;
if (theEvent.equals("d10"))
sides = 10;
if (theEvent.equals("d12"))
sides = 12;
if (theEvent.equals("d14"))
sides = 14;
if (theEvent.equals("d20"))
sides = 20;
if (theEvent.equals("d%"))
sides = 100;
if (theEvent.equals("+")) {
pos = 1;
lblPM.setText("+");
}
if (theEvent.equals("-")) {
pos = -1;
lblPM.setText("-");
}
if (pos > 0)
str = "Final: " + rolls + "d" + sides + " + " + addend;
else
str = "Final: " + rolls + "d" + sides + " - " + addend;
lblTotal.setText(str);
}

public void keyPressed(KeyEvent ke) {

}

public void keyReleased(KeyEvent ke) {
try {
rolls = Integer.parseInt(txtRolls.getText());
addend = Integer.parseInt(txtAddend.getText());
} catch (Exception e) {
return;
}
if (addend >= 0)
str = "Final: " + rolls + "d" + sides + " + " + addend;
else
str = "Final: " + rolls + "d" + sides + " - " + Math.abs(addend);
lblTotal.setText(str);
}

public void keyTyped(KeyEvent ke) {

}

public void itemStateChanged(ItemEvent ie) {
Object source = ie.getSource();
if (source == chkAddOn)
addon = !addon;
if (source == chkCondense)
condense = !condense;
}

int rollDice() {
int ran;
try {
rolls = Integer.parseInt(txtRolls.getText());
addend = Integer.parseInt(txtAddend.getText());
} catch (Exception e) {
StatusBar.setText("ERROR: Addend and Rolls must be positive integers.");
return -1;
}
if (rolls < 1) {
StatusBar.setText("ERROR: Number of rolls must be greater than 1");
return -1;
}
if (!addon) {
output.setText("");
total = 0;
}
// find roll data
int dieRolls[] = new int[sides+1];
int randRolls[] = new int[rolls+1];
for (int i=1; i<=rolls; i++) {
ran = randInt(1,sides);
randRolls[i] = ran;
total += ran;
dieRolls[ran]++;
}
total += (addend*pos);
// output roll data
if (!condense) {
printText("Die Formula: " + str.substring(7, str.length()));
printText("Percentages:");
for (int j=1; j<=sides; j++) {
printText(j + ": " + 100*((float)dieRolls[j] / (float)rolls) + "%");
}
printText("\nRoll Stats:");
for (int k=1; k<=rolls; k++) {
printText("Roll " + k + ": " + randRolls[k]);
}
} else {
printText("Die Formula: " + str.substring(7, str.length()));
printText("Rolls: ");
for (int k=1; k<=rolls; k++) {
if (k != rolls)
output.setText(output.getText() + randRolls[k] + ", ");
else
output.setText(output.getText() + randRolls[k] + ".\n");
}
}
output.setText(output.getText() + "Total: " + total);
theTotal.setText("Total: " + total);
printText("\n");
StatusBar.setText(" ");
return 1;
}

void printText(String text) {
output.setText(output.getText() + text + "\n");
}

}

No comments: