DieVisual.java
1    /* 
2     * Attempt to allow the user to roll a standard die 
3     * and let the user see the die be rolled 
4     */
5    
6    package chanceapps;
7    
8    import chance.Die;
9    import painter.SPainter;
10   import shapes.SCircle;
11   import shapes.SSquare;
12   
13   import javax.swing.*;
14   import java.awt.*;
15   import java.util.Random;
16   
17   public class DieVisual {
18   
19       private static void paintTheImage() {
20           //establish objects
21           SPainter chance = new SPainter("Your Roll", 300, 300); chance.setBrushWidth(5);
22           SSquare square = new SSquare(160);
23           SCircle pip = new SCircle(square.side()*0.1);
24           //get die value
25           int dieRoll = rollTheDie();
26           //create the animation
27           paintTheRoll(dieRoll,chance,square,pip);
28           paintTheResult(dieRoll,chance,square,pip);
29           printResult(dieRoll);
30       }
31   
32       private static void printResult(int dieRoll) {
33           System.out.println(dieRoll);
34       }
35   
36       private static void paintTheResult(int dieRoll, SPainter chance, SSquare square, SCircle pip) {
37           //die landing
38           chance.wash();
39           chance.pause(5);
40   
41           if (dieRoll == 1) {
42               faceOne(chance,square,pip);
43           } else if (dieRoll == 2) {
44               faceTwo(chance,square,pip);
45           } else if (dieRoll == 3) {
46               faceThree(chance,square,pip);
47           } else if (dieRoll == 4) {
48               faceFour(chance, square, pip);
49           } else if (dieRoll == 5) {
50               faceFive(chance, square, pip);
51           } else {
52               faceSix(chance, square, pip);
53           }
54       }
55   
56       private static void paintTheRoll(int dieRoll, SPainter chance, SSquare square, SCircle pip) {
57           //set heading
58           int heading = 20;
59           chance.setHeading(heading);
60           //create rgen
61           Random rgen = new Random();
62           int face = (rgen.nextInt(5)+1);
63   
64           //roll animation
65           for(int i = 1; i<=35; i= i+1) {
66               chance.wash();
67               chance.pause(5);
68               if (face == 1) {
69                   faceOne(chance,square,pip);
70               } else if (face == 2) {
71                   faceTwo(chance,square,pip);
72               } else if (face == 3) {
73                   faceThree(chance,square,pip);
74               } else if (face == 4) {
75                   faceFour(chance, square, pip);
76               } else if (face == 5) {
77                   faceFive(chance, square, pip);
78               } else {
79                   faceSix(chance, square, pip);
80               }
81               //set up for next run through
82               face = (rgen.nextInt(5)+1);
83               chance.pause(30);
84               //spin
85               heading = heading +20;
86               if (heading == 360) {
87                   heading = 0;
88               }
89               chance.setHeading(heading);
90           }
91       }
92   
93       private static void faceOne(SPainter chance, SSquare square, SCircle pip) {
94           //die
95           chance.setColor(colorA());
96           chance.paint(square);
97           chance.setColor(colorB());
98           chance.draw(square);
99           //pips
100          chance.paint(pip);
101      }
102  
103      private static void faceTwo(SPainter chance, SSquare square, SCircle pip) {
104          //die
105          chance.setColor(colorA());
106          chance.paint(square);
107          chance.setColor(colorB());
108          chance.draw(square);
109          //pips
110          //pip #1
111          chance.mbk(square.side()*0.25);
112          chance.mlt(square.side()*0.25);
113          chance.paint(pip);
114          chance.moveToCenter();
115          //pip #2
116          chance.mfd(square.side()*0.25);
117          chance.mrt(square.side()*0.25);
118          chance.paint(pip);
119          chance.moveToCenter();
120      }
121  
122      private static void faceThree(SPainter chance, SSquare square, SCircle pip) {
123          //die
124          chance.setColor(colorA());
125          chance.paint(square);
126          chance.setColor(colorB());
127          chance.draw(square);
128          //pips
129          //pip #1
130          chance.mbk(square.side()*0.25);
131          chance.mlt(square.side()*0.25);
132          chance.paint(pip);
133          chance.moveToCenter();
134          //pip #2
135          chance.mfd(square.side()*0.25);
136          chance.mrt(square.side()*0.25);
137          chance.paint(pip);
138          chance.moveToCenter();
139          //pip #3
140          chance.paint(pip);
141      }
142  
143      private static void faceFour(SPainter chance, SSquare square, SCircle pip) {
144          //die
145          chance.setColor(colorA());
146          chance.paint(square);
147          chance.setColor(colorB());
148          chance.draw(square);
149          //pips
150          //pip #1
151          chance.mbk(square.side()*0.25);
152          chance.mlt(square.side()*0.25);
153          chance.paint(pip);
154          chance.moveToCenter();
155          //pip #2
156          chance.mfd(square.side()*0.25);
157          chance.mrt(square.side()*0.25);
158          chance.paint(pip);
159          chance.moveToCenter();
160          //pip #3
161          chance.mfd(square.side()*0.25);
162          chance.mlt(square.side()*0.25);
163          chance.paint(pip);
164          chance.moveToCenter();
165          //pip #4
166          chance.mbk(square.side()*0.25);
167          chance.mrt(square.side()*0.25);
168          chance.paint(pip);
169          chance.moveToCenter();
170      }
171  
172      private static void faceFive(SPainter chance, SSquare square, SCircle pip) {
173          //die
174          chance.setColor(colorA());
175          chance.paint(square);
176          chance.setColor(colorB());
177          chance.draw(square);
178          //pips
179          //pip #1
180          chance.mbk(square.side()*0.25);
181          chance.mlt(square.side()*0.25);
182          chance.paint(pip);
183          chance.moveToCenter();
184          //pip #2
185          chance.mfd(square.side()*0.25);
186          chance.mrt(square.side()*0.25);
187          chance.paint(pip);
188          chance.moveToCenter();
189          //pip #3
190          chance.mfd(square.side()*0.25);
191          chance.mlt(square.side()*0.25);
192          chance.paint(pip);
193          chance.moveToCenter();
194          //pip #4
195          chance.mbk(square.side()*0.25);
196          chance.mrt(square.side()*0.25);
197          chance.paint(pip);
198          chance.moveToCenter();
199          //pip #5
200          chance.paint(pip);
201      }
202  
203      private static void faceSix(SPainter chance, SSquare square, SCircle pip) {
204          //die
205          chance.setColor(colorA());
206          chance.paint(square);
207          chance.setColor(colorB());
208          chance.draw(square);
209          //pips
210          //pip #1
211          chance.mbk(square.side()*0.25);
212          chance.mlt(square.side()*0.25);
213          chance.paint(pip);
214          chance.moveToCenter();
215          //pip #2
216          chance.mfd(square.side()*0.25);
217          chance.mrt(square.side()*0.25);
218          chance.paint(pip);
219          chance.moveToCenter();
220          //pip #3
221          chance.mfd(square.side()*0.25);
222          chance.mlt(square.side()*0.25);
223          chance.paint(pip);
224          chance.moveToCenter();
225          //pip #4
226          chance.mbk(square.side()*0.25);
227          chance.mrt(square.side()*0.25);
228          chance.paint(pip);
229          chance.moveToCenter();
230          //pip #5
231          chance.mrt(square.side()*0.25);
232          chance.paint(pip);
233          chance.moveToCenter();
234          //pip #6
235          chance.mlt(square.side()*0.25);
236          chance.paint(pip);
237          chance.moveToCenter();
238      }
239  
240      private static int rollTheDie() {
241          Die die = new Die();
242          die.roll();
243          System.out.println("And its a ...");
244          int sideRolled = die.top();
245          return sideRolled;
246      }
247  
248      //COLOR HOLD
249      private static Color colorA() {
250          Color a = new Color(153,50,204);
251          return a;
252      }
253      private static Color colorB() {
254          return Color.BLACK;
255      }
256  
257      //required infrastructure
258      public DieVisual() {
259          paintTheImage();
260      }
261      public static void main(String[] args) {
262          SwingUtilities.invokeLater(new Runnable() {
263              public void run() {
264                  new DieVisual();
265              }
266          });
267      }
268  }
269