25 February 2014

Arraigning Bricks In Our Game

Arrange-Bricks






import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Bricks extends JPanel implements Runnable {

 // variables declaration for brick...............................
    //starting position of the bricks
 int brickx = 85;
 int bricky = 50;

 //You can specify length and breadth of the bricks here
 int brickBreadth = 50;
 int brickHeight = 40;
 // variables declaration for brick...............................
 // ===============================================================

 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"
  };

 ArrayList<Rectangle> Brick = new ArrayList<Rectangle>();
 Thread t;

 Bricks() {

  setFocusable(true);
  t = new Thread(this);
  t.start();
 }

 public static void main(String[] args) {
  JFrame frame = new JFrame();
  Bricks game = new Bricks();
  JButton restart = new JButton("start");
  // JLabel lbl_startGame = new JLabel("Start Game", JLabel.LEFT);

  frame.setSize(350, 450);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  frame.add(game);

  frame.add(restart, BorderLayout.SOUTH);
  frame.setLocationRelativeTo(null);
  frame.setResizable(false);
  frame.setVisible(true);

 }

 public void paint(Graphics g) {
  g.setColor(Color.LIGHT_GRAY);
  g.fillRect(0, 0, 350, 450);
  g.setColor(Color.blue);

  g.setColor(Color.green);
  g.setColor(Color.GRAY);
  g.fillRect(0, 251, 450, 200);
  g.setColor(Color.red);
  g.drawRect(0, 0, 343, 250);
  for (int i = 0; i < Brick.size(); i++) {
   if (Brick.get(i) != null) {
    g.fill3DRect(Brick.get(i).x, Brick.get(i).y,
      Brick.get(i).width, Brick.get(i).height, true);
   }
  }

 }

 int brickscount;

 public void run() {

  int xaxis = brickx;
  int yaxis = bricky;

  for (int row = 0; row < LEVELS_ONE.length; row++) {
   for (int col = 0; col < LEVELS_ONE[row].length(); col++) {

    if (LEVELS_ONE[row].charAt(col) == 'W') {

     Brick.add(new Rectangle(xaxis, yaxis, brickBreadth, brickHeight));
     xaxis += (brickBreadth+1);

    }
    if (LEVELS_ONE[row].charAt(col) == ' ') {
     xaxis += (brickBreadth+1);

    }

   }
   yaxis += (brickHeight+2);
   xaxis = brickx;
  }

 }// while loop ends here

}

Click here for Breaking Bricks Game Code

Also see Bullet shooting game using Java.

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...