Java Code
/* Breakout - Final version Part 4
* remove flicker and update problems
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.image.*;
public class Breakout extends JApplet implements Runnable, KeyListener
{
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
// Part III
Rectangle paddle;
int speed;
// Part II
Thread thread;
Ball ball;
// Part IV
BufferedImage buffer;
public void init( )
{
resize(600, 400 );
numBlocks = 10;
numRows = 3;
blocks = new ArrayList( );
width = getWidth( );
height = getHeight( );
buildBlocks( );
// Part II
paddle = new Rectangle( 50, height-30, 50, 10 );
addKeyListener( this );
speed = 10;
setFocusable(true);
// PART IV
buffer = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
// PART III
ball = new Ball( 50, 120, 15, 5, width, height );
thread = new Thread(this);
thread.start( );
}
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 update( Graphics g )
{
paint(g);
}
public void paint( Graphics g )
{
// g.clearRect(ball.x-20, ball.y-20, ball.size+40, ball.size+40 );
Graphics bg = buffer.getGraphics( );
bg.setColor(Color.WHITE);
bg.fillRect(0,0,width,height);
//ball.paint( getGraphics( ) );
ball.paint( bg );
// g.setColor( Color.WHITE );
// g.fillRect(0,0,width,height);
bg.setColor( Color.RED );
for (int i=0; i<blocks.size( ); i++ )
{
Rectangle r = (Rectangle )blocks.get(i);
bg.fillRect( r.x, r.y, r.width, r.height );
}
bg.setColor( Color.BLACK );
bg.fillRect( paddle.x, paddle.y, paddle.width, paddle.height );
g.drawImage(buffer,0,0,this);
}
public void run( )
{
while( true )
{
// repaint( );
ball.move( );
checkForCollision( );
// ball.paint( buffer.getGraphics( ) );
repaint( );
try {
Thread.sleep(15);
}catch( Exception ex ) { }
}
}
public void checkForCollision( )
{
Rectangle ballR = new Rectangle( ball.x, ball.y, ball.size, ball.size);
for( int i=0; i<blocks.size( ); i++ )
{
Rectangle r = (Rectangle )blocks.get(i);
if ( r.intersects( ballR ) )
{
blocks.remove(r);
getGraphics( ).clearRect(r.x, r.y, r.width, r.height);
ball.dirX = -1 * ball.dirX;
ball.dirY = -1 * ball.dirY;
//repaint( );
return;
}
}
// check for paddle collision
if ( ballR.intersects( paddle ) )
{
ball.dirX = -1 * ball.dirX;
ball.dirY = -1 * ball.dirY;
// repaint( );
}
// repaint( );
}
public void keyTyped( KeyEvent ke ) { }
public void keyReleased( KeyEvent ke ) { }
public void keyPressed( KeyEvent ke )
{
int code = ke.getKeyCode( );
getGraphics( ).clearRect( paddle.x, paddle.y, paddle.width, paddle.height);
if( code == KeyEvent.VK_LEFT )
{
int x = paddle.x;
x -= speed;
paddle.x = x;
}
else if( code == KeyEvent.VK_RIGHT )
{
int x = paddle.x;
x += speed;
paddle.x = x;
}
repaint( );
}
}