Sunday, April 17, 2011

2D game in Java API - Basic format

Using Java to create a 2D game environment is quite complicated.
Yet, the following basic format outlines how to create a window and display game characters.




List of classes required

public class Runner // Where the program starts.
public class MainFrame extends JFrame // Where time and user inputs are managed.
public class MainPanel extends JPanel // Where game characters are displayed.
public abstract class Actor // A game character and its behaviors.
(And other concrete actor classes that extend Actor class)


Handler classes are inside the MainFrame class.
MainFrame class listens to events, and updates the MainPanel object.
MainPanel class updates its actors every time it is updated.

For example, TimeHandler in MainFrame tells MainPanel to update every 5 milliseconds,
and MainPanel tells its actors to update every 5 milliseconds.



Source Code (Based on Java 5.0)


Following lines are basic skeletons for the classes listed above:



public class Runner
{
public static void main(String[] args)
{
MainFrame mainFrame = new MainFrame();
}
}


public class MainFrame extends JFrame
{
private MainPanel mainPanel;
private TimeHandler timeHandler;
//Calls mainFrame's update() method every 5 milliseconds.
private Timer timer;
//Timer that calls timeHandler every 5 milliseconds.
public MainFrame()
{
mainPanel = new MainPanel();
add(mainPanel);
timeHandler = new TimeHandler();
timer = new Timer(5,null);
timer.addActionListener(timeHandler);
timer.start(); //Start the game.
setTitle("Title");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1,1));
setVisible(true);
}
//--------------------------------------------Inner classes
public class TimeHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
mainPanel.update();
}
}
/* Add some other Handler classes (ex: KeyHandler, MouseHandler) here.*/
}


public class MainPanel extends JPanel
{
private ArrayList<Actor> actorList; //List of all game characters.
public MainPanel()
{
actorList = new ArrayList<Actor>();
/*Add game characters to this list. */
}
//--------------------------------------------Core methods
public void update()
{
for(int i = 0; i < actorList.size(); i++)
{
actorList.get(i).update();
}
repaint();
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < actorList.size(); i++)
{
g.drawImage(actorList.get(i).getImage(),
(int)actorList.get(i).getX(),
(int)actorList.get(i).getY(), null);
}
}

//--------------------------------------------Accessors/Modifiers
public ArrayList<Actor> getActorList()
{
return actorList;
}
public void addActor(Actor newActor)
{
actorList.add(newActor);
}
public void removeActor(Actor actorToRemove)
{
actorList.remove(actorToRemove);
}
public void toFront(Actor actor)
{
Actor tempActor = actor;
actorList.remove(actor);
actorList.add(tempActor);
}
public void toBack(Actor actor)
{
Actor tempActor = actor;
actorList.remove(actor);
actorList.add(0, tempActor);
}
}


public abstract class Actor
{
protected String name; //Name of the game character.
protected Image image; //Image of the game character.
protected double x; //x-coordinate of the character's position.
protected double y; //y-coordinate of the character's position.
protected double dx; //character's horizontal velocity.
protected double dy; //character's vertical velocity.
public Actor(String n, Image i, double ax, double wy)
{
name = n;
image = i;
x = ax;
y = wy;
dx = 0;
dy = 0;
}
//--------------------------------------------Core methods
public abstract void update();
public void controlMovements() //Include this in update() method.
{
x += dx;
y += dy;
}
//--------------------------------------------Accessors/Modifiers
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
public Image getImage()
{
return image;
}
public void setImage(Image i)
{
image = i;
}
public double getX()
{
return x;
}
public void setX(double ax)
{
x = ax;
}
public double getY()
{
return y;
}
public void setY(double why)
{
y = why;
}
public double getDx()
{
return dx;
}
public void setDx(double deltaX)
{
dx = deltaX;
}
public double getDy()
{
return dy;
}
public void setDy(double deltaY)
{
dy = deltaY;
}
}


(An example of Actor's subclass)

public class Monster extends Actor
{
public Monster(String n, Image i, double ax, double wy)
{
super(n,i,ax,wy);
}
//--------------------------------------------Core methods
public void update()
{
controlMovements();
}
}

No comments:

Post a Comment