newbee question

hi people,

i am new to java games programming and have a simple question.

i programmed a simple game loop, have a map class with a 2d map array. from the game loop in my main class i start my gui class which should draw my array map with the paint (Graphics g) method, into the frame (gifs should be drawn right into the frame without a canvas or something like that)

when my program starts, the game loop and everything runs smoothly, except that the frame i open with the gui class is empty. when i start the gui class alone with a simple main method, the screen frame is not empty.

what am i doing wrong, do i have to call another repaint or update or something like that after i start the gui?

thx

[739 byte] By [ludoludoa] at [2007-9-29 14:09:22]
# 1

> start my gui class which should draw my array map with

> the paint (Graphics g) method, into the frame (gifs

> should be drawn right into the frame without a canvas

> or something like that)

This sounds a little off. How exactly are you drawing?

> what am i doing wrong, do i have to call another

> repaint or update or something like that after i start

> the gui?

You need to call repaint() to tell AWT to refresh the screen, but

it should display at least once, so I doubt that's the cause of this problem.

paulcwa at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 2

ok, here is some of my gui code:

public class JArena extends Frame {

Image leftBot, rightBot, floor, wall;

public JArena () {

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

dispose();

System.exit(0);

}

});

leftBot = java.awt.Toolkit.getDefaultToolkit().createImage("mapIcons/leftBot.jpg");

floor = java.awt.Toolkit.getDefaultToolkit().createImage("mapIcons/floor.jpg");

}

public void start () {

System.out.println("starting arena...");

JArena mainFrame = new JArena();

mainFrame.setSize(400, 420);

mainFrame.setTitle("ProBod");

mainFrame.setVisible(true);

}

public void paint(Graphics g) {

g.drawImage(leftBot,0,20,this);

g.drawImage(floor,40,60, this);

}

}

the start method is called from my main gameloop...

ludoludoa at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 3
By using Toolkit, your images are probably not fully loaded before you try drawing them.Replace java.awt.Toolkit.getDefaultToolkit().createImage(withjavax.imageio.ImageIO.read(
Seekelya at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 4
Or use a MediaTracker, I guess.I've never tried drawing directly in a Frame and I can't recall seeing anybody else do it. Why not just make your class subclass Canvas?It would probably be more flexible anyway.
paulcwa at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 5
I draw direct to frames all the time ^_^Its less code ;)btw, the images not being loaded is the problem with tthe posters code.(as mentioned above)Using passive repainting for a game aint such a smart plan either.
Abusea at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 6

> I draw direct to frames all the time ^_^

I usually draw in a Canvas, since then I can add the object to a Frame or to an Applet. Can you do this with Frames as well?

> Using passive repainting for a game aint such a smart

> plan either.

Eh? Why, and as opposed to what?

paulcwa at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 7
> Eh? Why, and as opposed to what?it's better to use active rendering for games
Woogleya at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 8

I had never heard of active rendering until now.

I did a search and only found it in the context of full-screen exclusive mode. That would explain why I had never heard about it, since I've been concentrating only on small games which (ideally) I could eventually install on my Zaurus or use in more-or-less cross-platform applets.

From what I skimmed from the tutorial, it's only available for Windows boxes. Is that correct? Also, in the page on active vs passive rendering, it has a code example that calls a method getPaintGraphics(), but I couldn't find that in the API. Am I right in thinking that that's just their way of briefly expressing "bufferStrategy.getDrawGraphics();"?

paulcwa at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 9

Passive rendering is allowing the AWT event Thread to process your repaint requests. Repaint requests are placed in the event queue, and executed asynchronously.

i.e.

your main Thread calls repaint();

at some later point in time, the AWT event Thread will then update(Graphics g) (which subsequently calls paint(Graphics g).

Active rendering eliminates this inefficiency bypassing the AWT event queue altogether.

Active rendering is not exclusive to fullscreen/BufferStrategy, you can use it when drawing onto any component. (component.getGraphics())

Abusea at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...
# 10
thanks very much for the feedback, i think i can manage it now...
ludoludoa at 2007-7-15 4:44:48 > top of Java-index,Other Topics,Java Game Development...