Wednesday, 3 July 2013

Java Ball Game Program

Java Ball Game Program

Java Code:

import java.awt.Color;
import java.awt.Graphics;
/**
 *this is a ball class code
 *it sets the ball size location and color
 *it contains all the methods that work for ball
 * @author Khan
 */

public class Ball {

    public int size; // ball size
    public int xpos, ypos; // position of ball moves
    public int xs,ys; //minimal box position
   
    /**
     * this is the contructor of the class
     * @param diameter sets the diameter of the ball
     * @param npixel sets the initial position of the ball
     */
   
    public Ball(int diameter, int npixel){ // traveling and initial ball position
        size = diameter;
        xpos = npixel;
        ypos = npixel;
        xs =5;
        ys = 5;   
    }
    /**
     * this is a method of setting position of ball
     * @return the top on Y axis position of the ball
     */
    public int getTop(){    // Return Y coordinate of top of the ball
        return ys;
    }
    /**
     * this is a method of setting position of ball
     * @return. the bottom on Y axis position of the ball
     */
    public int getBottom(){ // Return Y coordinate of bottom of the ball
        return ys + size;
    }
    /**
     *
     * this is a method of setting position of ball
     * @return the left on X axis position of the ball
     */
   
    public int getLeft(){ // Return x coordinate of left of the ball
        return xs;
    }
    /**
     * this is a method of setting position of ball
     * @return the right on X axis position of the ball
     */
   
    public int getRight(){ // Return X coordinate of right of the ball
        return xs + size;
    }
    /**
     * this method draw the ball
     * @param b is the object of ball
     */
    public void drawBall(Graphics b){ // method for draw a ball
        b.setColor(Color.white);
        b.fillOval(xs, ys, size, size);
    }
    /**
     * method to reverse the direction on x
     */
    public void reverseXDirection(){ // method to reverse the direction on x
        xpos = -xpos;
    }
    /**
     * method to reverse the direction on y
     */
    public void reverseYDirection(){ // method to reverse the direction on y
        ypos = -ypos;
    }
    /**
     *  method
     * @return horizontal center
     */
    public int horizontalCenter(){ // method to return horizontal center
        return xs + size/2;
    }
    /**
     *  method
     * @return vertical center
     */
    public int verticalCenter(){ // method to return vertical center
        return ys + size/2;
    }
    /**
     *
     * @param width when ball hits the x axis walls it chnges its direction to reverse
     * @param height  when ball hits the x axis walls it chnges its direction to reverse
     */
    public void Pos(int width, int height){    // method to increment in x & y position and when hits the wall change the direction
        xs += xpos;
        ys += ypos;
        if ((ys + size >= height) || (ys <=0 ))
            ypos = -ypos;
        if ((xs + size >= width ) || (xs <=0))
            xpos = -xpos;
        }
}
---------------------------------------------
import java.awt.*;
import java.awt.event.*;

import javax.swing.Timer;
/**
 * this is controller class which control ball paddle classes methods
 * display objects
 * and control mouse  i.e controller of the game
 * it implements actionsListener and mouseMotionListener
 * @author Khan
 */

public class Controller implements ActionListener, MouseMotionListener{

    private final static int DIAMETER = 20;    // diameter of ball
    private final static int INCREMENT = 5;    // increment in ball
    private final static int LENGTH = 45;    // length of paddle
   

    private Ball ball; // class ball object
    private Paddle paddle; // class paddle object
    private Display display; // class display object
    private Timer t; // timer
    /**
     *this is the constructor of the class
     *that initialize the object of other class
     *setting size of paddle and ball
     */
    public Controller(){ //constructor to register the contents of object of classes
        display = new Display(this);   
        ball = new Ball(DIAMETER, INCREMENT);
        paddle = new Paddle(LENGTH, display.getWidth()/2, display.getHeight()/8);
        t = new Timer(25, this);
        t.setInitialDelay(1000);
        t.start();   
        display.addMouseMotionListener(this);
    }
    /**
     *  display ball and paddle
     * @param d object that set for ball and padlle
     */
    public void displayObject(Graphics d){ // method to checking ball and paddle, they are exist or not
        if(ball != null)
            ball.drawBall(d);
        if(paddle != null)
            paddle.drawPaddle(d);
    }
    /**
     * method to menage paddle position
     */
   
