I'm trying to create a mario-esque game in Java and am at the point of implementing a camera.

Following normalocity's example, I understand the difference between the GameWorld and the Camera that follows that objects within the context of the world.

From a technical perspective,

I have JFrame that adds GamePanel (extends JPanel) and sets its size to 500,500 (that's the window size that I see pop up when I run the program).

Before learning about GameWorlds and such... I was using GamePanel as a generic JPanel to paint my objects and to implement boundary rules, timer, paintComponent, scoring, keyboard input, collision check... All painted objects are placed and shown relative to GamePanel's coordinates/size.

Now I'm trying to implement a Camera...

Is Camera a smaller JPanel added to the GamePanel? How do I make my GamePanel more like a GameWorld with a huge size (10,000 x 10,000 px), and only render everything on that GamePanel through the Camera JPanel?

From the GamePanel, I've tried altering the coordinates of the Camera using arrow keys... the camera coordinates of course change, but the objects painted on the GamePanel do not move with respect to the coordinates of the Camera.

EDIT: I'm using JPanel because that's what I'm familiar with. If you have a better suggestion on game dev in Java, please let me know!


2 Answers 2


You've misunderstood what a GameWorld is. It is not a concrete JPanel, it is not a concrete frame rendered on the screen. The world is the collection of all objects and their position, it is something more abstract.

The camera is not a JPanel either. It is a set of rules that allow you to decide what part of the world should be drawn.

For example, imagine you are looking at your desk and you have tons of stuff cluttered. The top of desk is your world. Your eyes are your camera and based on their natural constraints you only see a part of the world.

Let's get technical now. How do you define the camera? Well, it simply a rectangle - (x, y, width, height). How do you decide what to draw? Well, a sprite is simply another rectangle. In order for it to be visible by the camera, the rectangles defined by the sprite and the camera must intersect.

Here's the code that tests for intersection:

bool overlaps(r1, r2) {
  return !(r2.x> r1.x + r1.width || 
           r2.x + r2.width < r1.x || 
           r2.y> r1.y + r1.height ||
           r2.y + r2.height< r1.y);

Now in your draw / render method, test each sprite for intersection with the camera. If the result is negative, skip the sprite. If it is positive - you should draw the sprite at appropriate screen coordinates. Using some basic math, you can see that the sprite should be drawn at screen coordinates (sprite.X - camera.X, sprite.Y - camera.Y). And that's it.


If I am understanding this correctly, you wish to render only the objects on GamePanel that are within the visible bounds of the camera? I don't think that using a JPanel and moving it is the best of ideas, but maybe what you can do is override the paintComponent procedure of JPanel and do your rendering there. You may need to take a look at java graphics if you are not too confident with it.

In every game that I have made, I have overridden the method and used an x- and y-offset (for the camera x and y) and I use this method: g.drawSomething(thing, thing.X + xOffSet, thing.y + yOffSet);

And when you press the arrow keys or a & d, you update the x- and y-offsets instead of moving a JPanel. Do all the rendering within just one panel.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .