JavaReference.com AllDevNet.com"
Welcome Guest     Login     Register    
 [ Home > Articles > Java Game Programming, Part IV ]  Submit Article   
Java Game Programming, Part IV
[ by Vijay Kukreja ]
Print   E-Mail 
About the Author
Vijay is an Information Systems Architect at CVS Pharmacy in Rhode Island. His primary focus is J2EE technology based solution Design and Development. Along with a combination of other enterprise technologies he believes that J2EE will become a primary driving force in the delivery of enterprise level systems. Vijay has participated in numerous consulting projects over the past six years. He has a Masters in Computer Science and a Sun Certified Java Programmer and Architect. His projects involve UML, Java, JSP/Servlets, EJB's, CORBA, XML, JMS, MQSeries & various tools. He has provided onsite J2EE and/or XML development at high-tech companies located in and around Massachusetts, CT and RI. He has also published articles on Java Programming, C++ and other technologies, in various online magazines and with other consultants and is a frequent writer on Java and related technologies. He can be reached at vijay (at) javareference (dot) com
More articles by Vijay Kukreja 
Advertisement

Colors and shapes

In the previous article, we talked about the design for the game and then went ahead and wrote some code. In this part we will put together the graphical view of the whole game. Graphics can get complex and is left to your imagination, we will just touch on the basics and concentrate on the logic.

To make our program capable of displaying graphical objects we need to understand the function paint(). This method is the most important method for all graphics based programs.

public void paint(Graphics g)
{
    g.drawString("hello world",100,100);
}

The paint method is used to do exactly that. Paint. It accepts the Graphics object, which is supplied by the JVM. The paint method can call functions on this graphics object to render different things on the object. Typically, a paint method is a part of all components. Each components' implementation of the paint method decides how that component looks. Adding the code presented above to a Frame will produce the following result.

Paint method demonstration

Armed with the usage of the paint method lets try using it to paint our ball bat and bricks.

The complete code to draw a single brick and color it and a ball and a bat and color each of them is as below.

/*
* A simple brick game in Java.
* Program Name: Bricks a simple a Java Game.
* File Name : BricksGame
* Author: Vijay Kukreja
* Version : 1.0
*/

package JavaGames;

//Lets keep all our files under one package
//lets import the necessary classes to which will create the GUI


import java.awt.*;
import java.awt.event.*;
import java.awt.Color;

//our main class for the application
public class  BricksGame extends Frame 
{

    BricksEntity mBrick;
    BallEntity mBall;
    BatEntity mBat;
    int iFramex=0;
    int iFramey=0;
    int iFrameh=500;
    int iFramew=800;

    public void paint(Graphics g)
    {
        g.drawRect(mBrick.getLeft(),mBrick.getTop(),mBrick.getWidth(),mBrick.getHeight());
        g.drawRect(mBat.getLeft(),mBat.getTop(),mBat.getWidth(),mBat.getHeight());
        g.drawOval(mBall.getLeft(),mBall.getTop(),mBall.getHeight(),mBall.getWidth());
    }

    public void initBrick()
    {
        mBrick.setTop(50);
        mBrick.setLeft(10);
        mBrick.setHeight(20);
        mBrick.setWidth(50);
    }
    
    public void initBall()
    {
        mBall.setTop(150);
        mBall.setLeft(100);
        mBall.setHeight(40);
        mBall.setWidth(40);
    }
    
    public void initBat()
    {
        mBat.setTop(400);
        mBat.setLeft(100);
        mBat.setHeight(20);
        mBat.setWidth(100);
    }

    //the main method the entry point into our game program.
    public static void main(String[] args) 
    {
        System.out.println("Welcome to the Bricks Game!");
        BricksGame m_bricksGame=new BricksGame();
    }

    //the constructor where we'll initialize our environment and create the frame
    public BricksGame()
    {
        this.setBounds(iFramex,iFramey,iFramew,iFrameh);
        this.setTitle("Welcome to Bricks Game");
        //Let us create live objects out of our entity classes.
        mBrick=new BricksEntity();
        mBall=new BallEntity();
        mBat=new BatEntity();
        this.setVisible(true);
        //initialize the sizes of the bricks and ball and bat
        initBrick();
        initBall();
        initBat();
    }

    //In case someone wants to close the program from the Frame we need to
    //process the window closing event and handle it appropriately

    protected void processWindowEvent(WindowEvent e)
    {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING)
        {
            System.exit(0);
            //close the window and exit out of the program.
        }
    }
}



So as you can see from the code above, there have been additions to the constructor method BricksGame(). I have added the initialization calls for brick, bat and ball.

The three methods initBrick() and initBat() and initBall() are basically for giving the position and height and width of those objects.

The output of the above code looks like this :

Bricks Game First Cut

The above source is also available in .zip format. Click here to download it.

In the coming articles lets continue with colors and how to start the animating these objects. We will be getting into the threading concepts involved in getting the ball rolling!!...


Print   E-Mail 
  Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. and its subsidiaries in the U.S. and other countries.