import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.TexturePaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BrickInteraction extends JPanel implements ActionListener {
private static final String[] LEVELS_ONE = new String[]
{ "WWWWW",
"W W",
"W W W",
"W W",
"WWWWW" };
private static final String[] LEVELS_TWO = new String[]
{ "WWWWW",
" ",
"W W W",
" ",
"WWWWW" };
private static final String[] LEVELS_Three = new String[]
{
" ",
"W ",
" ",
"WW " };
private int ballDimension = 10;
private int moveX = 1;
private int moveY = 1;
static int gameSpeed = 5;
private ArrayList<Rectangle> brickList = new ArrayList<Rectangle>();
Rectangle ball = new Rectangle(0,0,ballDimension,ballDimension);
private Graphics2D graphics2D;
TexturePaint texturePaint;
BufferedImage bufferedImage;
Rectangle shades;
Timer timer;
Container container;
BrickInteraction(Container container) {
this.container= container;
bufferedImage = new BufferedImage(5,5,BufferedImage.TYPE_INT_RGB);
shades = new Rectangle(0, 0, 5, 5);
texturePaint = new TexturePaint(bufferedImage, shades);
constructBricks();
timer = new Timer(gameSpeed, this);
timer.start();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
final BrickInteraction game = new BrickInteraction(frame.getContentPane());
frame.setSize(350, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(game);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
JButton button = new JButton("restart");
buttonPanel.add(button);
JButton pauseButton = new JButton("pause");
buttonPanel.add(pauseButton);
frame.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.getSize();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
game.restartGame();
}
});
pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
if(button.getText().equals("pause")){
button.setText("play");
game.pauseGame();
}
else if(button.getText().equals("play")){
button.setText("pause");
game.resumeGame();
}
}
});
}
public void texture(){
Graphics2D big = bufferedImage.createGraphics();
big.setColor(Color.magenta);
big.fillRect(0, 0, 5, 5);
big.setColor(Color.lightGray);
big.fillOval(0, 0, 5, 5);
}
public void paintBackGround(){
graphics2D.setColor(Color.LIGHT_GRAY);
graphics2D.fillRect(0, 0, 350, 400);
texture();
graphics2D.setPaint(texturePaint);
graphics2D.fillRect(0, 251, 400, 200);
graphics2D.setColor(Color.red);
graphics2D.drawRect(0, 0, 343, 250);
}
public void paintBall(){
graphics2D.setColor(Color.BLUE);
graphics2D.fillOval(ball.x, ball.y, ballDimension, ballDimension);
}
public void paintBricks(){
for (Rectangle brick : brickList) {
graphics2D.fill3DRect(brick.x, brick.y, brick.width, brick.height, true);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
graphics2D = (Graphics2D) g;
graphics2D.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
paintBackGround();
paintBricks();
paintBall();
}
public void constructBricks() {
// starting position of the bricks
int initialBrickXAxis = 85;
int initialBrickYAxis = 50;
// You can specify length and breadth of the bricks here
int brickBreadth = 30;
int brickHeight = 10;
int variableXAxisPosition = initialBrickXAxis;
int variableYAxisPosition = initialBrickYAxis;
for (String colBricks : LEVELS_ONE) {
char[] rowBrick = colBricks.toCharArray();
for (char eachBrick : rowBrick) {
if (eachBrick == 'W') {
brickList.add(new Rectangle(variableXAxisPosition, variableYAxisPosition, brickBreadth,
brickHeight));
variableXAxisPosition += (brickBreadth + 1);
} else if (eachBrick == ' ') {
variableXAxisPosition += (brickBreadth + 1);
}
}
variableYAxisPosition += (brickHeight + 2);
variableXAxisPosition = initialBrickXAxis;
}
}
public void paintGameOver(){
JOptionPane.showMessageDialog(container,
"Game Over");
}
public void moveBall(){
ball.x = ball.x + moveX;
ball.y = ball.y + moveY;
if(ball.x > (343 - ballDimension) ){
moveX = -1;
}
if(ball.y > (250 - ballDimension)){
moveY = -1;
}
if(ball.y < 0){
moveY = +1;
}
if(ball.x < 0 ){
moveX = +1;
}
}
public void whenHitBrick(){
for (Iterator<Rectangle> iterator = brickList.iterator(); iterator.hasNext();) {
Rectangle brick = (Rectangle) iterator.next();
if(brick.intersects(ball)){
iterator.remove();
}
}
if(brickList.size() <= 0){
paintGameOver();
timer.stop();
}
/*while (iterator.hasNext()) {
Rectangle brick = (Rectangle) iterator.next();
if(brick.intersects(ball)){
iterator.remove();
}
}*/
}
public void pauseGame(){
timer.stop();
}
public void resumeGame(){
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
moveBall();
whenHitBrick();
repaint();
}
public void restartGame(){
ball.x = 0;
ball.y = 0;
constructBricks();
//timer.setInitialDelay(500);
timer.start();
}
}
//Please feel free to comment