    public void mouseDragged(MouseEvent m){ // method to manage paddle position
        int x, y;
        x = m.getX();
        y = m.getY();
        paddle.PPos(x, y);
        display.repaint();
    }
    /**
     * method to manage paddle position
     */
    public void mouseMoved(MouseEvent m){ // method to manage paddle position
        int x, y;
        x = m.getX();
        y = m.getY();
        paddle.PPos(x, y);
        display.repaint();
    }
    /**
     * method to check ball in contact and calling updates methods of classes
     * display the objects
     */
    public void actionPerformed(ActionEvent a){ // method to check ball in contact and calling updates methods of classes
        int pbottom;
        int bcenter;
        int btop;
        ball.Pos(display.getWidth(), display.getHeight());
        btop = ball.getTop();
        pbottom = paddle.getBottom();
        if(btop <= pbottom && btop >= pbottom - INCREMENT){
            bcenter = ball.horizontalCenter();
            if(bcenter >= paddle.getLeft() && bcenter <= paddle.getRight()){
                ball.reverseYDirection();
            }
        }
        display.repaint();
    }
}
--------------------------------------------------
import javax.swing.*;
import java.awt.*;
/**
 * this methods displays all the class in the frame
 * @author Khan
 */
public class Display extends JPanel {
    public Controller controller;
    public JFrame frame;
    /**
     * this method display the background and color of the back frame
     */
    public void paintComponent(Graphics d){     // method draw back display of the game
        d.setColor(Color.black);
        d.fillRect(0, 0, getWidth(), getHeight());
        controller.displayObject(d);
    }
    /**
     * this is the constructor of the class
     * setting frame size, and exit function of farme
     * @param c setting all content getting from class controller 
     */
    public Display(Controller c){ // constructor display frame and other its objects
        controller = c;
        frame = new JFrame();
        frame.add(this);
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    }
}
-----------------------------------------
import java.awt.Color;
import java.awt.Graphics;
/**
 * this paddle class sets the location size and color of paddle
 * @author Khan
 */

public class Paddle {

    private int length; // length of paddle
    private int pxpos, pypos; // x and y position of paddle
    /**
     *this is a contructor of the class paddle
     * @param l manage length of paddle
     * @param px set x postion of paddl e
     * @param py
     */
    public Paddle(int l, int px , int py){
        length=l;
        pxpos=px;
        pypos=py;
    }
    /**
     * width set the width of paddle
     * and methods draw the paddle
     */
    private static final int WIDTH = 7; // width of paddle
    public void drawPaddle(Graphics p){ //method to draw paddle
        p.setColor(Color.darkGray);
        p.fillRect(pxpos - length/2, pypos, length, WIDTH);
    }
    /**
     * method of the paddle
     * @return Y top surface of the paddle
     */
    public int getTop(){ // return Y coordinate of top of the paddle
        return pypos;
    }
    /**
     * @return Y bottom surface of the paddle
     */
    public int getBottom(){ // return Y coordinate of bottom of the paddle
        return pypos + WIDTH;
    }
    /**
     * @return X left surface of the paddle
     */
    public int getLeft(){ // return X coordinate of left of the paddle
        return pxpos - length / 2;
    }
    /**
     * @return X right surface of the paddle
     */
    public int getRight(){ // return X coordinate of right of the paddle
        return pxpos + length / 2;
    }
    /** 
     * @param x  changes in x position of the paddle
     * @param y y is same
     */
    public void PPos(int x, int y){ // change x
        pxpos = x;
    }
}
------------------------------------------------
/**
 * this class menage to run all the class
 * @author Khan
 */
public class Play {
    public static void main(String[] args)
    {
        Controller start = new Controller();
    }
}
----------------------------

Snapshots:


No comments:

Post a Comment