Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3653 | schaersvoo | 1 | /* |
2 | Copyright 2004-2006 David P. Little |
||
3 | license: |
||
4 | Unless otherwise stated, the above applets were written by David Little. |
||
5 | They may be used without permission from the author for home and/or educational (non-profit) purposes only. |
||
6 | Any other use must be approved by the author. |
||
7 | |||
8 | Modified for wims interactive usage with permission of the author. |
||
9 | |||
10 | J.M. Evers |
||
11 | */ |
||
12 | import java.awt.*; |
||
13 | import java.awt.geom.*; |
||
14 | import java.awt.event.*; |
||
15 | import java.awt.image.*; |
||
16 | import javax.swing.*; |
||
17 | |||
18 | public class SplashPanel extends JPanel implements MouseListener,Runnable{ |
||
19 | Plinko plinko; |
||
20 | double w; |
||
21 | double h; |
||
22 | int TOTAL; // total number of balls that have been dropped |
||
23 | int[] HIST; // number of balls in each bin; |
||
24 | double MAX; // maximum number of balls in a single bin |
||
25 | Point[][] PINS; // coordinates of pins (including one at the base of each bin) |
||
26 | double DIST; // vertical distance between pins |
||
27 | double BALL_RAD; // radius of ball |
||
28 | int PIN_RAD; // radius of pin |
||
29 | int BINS = 20; // total number of bins |
||
30 | PlinkoBall FIRST_BALL; // represents beginning of doubly linked list of plinko balls |
||
31 | int BALL_COUNT; |
||
32 | int BOTTOM_MARGIN = 5; |
||
33 | // private static byte[] data; |
||
34 | private final static int hx = 15; |
||
35 | private final static int hy = 15; |
||
36 | private final static int bgGrey = 192; |
||
37 | Image[] IMAGES; |
||
38 | boolean newBackground = true; |
||
39 | boolean active = true; |
||
40 | Thread thread; |
||
41 | public SplashPanel( Plinko plinko ){ |
||
42 | setBackground( Color.white ); |
||
43 | addMouseListener( this ); |
||
44 | this.plinko = plinko; |
||
45 | for ( int i = 0; i<25; i++ ){ |
||
46 | dropBall(); |
||
47 | FIRST_BALL.ROW = 10+(int)(10*Math.random()); |
||
48 | FIRST_BALL.COL = (int)(FIRST_BALL.ROW*Math.random()); |
||
49 | FIRST_BALL.C = 10; |
||
50 | FIRST_BALL.t = i%11; |
||
51 | } |
||
52 | start(); |
||
53 | } |
||
54 | |||
55 | public void start(){ |
||
56 | active = true; |
||
57 | thread = new Thread(this); |
||
58 | thread.start(); |
||
59 | } |
||
60 | |||
61 | |||
62 | public void run(){ |
||
63 | while ( active ){ |
||
64 | if ( Math.random() < 0.4 ){ |
||
65 | dropBall(); |
||
66 | FIRST_BALL.ROW = 11; |
||
67 | FIRST_BALL.COL = (int)(FIRST_BALL.ROW*Math.random()); |
||
68 | FIRST_BALL.C = 10; |
||
69 | } |
||
70 | repaint(); |
||
71 | try {Thread.sleep(100);} catch (InterruptedException e){} |
||
72 | } |
||
73 | } |
||
74 | |||
75 | |||
76 | public void dropBall(){ |
||
77 | if ( FIRST_BALL == null ){ |
||
78 | FIRST_BALL = new PlinkoBall(); |
||
79 | } else { |
||
80 | FIRST_BALL.previousBall = new PlinkoBall(); |
||
81 | FIRST_BALL.previousBall.nextBall = FIRST_BALL; |
||
82 | FIRST_BALL = FIRST_BALL.previousBall; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | |||
87 | //Image backImage; |
||
88 | Image background; |
||
89 | Graphics2D backGraphics; |
||
90 | int FUDGE = 200; |
||
91 | |||
92 | public void setup(){ |
||
93 | if(plinko.wims_exercise == true){ FUDGE = 100;} |
||
94 | if ( h-BOTTOM_MARGIN<h/2 ){ |
||
95 | DIST = (double)(h-BOTTOM_MARGIN)/BINS; |
||
96 | } else { |
||
97 | DIST = (double)(w-10)/(2*BINS); |
||
98 | } |
||
99 | |||
100 | PIN_RAD = (int)DIST/9 + 1; |
||
101 | BALL_RAD = Math.max(2*DIST/7,2.0) + 1; |
||
102 | |||
103 | // create images of colored balls |
||
104 | IMAGES = new Image[ PlinkoBoard.COLORS.length ]; |
||
105 | Graphics2D g; |
||
106 | int red; |
||
107 | int green; |
||
108 | int blue; |
||
109 | for ( int i=0; i<PlinkoBoard.COLORS.length; i++ ){ |
||
110 | IMAGES[i] = PlinkoBoard.getBall( BALL_RAD, PlinkoBoard.COLORS[i] ); |
||
111 | } |
||
112 | |||
113 | PINS = new Point[BINS][]; |
||
114 | for (int i=0; i<BINS; i++){ |
||
115 | PINS[i] = new Point[i+1]; |
||
116 | for (int j=0; j<=i; j++){ |
||
117 | PINS[i][j]= new Point((int)(DIST*(2*j-i)+w/2)-FUDGE/2,(int)(DIST*(i+1))-(FUDGE-15)); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | // pins at the base of each bin |
||
122 | for (int i=0; i<BINS; i++){ |
||
123 | PINS[BINS-1][i]= new Point((int)(DIST*(2*i-BINS+1)+w/2),(int)(h-30-BALL_RAD)); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | |||
128 | public void paintComponent( Graphics graphics ){ |
||
129 | // have a copy of the background on which to draw |
||
130 | w = getWidth(); |
||
131 | h = getHeight(); |
||
132 | |||
133 | w = Math.max( getWidth(),getHeight() ) + FUDGE; |
||
134 | h = w; |
||
135 | |||
136 | Graphics2D g = (Graphics2D)graphics; |
||
137 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); |
||
138 | |||
139 | if ( background == null || background.getWidth(this) != h || background.getHeight(this) != w ){ |
||
140 | setup(); |
||
141 | background = createImage( (int)w, (int)h ); |
||
142 | drawBackground((int)w,(int)h); |
||
143 | } |
||
144 | |||
145 | g.drawImage( background, 0, 0, this ); |
||
146 | |||
147 | // run through all active balls and draw them on the image |
||
148 | PlinkoBall ball = FIRST_BALL; |
||
149 | |||
150 | int red; |
||
151 | int green; |
||
152 | int blue; |
||
153 | while ( ball != null ){ |
||
154 | graphics.drawImage( IMAGES[ball.spaz], (int)(ball.X - BALL_RAD), (int)(ball.Y - 2*BALL_RAD - PIN_RAD + 1), this ); |
||
155 | if ( active ) increment( ball ); |
||
156 | ball = ball.nextBall; |
||
157 | } |
||
158 | //g.drawImage(image,0,0,this); |
||
159 | |||
160 | w = getWidth(); |
||
161 | h = getHeight(); |
||
162 | g.setColor( new Color( 128,128,128,220 ) ); |
||
163 | g.setFont( new Font("Helvetica",Font.BOLD,plinko.font_size) ); |
||
164 | //String str = "CLICK ANYWHERE TO BEGIN"; |
||
165 | String str = plinko.click_text; |
||
166 | |||
167 | g.drawString( str, (int)(w-g.getFontMetrics().stringWidth(str)-5), (int)(h-5) ); |
||
168 | } |
||
169 | |||
170 | |||
171 | public void increment( PlinkoBall ball ){ |
||
172 | // if ball has landed on pin, reset t to 0 and pick a direction |
||
173 | if ( ball.t == ball.C && ball.ROW < BINS-2 ){ |
||
174 | ball.ROW++; |
||
175 | ball.COL += ball.DIR; |
||
176 | ball.t = 0; |
||
177 | ball.DIR = 0; |
||
178 | if ( Math.random() < 0.5 ) ball.DIR = 1; |
||
179 | |||
180 | ball.C = 10; |
||
181 | |||
182 | //if ( C>1 ) C += spaz; |
||
183 | } |
||
184 | |||
185 | double dx = DIST*ball.t*(2*ball.DIR - 1)/ball.C; |
||
186 | |||
187 | if ( ball.ROW < 0 ){ // ball falling onto top pin |
||
188 | ball.X = PINS[0][0].x; |
||
189 | dx = Math.abs(dx); |
||
190 | ball.Y = PINS[0][0].y - DIST + DIST*ball.t*ball.t/(ball.C*ball.C); |
||
191 | } else if ( ball.ROW < BINS-2 ) { |
||
192 | ball.X = PINS[ball.ROW][ball.COL].x + dx; |
||
193 | dx = Math.abs(dx); |
||
194 | //Y = PlinkoBoard.PINS[ROW][COL].y - (int)(dx*(A*dx/PlinkoBoard.DIST+B)); |
||
195 | ball.Y = PINS[ball.ROW][ball.COL].y - DIST*PlinkoBoard.DYS[ball.C][ball.t]; |
||
196 | } else { // ball falling into bin |
||
197 | ball.X = PINS[ball.ROW][ball.COL].x + dx; |
||
198 | if ( dx>0 ){ |
||
199 | ball.X = Math.min(ball.X,PINS[ball.ROW][ball.COL].x + 2*DIST - BALL_RAD ); |
||
200 | } else { |
||
201 | ball.X = Math.max(ball.X,PINS[ball.ROW][ball.COL].x - 2*DIST + BALL_RAD + 1); |
||
202 | } |
||
203 | dx = Math.abs(dx); |
||
204 | ball.Y = PINS[ball.ROW][ball.COL].y - dx*(ball.A*dx/DIST+ball.B); |
||
205 | } |
||
206 | |||
207 | ball.t++; |
||
208 | |||
209 | if ( ball.Y > h - BOTTOM_MARGIN - PIN_RAD ){ |
||
210 | if ( ball.previousBall != null && ball.nextBall != null){ |
||
211 | ball.previousBall.nextBall = ball.nextBall; |
||
212 | ball.nextBall.previousBall = ball.previousBall; |
||
213 | } else if ( ball.previousBall != null && ball.nextBall == null ) { |
||
214 | ball.previousBall.nextBall = null; |
||
215 | } else if ( ball.previousBall == null && ball.nextBall != null ) { |
||
216 | ball.nextBall.previousBall = null; |
||
217 | FIRST_BALL = ball.nextBall; |
||
218 | } else { |
||
219 | FIRST_BALL = null; |
||
220 | //plinko.bins.setEnabled( true ); |
||
221 | } |
||
222 | |||
223 | BALL_COUNT--; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | |||
228 | public void drawBackground( int W, int H ){ |
||
229 | Point p; |
||
230 | |||
231 | Graphics2D backgroundgraphics = (Graphics2D) background.getGraphics(); |
||
232 | backgroundgraphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); |
||
233 | backgroundgraphics.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON ); |
||
234 | backgroundgraphics.setColor(Color.white); |
||
235 | backgroundgraphics.fillRect( 0, 0, W, H ); |
||
236 | backgroundgraphics.setColor(Color.black); |
||
237 | Image img = PlinkoBoard.getBall( PIN_RAD, Color.black ); |
||
238 | |||
239 | // draw pins |
||
240 | for (int i=0; i<BINS-1; i++){ |
||
241 | for (int j=0;j<=i;j++){ |
||
242 | p = PINS[i][j]; |
||
243 | //backgroundgraphics.fillOval(p.x-PIN_RAD,p.y-PIN_RAD,2*PIN_RAD,2*PIN_RAD); |
||
244 | backgroundgraphics.drawImage(img, (int)(p.x-PIN_RAD),(int)(p.y-PIN_RAD),this ); |
||
245 | } |
||
246 | } |
||
247 | repaint(); |
||
248 | } |
||
249 | |||
250 | |||
251 | public void mouseClicked( MouseEvent me ){ |
||
252 | active = false; |
||
253 | plinko.showFrame(); |
||
254 | } |
||
255 | |||
256 | |||
257 | public void mouseEntered( MouseEvent me ){ |
||
258 | } |
||
259 | |||
260 | |||
261 | public void mouseExited( MouseEvent me ){ |
||
262 | } |
||
263 | |||
264 | |||
265 | public void mousePressed( MouseEvent me ){ |
||
266 | } |
||
267 | |||
268 | |||
269 | public void mouseReleased(MouseEvent me){ |
||
270 | } |
||
271 | |||
272 | |||
273 | } |