Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (654)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Tile map game  (Read 3296 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Devvie

« Posted 2007-06-08 22:02:33 »

Hi, i'm just starting to try game development using java
I already know the basic stuff like the collections, arrays, objects, classes, simple gui forms
I started gathering all the information i can get my hands on and started trying out easy coding examples. What i'm trying to create is a tilemap with predefined pictures, turn based, keybord input, 2 players.

I'm trying to use MIDP 2.0 because it includes a Game API and a layerManager wich i think is just what i need.
So i tried following this tutorial at

But it gives an error when i try to run it.

I did the basic game loop with a rotating X  that can be controlled by the keybord...
import javax.microedition.lcdui.*;

public class SimpleGameCanvas
    extends GameCanvas
    implements Runnable {
  private volatile boolean mTrucking;
  private long mFrameDelay;
  private int mX, mY;
  private int mState;
  public SimpleGameCanvas() {
    mX = getWidth() / 2;
    mY = getHeight() / 2;
    mState = 0;
    mFrameDelay = 20;
  public void start() {
    mTrucking = true;
    Thread t = new Thread(this);
  public void stop() { mTrucking = false; }
  public void run() {
    Graphics g = getGraphics();
    while (mTrucking == true) {
      try { Thread.sleep(mFrameDelay); }
      catch (InterruptedException ie) { stop(); }
  private void tick() {
    mState = (mState + 1) % 20;
  private void input() {
    int keyStates = getKeyStates();
    if ((keyStates & LEFT_PRESSED) != 0)
      mX = Math.max(0, mX - 1);
    if ((keyStates & RIGHT_PRESSED) != 0)
      mX = Math.min(getWidth(), mX + 1);
    if ((keyStates & UP_PRESSED) != 0)
      mY = Math.max(0, mY - 1);
    if ((keyStates & DOWN_PRESSED) != 0)
      mY = Math.min(getHeight(), mY + 1);
  private void render(Graphics g) {
    g.fillRect(0, 0, getWidth(), getHeight());
    g.drawLine(mX, mY, mX - 10 + mState, mY - 10);
    g.drawLine(mX, mY, mX + 10, mY - 10 + mState);
    g.drawLine(mX, mY, mX + 10 - mState, mY + 10);
    g.drawLine(mX, mY, mX - 10, mY + 10 - mState);

I transferred the classes to the java dir and added them into the program.
But it gives an error at g.setColor(0xffffff);
g is null hence the java.lang.nullpointer exeption... how can a example be wrong, or is this only made for cellphones?
If i'm realy wrong what classes should i use then to create a tile map, letting 2d pictures move around it and turn based?
Offline keldon85

Senior Devvie

Medals: 1

« Reply #1 - Posted 2007-06-08 23:59:39 »

getGraphics() is not the way! Change render to the paint method of the Canvas and instead call repaint(). This will cause a call to Canvas.update(g), clearing the canvas with the background colour, which then calls Canvas.paint(g). If you're wondering where it gets g from, well that would be the Graphics g associated with the current draw of the frame.

Offline stef569

Junior Devvie

« Reply #2 - Posted 2007-06-09 12:41:42 »

I don't understand what you just said, the example given on the website iswrong?
I'm having a hard time finding out what the methods do. But i'll tell you what i did.

First i removed the line Graphics g = getGraphics(); because i shouldn't use it for some reason.
Then the name of the function render should be changed to paint
and i should call repaint, paint in it but there i lost you

When i enter the function paint( it needs Graphics) and won't compile Angry
I'm alsoo unsure if i'm ment to use the game classes in javax.microedition or mayby i need different ones, not for cellphones ?? I just want to run it on a pc and
if i keep using them can it be run on a compputer aswell then?
Mayby somebody know a site for starters in java game programming?
Note i use IntelJIdea 4.5
so many questions...

btw I found that this game was +- what i had in mind.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Devvie

« Reply #3 - Posted 2007-06-09 14:48:12 »

I can't help you because I don't what is the super class (GameCanvas) based on.

keldon85's advice were based on swing I guess... I don't think the example you have is based on swing.
Offline stef569

Junior Devvie

« Reply #4 - Posted 2007-06-09 14:56:01 »

ok, nevermind the example from the website.

let's put it in a short Question, I want to create a game on my local pc using 2D, Tile map using an Array, and Turn based.

What Game classes are there that support the above.
and can the MIDP 2.0 Game class be used not for mobil phone games but just for the pc. There is where i found it.
Offline keldon85

Senior Devvie

Medals: 1

« Reply #5 - Posted 2007-06-09 18:07:58 »

I can't help you because I don't what is the super class (GameCanvas) based on.

keldon85's advice were based on swing I guess... I don't think the example you have is based on swing.

Oh right; when I read in the documentation that GameCanvas extended Canvas I thought they meant the Java Canvas not their own random_canvas. For some reason GameCanvas is not loading!

Offline Kova

Senior Devvie

« Reply #6 - Posted 2007-06-09 20:02:54 »

but his, stef569's, link works ... sorry I'm not going into that deep to examine classes
Offline lhkbob

JGO Knight

Medals: 32

« Reply #7 - Posted 2007-06-09 21:23:13 »

I highly doubt that the MIDP library will work just on PC's. Looking at the GameCanvas documentation, I noticed one thing that makes me believe this:  It had static variables to represent various keys, and these keys were for ones that wouldn't be used on a PC for a game, they seemed tailored for cell phones.

Perhaps you chose to use an external library for your 2D game stuff to make it easier, and there probably is one out there, but it's not that hard to do what you need, especially because Swing and AWT do most of it.  If you do do it yourself, you will learn so much more about basic Java programming that wouldn't have learned by using someone else's work (not to say that reusing is bad, but for beginning things, it's a good learning experience to do it yourself).

Offline keldon85

Senior Devvie

Medals: 1

« Reply #8 - Posted 2007-06-09 21:52:44 »

One question; what programming experience do you have? What are you aiming to achieve? And what do you know about how you need to go about getting started with games development?

I am asking so that we all know where you are coming from, that way you can be better directed on an appropriate route. There is nothing wrong with asking such a question, especially if there is no FAQ in an obvious place telling you this answer.

Offline stef569

Junior Devvie

« Reply #9 - Posted 2007-06-09 23:29:57 »

Thanks for the replys!

Keldon i have no problem awnsering to your questions:
what programming experience do you have?
I'm a student First grade Informatics, and I got an introduction into java and c.
An introduction means the difference between classes and objects, inheritance, public/private, collections, arrays, simple gui forms.
I didn't had: Drawing, buffering, shading, big projects(never more then 5 classes), animation.

What are you aiming to achieve?
I'm trying to learn the language better by using it, i've been thinking a long time to make a game, we actualy got a project for the c language
where i made a 4 on a row game text based. But i couldn't draw on the screen too complicated.
Then Java was about objects and it looked much easier and clear.
Then i saw a simple game on nintendo ds and i tought This is not so hard, let's try that. It's called Advanced wars.

And what do you know about how you need to go about getting started with games development?
I've been reading alot about it on the internet, I tried code moving triangles over myown canvas, saw tile based maps wich realy looked easy, stumbled over a game api with all kind of special stuff but that didn't work
then I came here. Later i found out it was for mobile phones...

lhkbob, i'll start with using awt and swing and maybyi'll post myfinished program here in some umm months?
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

bilznatch (24 views)
2015-08-04 11:03:17

SHC (39 views)
2015-08-01 03:58:20

Jesse (24 views)
2015-07-29 04:35:27

Riven (45 views)
2015-07-27 16:38:00

Riven (25 views)
2015-07-27 15:35:20

Riven (27 views)
2015-07-27 12:26:13

Riven (18 views)
2015-07-27 12:23:39

BurntPizza (41 views)
2015-07-25 00:14:37

BurntPizza (53 views)
2015-07-24 22:06:39

BurntPizza (33 views)
2015-07-24 06:06:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!