Back to index

wims  3.65+svn20090927
SplashPanel.java
Go to the documentation of this file.
00001 import java.awt.*;
00002 import java.awt.geom.*;
00003 import java.awt.event.*;
00004 import java.awt.image.*;
00005 import javax.swing.*;
00006 
00007 public class SplashPanel extends JPanel implements MouseListener,Runnable{
00008        Plinko plinko;
00009        double w;
00010        double h;     
00011        int TOTAL;                                // total number of balls that have been dropped
00012        int[] HIST;                               // number of balls in each bin;
00013        double MAX;                               // maximum number of balls in a single bin
00014        Point[][] PINS;                           // coordinates of pins (including one at the base of each bin)
00015        double DIST;                       // vertical distance between pins
00016        double BALL_RAD;                   // radius of ball
00017        int PIN_RAD;                       // radius of pin
00018        int BINS = 20;                            // total number of bins
00019        PlinkoBall FIRST_BALL;          // represents beginning of doubly linked list of plinko balls
00020        int BALL_COUNT;
00021        int BOTTOM_MARGIN = 5;
00022 //     private static byte[] data;
00023        private final static int hx = 15;
00024        private final static int hy = 15;
00025        private final static int bgGrey = 192;
00026        Image[] IMAGES;
00027        boolean newBackground = true;
00028        boolean active = true;
00029        Thread thread;
00030        public SplashPanel( Plinko plinko ){
00031               setBackground( Color.white );
00032               addMouseListener( this );   
00033               this.plinko = plinko;
00034               for ( int i = 0; i<25; i++ ){
00035                      dropBall();
00036                      FIRST_BALL.ROW = 10+(int)(10*Math.random());
00037                      FIRST_BALL.COL = (int)(FIRST_BALL.ROW*Math.random());
00038                      FIRST_BALL.C = 10;
00039                      FIRST_BALL.t = i%11;
00040               }
00041               start();
00042        }
00043 
00044        public void start(){
00045               active = true;
00046               thread = new Thread(this);
00047               thread.start();
00048        }
00049 
00050 
00051        public void run(){
00052            while ( active ){
00053               if ( Math.random() < 0.4 ){
00054                   dropBall();
00055                   FIRST_BALL.ROW = 11;
00056                   FIRST_BALL.COL = (int)(FIRST_BALL.ROW*Math.random());
00057                   FIRST_BALL.C = 10;
00058               }
00059               repaint();
00060               try {Thread.sleep(100);} catch (InterruptedException e){}
00061            }
00062        }
00063 
00064 
00065        public void dropBall(){
00066            if ( FIRST_BALL == null ){
00067                      FIRST_BALL = new PlinkoBall();
00068            } else {
00069                      FIRST_BALL.previousBall = new PlinkoBall();
00070                      FIRST_BALL.previousBall.nextBall = FIRST_BALL;
00071                      FIRST_BALL = FIRST_BALL.previousBall;
00072            }
00073     }
00074 
00075 
00076     //Image backImage;
00077        Image background;
00078        Graphics2D backGraphics;
00079        int FUDGE = 200;
00080 
00081     public void setup(){
00082               if(plinko.wims_exercise == true){ FUDGE = 100;}
00083               if ( h-BOTTOM_MARGIN<h/2 ){
00084                      DIST = (double)(h-BOTTOM_MARGIN)/BINS;
00085               } else {
00086                      DIST = (double)(w-10)/(2*BINS);
00087               }
00088 
00089               PIN_RAD = (int)DIST/9 + 1;
00090               BALL_RAD = Math.max(2*DIST/7,2.0) + 1;
00091 
00092               // create images of colored balls
00093               IMAGES = new Image[ PlinkoBoard.COLORS.length ];
00094               Graphics2D g;
00095               int red;
00096               int green;
00097               int blue;
00098               for ( int i=0; i<PlinkoBoard.COLORS.length; i++ ){
00099                      IMAGES[i] = PlinkoBoard.getBall( BALL_RAD, PlinkoBoard.COLORS[i] );
00100               }
00101 
00102               PINS = new Point[BINS][];
00103               for (int i=0; i<BINS; i++){
00104                      PINS[i] = new Point[i+1];
00105                      for (int j=0; j<=i; j++){
00106                             PINS[i][j]= new Point((int)(DIST*(2*j-i)+w/2)-FUDGE/2,(int)(DIST*(i+1))-(FUDGE-15));
00107                      }
00108               }             
00109               
00110               // pins at the base of each bin
00111               for (int i=0; i<BINS; i++){
00112                      PINS[BINS-1][i]= new Point((int)(DIST*(2*i-BINS+1)+w/2),(int)(h-30-BALL_RAD));
00113               }
00114     }
00115 
00116 
00117        public void paintComponent( Graphics graphics ){
00118               // have a copy of the background on which to draw
00119               w = getWidth();
00120               h = getHeight();
00121               
00122               w = Math.max( getWidth(),getHeight() ) + FUDGE;
00123               h = w;
00124 
00125               Graphics2D g = (Graphics2D)graphics;
00126               g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
00127 
00128               if ( background == null || background.getWidth(this) != h || background.getHeight(this) != w ){
00129                      setup();
00130                      background = createImage( (int)w, (int)h );
00131                      drawBackground((int)w,(int)h);
00132               }
00133 
00134               g.drawImage( background, 0, 0, this );
00135 
00136               // run through all active balls and draw them on the image
00137               PlinkoBall ball = FIRST_BALL;
00138 
00139               int red;
00140               int green;
00141               int blue;
00142               while ( ball != null ){
00143                      graphics.drawImage( IMAGES[ball.spaz], (int)(ball.X - BALL_RAD),  (int)(ball.Y - 2*BALL_RAD - PIN_RAD + 1), this );
00144                      if ( active ) increment( ball );
00145                      ball = ball.nextBall;
00146               }
00147               //g.drawImage(image,0,0,this);
00148 
00149               w = getWidth();
00150               h = getHeight();
00151               g.setColor( new Color( 128,128,128,220 ) );
00152               g.setFont( new Font("Helvetica",Font.BOLD,plinko.font_size) );
00153               //String str = "CLICK ANYWHERE TO BEGIN";
00154               String str = plinko.click_text;
00155               
00156               g.drawString( str, (int)(w-g.getFontMetrics().stringWidth(str)-5), (int)(h-5) );
00157        }
00158 
00159 
00160     public void increment( PlinkoBall ball ){
00161               // if ball has landed on pin, reset t to 0 and pick a direction
00162               if ( ball.t == ball.C && ball.ROW < BINS-2 ){
00163                      ball.ROW++;
00164                      ball.COL += ball.DIR;
00165                      ball.t = 0;
00166                      ball.DIR = 0;
00167                      if ( Math.random() < 0.5 ) ball.DIR = 1;
00168 
00169                      ball.C = 10;
00170                      
00171                      //if ( C>1 ) C += spaz;
00172               }
00173 
00174               double dx = DIST*ball.t*(2*ball.DIR - 1)/ball.C;
00175 
00176               if ( ball.ROW < 0 ){ // ball falling onto top pin
00177                      ball.X = PINS[0][0].x;
00178                      dx = Math.abs(dx);
00179                      ball.Y = PINS[0][0].y - DIST + DIST*ball.t*ball.t/(ball.C*ball.C);
00180               } else if ( ball.ROW < BINS-2 ) {
00181                      ball.X = PINS[ball.ROW][ball.COL].x + dx;
00182                      dx = Math.abs(dx);
00183                      //Y = PlinkoBoard.PINS[ROW][COL].y - (int)(dx*(A*dx/PlinkoBoard.DIST+B));
00184                      ball.Y = PINS[ball.ROW][ball.COL].y - DIST*PlinkoBoard.DYS[ball.C][ball.t];
00185               } else { // ball falling into bin
00186                      ball.X = PINS[ball.ROW][ball.COL].x + dx;
00187                      if ( dx>0 ){ 
00188                             ball.X = Math.min(ball.X,PINS[ball.ROW][ball.COL].x + 2*DIST - BALL_RAD );
00189                      } else {
00190                             ball.X = Math.max(ball.X,PINS[ball.ROW][ball.COL].x - 2*DIST + BALL_RAD + 1);
00191                      }
00192                      dx = Math.abs(dx);
00193                      ball.Y = PINS[ball.ROW][ball.COL].y  - dx*(ball.A*dx/DIST+ball.B);
00194               }
00195 
00196               ball.t++;
00197 
00198               if ( ball.Y > h - BOTTOM_MARGIN - PIN_RAD ){
00199                      if ( ball.previousBall != null && ball.nextBall != null){
00200                             ball.previousBall.nextBall = ball.nextBall;
00201                             ball.nextBall.previousBall = ball.previousBall;
00202                      } else if ( ball.previousBall != null && ball.nextBall == null ) {
00203                             ball.previousBall.nextBall = null;
00204                      } else if ( ball.previousBall == null && ball.nextBall != null ) {
00205                             ball.nextBall.previousBall = null;
00206                             FIRST_BALL = ball.nextBall;
00207                      } else  {
00208                             FIRST_BALL = null;
00209                             //plinko.bins.setEnabled( true );
00210                      }
00211 
00212                      BALL_COUNT--;
00213               }
00214     }
00215        
00216        
00217     public void drawBackground( int W, int H ){
00218               Point p;
00219 
00220               Graphics2D backgroundgraphics = (Graphics2D) background.getGraphics();
00221               backgroundgraphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
00222               backgroundgraphics.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
00223               backgroundgraphics.setColor(Color.white);
00224               backgroundgraphics.fillRect( 0, 0, W, H );
00225               backgroundgraphics.setColor(Color.black);
00226               Image img = PlinkoBoard.getBall( PIN_RAD, Color.black );
00227 
00228               // draw pins
00229               for (int i=0; i<BINS-1; i++){
00230                      for (int j=0;j<=i;j++){
00231                             p = PINS[i][j];
00232                             //backgroundgraphics.fillOval(p.x-PIN_RAD,p.y-PIN_RAD,2*PIN_RAD,2*PIN_RAD);
00233                      backgroundgraphics.drawImage(img, (int)(p.x-PIN_RAD),(int)(p.y-PIN_RAD),this );
00234                      }
00235               }
00236               repaint();
00237     }
00238 
00239 
00240        public void mouseClicked( MouseEvent me ){
00241               active = false;
00242               plinko.showFrame();
00243        }
00244        
00245        
00246        public void mouseEntered( MouseEvent me ){
00247        }
00248 
00249 
00250        public void mouseExited( MouseEvent me ){
00251        }
00252 
00253 
00254        public void mousePressed( MouseEvent me ){
00255        }
00256 
00257 
00258        public void mouseReleased(MouseEvent me){
00259        }
00260 
00261        
00262 }