26 May 2016

Breaking bricks when ball hits them.



                                      








import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
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 int ballDimension = 10;
    private int moveX = 1;
    private int moveY = 1;
    private ArrayList<Rectangle> brickList = new ArrayList<Rectangle>();
    BrickInteraction() {       
        constructBricks();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        BrickInteraction game = new BrickInteraction();
        JButton restart = new JButton("start");
        frame.setSize(350, 450);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(game);
        frame.add(restart, BorderLayout.SOUTH);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.getSize();
        Timer timer = new Timer(20, game);
        timer.setInitialDelay(500);
        timer.start();
       

    }

    public void paintBackGround(Graphics2D g){
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, 350, 450);
        g.setColor(Color.GRAY);
        g.fillRect(0, 251, 450, 200);
        g.setColor(Color.red);
        g.drawRect(0, 0, 343, 250);
       
    }
   
   
    Rectangle ball = new Rectangle(0,0,ballDimension,ballDimension);
    public void paintBall(Graphics2D g){       
        g.setColor(Color.BLUE);
        g.fillOval(ball.x, ball.y, ballDimension, ballDimension);
    }
   
    public void paintBricks(Graphics2D g){
        for (Rectangle brick : brickList) {
            g.fill3DRect(brick.x, brick.y, brick.width, brick.height, true);
        }
    }
   

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        paintBackGround(g2d);
        paintBricks(g2d);
        paintBall(g2d);
       
    }

    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_TWO) {
            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 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();
            }
        }
       
        /*while (iterator.hasNext()) {
            Rectangle brick = (Rectangle) iterator.next();
            if(brick.intersects(ball)){
                iterator.remove();
            }
        }*/
       
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        moveBall();
        whenHitBrick();
        repaint();
    }

}

Contact Form

Name

Email *

Message *

Smooth Graphics Java

// Arrange Bricks in Game //java brick breaker game code without flickering // added game levels 1,2,3 in the game //java paddle ball ga...