|
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
|
|
|
|
In my previous part of the article, we
saw a part of our game. In this article, we will delve deeper into
writing the other entities like the ball and bat and how they will
interact and what design issues are involved in designing these
entities.
Some design strategies and how to make usable classes
The first entity when visualized with the other entities gives us
a small picture.

The wall is actually our frame. We know each of these entities has
certain characteristics. lets code the bat entity and the ball
entity.
/* * A
simple brick game in Java. * Program
Name: Basic entity Bat defined here. *
File Name: BatEntity * Author: Vijay
Kukreja * Version :
1.0 */ package JavaGames;
//Lets
keep all our files under one package //there
is nothing to import for creating the bat entity //our
bat entity class public class BatEntity { //Some
attributes of
Bat private int top=0; private int left=0; private int height=0; private int width=0; //some
get/set methods for
brick public int getTop() { return top; } public int getLeft() { return left; } public int getHeight() { return height; } public int getWidth() { return width; } //setter
methods public void setTop(int t) { top=t; } public void setLeft(int l) { left=l; } public void setHeight(int h) { height=h; } public void setWidth(int w) { width=w; } } //end
of BatEntity
The BallEntity Class.
/* * A
simple brick game in Java. * Program
Name: Basic entity Ball defined here. *
File Name : BallEntity * Author:
Vijay Kukreja * Version :
1.0 */ package JavaGames; //Lets
keep all our files under one package //there
is nothing to import for creating the ball entity //our
ball entity class public class BallEntity { //Some
attributes of
Ball private int top=0; private int left=0; private int height=0; private int width=0; //some
get/set methods for
brick public int getTop() { return top; } public int getLeft() { return left; } public int getHeight() { return height; } public int getWidth() { return width; } //setter
methods public void setTop(int t) { top=t; } public void setLeft(int l) { left=l; } public void setHeight(int h) { height=h; } public void setWidth(int w) { width=w; } } //end
of BallEntity
The first diagram looks pretty boring, lets make it more
interesting :

So there you are – Colors and Shapes make more interesting
games. The Ball needs to be blue and circular. The brick needs to be
Square or rectangular and yellow. So now you can easily modify each
entity class and add to it the required attribute. This is the
advantage of decoupling the Wall from the ball and bat and bricks. So
we can make changes to each as required without disturbing the
function or look of the other. Now we need to bring these together
for our basic layout of the game.
So lets open our original frame or base class –
BricksGame.java and add the Brick, ball and bat.
/* 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
applicationpublic class BricksGame extends Frame implements Runnable { //Some
variable declarations //Let
us declare those here and use them in our
wall. BrickEntity mBrick; BallEntity mBall; BatEntity mBat; int iFramex=0; int iFramey=0; int iFrameh=500; int iFramew=800; //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(m_iFramex,m,_iFramey,m_iFramew,m_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); } //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. } } }
In the next article I’ll continue with explaining all the
code we added why we added it. Also I will show you how to display
these in live color on the screen i.e. frame we made earlier. Then
comes the fun part of driving these around and making them
intelligent. So you can start enjoying the playing of the game you
have been working hard to make…
|