001    package breakout;
002    
003    import java.awt.*;
004    
005    /** Subclasses BasicBrick to paint confetti on itself that changes every 20 ticks. **/
006    public class ConfettiBrick extends BasicBrick {
007        private int iter, w;
008        private int[] r=new int[20],
009                      gr=new int[20],
010                      b=new int[20],
011                      x=new int[20],
012                      y=new int[20];
013        
014        public ConfettiBrick(int width, int height, int confettiWidth) {
015            super(width, height);
016            this.iter = 0;
017            for(int i=0; i<20; i++) {
018                this.r[i]=0;
019                this.gr[i]=0;
020                this.b[i]=0;
021                this.x[i]=0;
022                this.y[i]=0;
023            }
024            this.w=confettiWidth;
025        }
026        
027        public void paint(Graphics g) {
028            if(this.iter==0) { this.confettify(); }
029            this.iter++;
030            if(this.iter > 20) this.iter=0;
031            
032            g.setClip(0,0,this.size.width, this.size.height);
033            for(int i=0; i<20; i++) {
034                g.setColor(new Color(this.r[i], this.gr[i], this.b[i]));
035                g.fillRect(this.x[i], this.y[i], this.w, this.w);
036            }
037            
038        }
039        
040        private void confettify() {
041            int width = this.size.width;
042            int height = this.size.height;
043            for(int i=0; i<20; i++) {
044                    r[i] = (int)(255*Math.random());
045                    gr[i] = (int)(255*Math.random());
046                    b[i] = (int)(255*Math.random());
047                    x[i] = (int)((width-w)*Math.random());
048                    y[i] = (int)((height-w)*Math.random());
049            }
050        }
051    }