28 May 2016

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 game code
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.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
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,MouseMotionListener {

    private static final String[] LEVELS_ONE = new String[]
    {
        "     ",
        "WW   ",
        "     ",
        "W    " };
    private static final String[] LEVELS_TWO = new String[]
    {
        "     ",
        "W   W",
        "     ",
        "W    " };
    private static final String[] LEVELS_THREE = new String[]
            {
              "     ",
              "W    ",
              "     ",
              "W    " };
    private static String[] currentLevel;
    private int ballDimension = 10;
    private int moveX = 1;
    private int moveY = 1;
    static int gameSpeed = 15;
    private ArrayList<Rectangle> brickList = new ArrayList<Rectangle>();
    Rectangle ball = new Rectangle(0,0,ballDimension,ballDimension);
    Rectangle paddle = new Rectangle(290, 240, 40, 10);
    private Graphics2D graphics2D;
 
    TexturePaint texturePaint;
    BufferedImage bufferedImage;
    Rectangle shades;
    Timer timer;
    Container container;
    private String[][] levels = {LEVELS_ONE,LEVELS_TWO,LEVELS_THREE};
    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();
        addMouseMotionListener(this);
      
    }

    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("start");
     
        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) {

                JButton button = (JButton)e.getSource();
                if(button.getText().equals("start")){
                    button.setText("restart");
                    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.BLACK);
        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 paintPaddle(){
        graphics2D.setColor(Color.green);
        graphics2D.fill3DRect(paddle.x, paddle.y, paddle.width, paddle.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();
        paintPaddle();
     
    }

    public void paintGameOver() {
        timer.stop();
        JOptionPane.showMessageDialog(container, "Game Over");
        currentLevel = LEVELS_TWO;
        restartGame();
    }
    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 : currentLevel) {
            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(){
        Iterator<Rectangle> iterator = brickList.iterator();
        int count = 0;
        while (iterator.hasNext()) {
            count++;
            Rectangle brick = (Rectangle) iterator.next();
            Line2D verticalSideRight = new Line2D.Double(ball.x+ballDimension, ball.y+2, ball.x+ballDimension, ball.y+ballDimension -2);
         
            Line2D verticalSideLeft = new Line2D.Double(ball.x, ball.y+2, ball.x, ball.y+ballDimension-2);
            Line2D horizonBottom = new Line2D.Double(ball.x+2, ball.y+ballDimension, ball.x+ballDimension-2, ball.y+ballDimension);
            Line2D horizonTop = new Line2D.Double(ball.x+2, ball.y, ball.x+ballDimension-2, ball.y);
            if(brick.intersectsLine(verticalSideRight)){
               //System.out.println("Rig"+count);
            moveX = -1;
            iterator.remove();
            }else if(brick.intersectsLine(verticalSideLeft)){
                //System.out.println("lef"+count);
            moveX = +1;
            iterator.remove();
            break;
            }
         
            if(brick.intersectsLine(horizonBottom)){
                //System.out.println("bot"+count);
                moveY = -1;
                iterator.remove();
            }else if(brick.intersectsLine(horizonTop)){
                //System.out.println("top"+count);
                moveY = +1;
                if(!brick.equals(null))
                iterator.remove();
            }
        }
        if(brickList.size() <= 0){
            paintGameOver();
        }
     
    }
   boolean gameOver = false;
    public void ballHitPaddle(){
        if(ball.intersects(paddle)){
            moveY = -1;
        }
    }
public void pauseGame(){
    timer.stop();
}
public void resumeGame(){
    timer.start();
}
    @Override
    public void actionPerformed(ActionEvent e) {
        moveBall();
        whenHitBrick();
        ballHitPaddle();
        repaint();
    }
    int levelCount = 0;
    public void restartGame(){
        ball.x = 0;
        ball.y = 0;
        if(levelCount < levels.length){
        currentLevel =    levels[levelCount];
        levelCount++;
        gameSpeed -= 3;
      
        constructBricks();
        timer = new Timer(gameSpeed, this);
        timer.start();
        }
      
      
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        mouseMoved(e);
     
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        paddle.x = (int) e.getPoint().getX();
        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...