keylistener <->graphics and speed problem

hello,

ive been trying to make a little java game (cfr bomberman) and im experiencing several problems.

the main problem is that the only way i can implement both graphics and a keylistener is by putting them in one class (so both extends canvas and implements keylistener)

but i wouldnt really call this object oriented, so ive been trying to separate them, in several ways, but it alsways works out that one of the two doenst work. if i add them separately to a jframe only one of them works, and i dont see a way to get them both referred to the same frame

another problem is that the game runs rather slow. there are around 60 objects to get onto the frame (all 25x25 pixels) and they are loaded only once for the running of the whole program, but still framerates around 20 are my max. can this be a problem with eclipse (design environment)

last problem, i always have to click the frame (only once) to be able to get key events, even if the frame is already the active one.

hope anyone got answers, and if you want any code, just ask

thx

[1093 byte] By [Seahadesa] at [2007-10-2 12:19:01]
# 1

Okay, first of all, KeyListener is not something that should be added to the frame; you add the KeyListener to the canvas, which is then added to the frame.

for example, if you wrote a class called CustomKeyListener that handled all your key events, then you would do this to get them both to work correctly:

Canvas canvas = new Canvas();

frame.add(canvas);

frame.addKeyListener(new CustomKeyListener());

That should add the canvas to the frame, then register the custom listener with the frame. Now, for your other little thing about having to click on the frame once, did you add the KeyListener to the Canvas or to the Frame? If the key listener is added to the canvas, then you need to call canvas.grabFocus() at the end of initializing your game. (The problem was that the key event was actually registering with the frame and not the canvas that has the key listener). Now, if the listener was added to your frame, and the events still don't register in your KeyListener, then you'll just need to post your initialization code and I'll see what I can do =D

Now, for the FPS, how are you drawing everything? Are you double-buffering with something besides a VolatileImage? (don't know what the hell i'm talking about? Ignore that) How does your game loop?(does it just repeatedly update and then repaint()?)

Your design environment should have NOTHING to do with performance if you are in fact using Frames and not an Applet (of which Eclipse has its own little appletviewer... but i've still gotten over 100 FPS in that...)

In order for me to help you with the FPS, I'll need to know SOMETHING about what you're doing. (Just letting you know, with a little bit of graphics enhancements, I got a little space invaders game to run full screen on my little 2003 laptop with a 1440x900 screen at *GASP* 225 FPS BABY).

AHH that could be a problem (just looking over your post one last time)... It is MUCH more efficient to use AWT for rendering than Swing. Swing is really designed to use JButtons, JLabels, etc., so it is pumped up with a bunch of stuff that you don't need if you're writing a game. Now, if you plan on using JButtons and stuff in your game, you might have to play around a little bit to get the buttons to be compatible with your game graphics...

What am I trying to say? Use a Frame, not a JFrame.

Hope I can help you in the future (after a little more information)

Alex

aRyan316a at 2007-7-13 9:07:01 > top of Java-index,Other Topics,Java Game Development...