5
\$\begingroup\$

I'm beginner in game development, in Java and here on this site too and I have a game design question. Please comment my idea:

I have a main loop which call update and draw method. I want to use an ArrayList which stores graphical objects, they have coordinate and image or text to draw and my game objects extends this class. In update, I can choose which objects should be put in the array and in draw method I'll display the elements of array on the screen. I'm using a buffer and draw first there. Here is a simple (not full) code, only the logic:

public class GamePanel extends JPanel implements KeyListener
{
   ArrayList<graphicalObjects> graphArray = new ArrayList<graphicalObjects>();

   public void update()
   {
      //change the game scene, update the graphArray, process input etc.
   }

   public void draw()
   {
      //draws every element of graphArray to a JPanel
   }

   public static main(String[] args)
   {
      while(true)
      {
          update();
          draw();
      }
   }
}

My questions:

  1. Should have I use interface or abstract class for graphicalObjects? graphicalObjects class and the ArrayList really needs or there is some better solution?

  2. How to draw objects? They draw themself with their own method or in the draw method I have to draw manually based on graphicalObjects variables (x,y coordinates, image etc.)?

  3. If this conception is wrong, please suggest another one!

\$\endgroup\$
0

2 Answers 2

7
\$\begingroup\$
  1. Should have I use interface or abstract class for graphicalObjects? graphicalObjects class and the ArrayList really needs or there is some better solution?

I would go with an interface. If all graphical objects have lots of similar data, you may reconsider.

2.  How to draw objects? They draw themself with their own method or in the draw method I have to draw manually based on graphicalObjects variables (x,y coordinates, image etc.)?

I suggest you follow Javas pattern and pass on a Graphics object and let the object paint themselves. Just set clip and offset properly before you pass on the graphics object.

3. If this conception is wrong, please suggest another one!

I think it's okay, but here is a small remark:

Most Java UI's are event-driven. Since you seem to plan to implement a KeyListener, I would suggest you follow this convention. That is, throw out the update/draw loop, and repaint the panel after processing received events.

\$\endgroup\$
3
  • \$\begingroup\$ Agreed about the interface, especially if you follow the second rec and pass in the graphics object needed to draw themselves. \$\endgroup\$
    – ChrisE
    Feb 9, 2011 at 17:36
  • 2
    \$\begingroup\$ I agree with all the answers but offer caution on the last answer. If the game is itself event driven then this will work. If however, it is not, then the use of some kind of update loop will be needed.. To either render the new position an object has taken while you are watching it, or even to simply redraw a timer on the screen while you are trying to guess the answer to a trivia question. That's all, otherwise spot on answer! :) \$\endgroup\$
    – James
    Feb 9, 2011 at 23:20
  • 1
    \$\begingroup\$ I agree with @James, and I think that most games will require a update/render loop. \$\endgroup\$ Feb 10, 2011 at 3:33
0
\$\begingroup\$

What needs an object to be drawn

Let's start with a basic class for an object which should do the following.

  • The coordinates Simple variables such as x_pos and y_pos
  • An Image The face of the object
  • a render() method To draw the object

The coordinates

Every object requires the coordinates to note it's position on the screen. They can be complex Vectors and also simple vars. Let's name them x_pos and y_pos. They need to set in the constructor for games (To have multiple instances).

The image

An image let's the user to identify the game objects. Loading images is extremely easy than sounds. Here's a loadImage() method.

public static Image loadImage(String name){
    Image img = null;
    if (cache.containsKey(name)) {
        img = cache.get(name);
    } else {
        try {
            img = new ImageIcon(this.getClass().getClassLoader().getResource(name)).getImage();
            cache.put(name, img);
        } catch (Exception e) {
            System.err.println("Error loading image : " + name
                    + "\nFatal error. The program is exiting.");
        }
    }
    return img;
}

Here the cache is a HashMap so that an image is only loaded once and all the instances use the same image.

A render() method

In this method you would draw the object to the game. See it and you may understand it without any explanation.

public void render(Graphics2D g){
    g.drawImage(img, x_pos, y_pos, null);
}

Where the null refers to ImageObserver class which we do not need. To draw the object, simply call this method from the paint method of the game. (Don't forget to cast the legacy Graphics class to the new Graphics2D class).

Reference

You may see this class as a reference. Just rename it to graphicalObjects and use it up.

GObject class

List of pre-created actions (useful for beginners) like bounce() etc.,

Pre-Created Actions (Inspired by Game Maker)

\$\endgroup\$
3
  • \$\begingroup\$ -1 Answers that are just a link to something and don't really explain anything aren't very useful. What if your link goes dead? Your answer becomes useless. Try to actually say something in your answer. \$\endgroup\$ Aug 19, 2012 at 0:41
  • \$\begingroup\$ @JonathanHobbs The links will never be dead because they are from my game engine which is hosted on googlecode. And I've just given links because the class contains 774 lines of code. \$\endgroup\$ Aug 19, 2012 at 0:56
  • \$\begingroup\$ Your answer is just a link to some code. It doesn't answer the question (how to implement graphical objects and draw things in the game) and the class itself doesn't count as an explanation - it's just a lot of code which you'd only be able to fully understand if you didn't have to ask this question. The list of pre-created actions itself has no apparent meaning. You're not answering anything or explaining anything. \$\endgroup\$ Aug 19, 2012 at 1:02

You must log in to answer this question.