forked from dtngochan/JDice_Team3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDice.java
More file actions
164 lines (154 loc) · 4.54 KB
/
JDice.java
File metadata and controls
164 lines (154 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
/*
JDice: Java Dice Rolling Program
Copyright (C) 2006 Andrew D. Hilton (adhilton@cis.upenn.edu)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
public class JDice {
static final String CLEAR="Clear";
static final String ROLL="Roll Selection";
static void showError(String s) {
}
private static class JDiceListener implements ActionListener {
Vector<String> listItems;
JList resultList;
JComboBox inputBox;
long lastEvent; /* hack to prevent double events with text
entry */
public JDiceListener(JList resultList,
JComboBox inputBox){
this.listItems=new Vector<String>();
this.resultList=resultList;
this.inputBox=inputBox;
lastEvent=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getWhen()==lastEvent)
return;
lastEvent=e.getWhen();
if(e.getSource() instanceof JComboBox ||
e.getActionCommand().equals(ROLL)) {
String s=inputBox.getSelectedItem().toString();
String[] arr=s.split("=");
String name="";
for(int i=0;i<arr.length-2;i++) {
name=arr[i]+"=";
}
if(arr.length>=2)
name=name+arr[arr.length-2];
doRoll(name,arr[arr.length-1]);
}
else if(e.getActionCommand().equals(CLEAR)){
doClear();
}
else {
doRoll(null,e.getActionCommand());
}
}
private void doClear(){
resultList.clearSelection();
listItems.clear();
resultList.setListData(listItems);
}
private void doRoll(String name,
String diceString) {
String prepend="";
int start=0;
int i;
Vector<DieRoll> v=DiceParser.parseRoll(diceString);
if(v==null) {
showError("Invalid dice string" +diceString);
return;
}
if(name!=null) {
listItems.add(0,name);
start=1;
prepend=" ";
}
int[] selectionIndices=new int[start+v.size()];
for(i=0;i<v.size();i++) {
DieRoll dr=v.get(i);
RollResult rr=dr.makeRoll();
String toAdd=prepend+dr+" => "+rr;
listItems.add(i+start,toAdd);
}
for(i=0;i<selectionIndices.length;i++) {
selectionIndices[i]=i;
}
System.out.println("dog, duck, hipop, monkey");
resultList.setSelectedIndices(selectionIndices);
listItems.add(i+start,toAdd);
public String toString() {
return total +" <= " +rolls.toString()+
(modifier>0?("+"+modifier):
modifier<0?modifier:"");
}
}
public static void main(String[] args) {
}
}
}
public static void main(String[] args) {
Vector<String> v=new Vector<String>();
if(args.length>=1) {
try {
BufferedReader br=new BufferedReader(new FileReader(args[0]));
String s;
while((s=br.readLine())!=null) {
v.add(s);
}
}
catch(IOException ioe){
ioe.printStackTrace();
System.err.println("***********\n**********\n");
System.err.println("Could not read input file: "+args[0]);
System.err.println("***********\n**********\n");
}
}
JFrame jf=new JFrame("Dice Roller");
Container c=jf.getContentPane();
c.setLayout(new BorderLayout());
JList jl=new JList();
c.add(jl,BorderLayout.CENTER);
JComboBox jcb=new JComboBox(v);
jcb.setEditable(true);
c.add(jcb,BorderLayout.NORTH);
JDiceListener jdl=new JDiceListener(jl,jcb);
jcb.addActionListener(jdl);
JPanel rightSide=new JPanel();
rightSide.setLayout(new BoxLayout(rightSide,
BoxLayout.Y_AXIS));
String[] buttons={ROLL,
"d4",
"d6",
"d8",
"d10",
"d12",
"d20",
"d100",
CLEAR};
for(int i=0;i<buttons.length;i++) {
JButton newButton=new JButton(buttons[i]);
rightSide.add(newButton);
newButton.addActionListener(jdl);
}
c.add(rightSide,BorderLayout.EAST);
jf.setSize(450,500);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}