Java Code
/* Breakout - Part 1
* Display the bricks
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class BreakoutPart1 extends JApplet
{
ArrayList blocks;
int width, height; // dimensions of applet
int numBlocks; // number of blocks horizontally
int numRows; // number of rows - 1 least difficult, 5 more difficult
public void init( )
{
resize(600, 400 );
numBlocks = 10;
numRows = 3;
blocks = new ArrayList( );
width = getWidth( );
height = getHeight( );
buildBlocks( );
}
public void buildBlocks( )
{
int sizeOfBlock = width / numBlocks;
int heightOfBlock = 15;
for( int rows=0; rows<width; rows += sizeOfBlock )
for( int cols=0; cols<numRows*heightOfBlock; cols += heightOfBlock )
{
Rectangle r = new Rectangle( rows,80+cols, sizeOfBlock-2, heightOfBlock-2 );
blocks.add(r);
}
}
public void paint( Graphics g )
{
g.clearRect(0,0,width,height); // clear screen
g.setColor( Color.RED );
for (int i=0; i<blocks.size( ); i++ ) // color remaining blocks
{
Rectangle r = (Rectangle )blocks.get(i);
g.fillRect( r.x, r.y, r.width, r.height );
}
g.setColor( Color.BLACK );
}
}