How can I combo Java2d with LWJGL?

I am seeking for a chance to use Input polling provided
by LWJGL (Mouse and Keyboard). But how can I
set this up?
When I do this the Java2d way and just use this statement let’s say in the game loop:
if (Keyboard.isKeyDown(Keyboard.KEY_F))

where Keyboard should be provided by LWJGL, I get:

java.lang.IllegalStateException: Keyboard must be created before you can query key state
at org.lwjgl.input.Keyboard.isKeyDown(Keyboard.java:354)

How can I create the Keyboard?
Do I need to completely use OpenGL?
If yes how do I get Graphics from LWJGL’s Display?

Thank you in advance if you could tell me any possible
solution!

How can I create the Keyboard?

You need to create the display first: org.lwjgl.opengl.Display.create()
Then you can create the Keyboard by: org.lwjgl.input.Keyboard.create()

Do I need to completely use OpenGL?

I think so, if you create the display, the display window comes up and you need to do the game in there and of course it needs OpenGL functions to perform the drawing.
But note I’m not expert in this LWJGL OpenGL, only trying to help from my newbie experience :slight_smile:

If yes how do I get Graphics from LWJGL’s Display?

What do you mean by Graphics in there? java.awt.Graphics?
There is no java.awt.Graphics in OpenGL, you need to draw your images from textured triangles or quads.

You could see my LWJGL->Java2D implementation in here :
http://www.goldenstudios.or.id/products/utilities/addons/
If you have any troubles understanding the source just ask me.

For the keyboard I map all LWJGL key to Java2D key.
So I can use LWJGL Keyboard without changing my game source code a bit.
E.g: isKeyDown(KeyEvent.VK_F) -> isKeyDown(Keyboard.KEY_F)

For the graphics I make a fake java.awt.Graphics2D class and override all the methods to do the same like Graphics2D with OpenGL implementation.
So (once more) I do not need to change my game source code. :slight_smile:

Hope that helps. :slight_smile:

Thanks a lot. I though that someone would say that. :slight_smile:
I just wanted to have it proven… ;D

It’s kinda smart what you did… I mean that Java2d to
OpenGL converter thing…

Hmm… So you suggest to change the typical Java2d
fulscreen code created with the bufferstrategies with
OpenGL. That’s actually even easier. You know we
just finished some stuff with pure Java2D. But as you
might know there are sound issues comming up with
Java5 and so I decided to use LWJGL’s OpenAL implementation. And since I am already making use of it, I thought why not completely benefit from OpenGL.
But since I don’t want to rewrite the entire code into OpenGL, especially with sprites movements I though it would be advisable to ask some people in here what would be best. I saw the sample implementation <Space Invaders 104> and the TextureLoader and the Texture class and we started to rewrite the code to match our java2d implementation. Everything went well but the Fog of War images. You know we to darken them down when reading the normal the images such as
trees and other sprites. (Unexplored areas).
But now we don’t know how to darken them in OpenGL.
I know a possible solution but I once had problems using: GraphicsConfiguration and createCompatibleImage to obtain an image where I can change the alpha etc… during loading the images since I don’t want to prepare all images in all different kind of
forms (normal, unexplored)
Do you know any other way around this issue?

Thank you very much for the for your help. In
GameDev (where I have seen you) you would have gotten a ++ for those tips!!

Umm I’m not suggesting to change from Java2D to OpenGL, I’m just saying if you use the LWJGL input you need to use the whole LWJGL display, someone correct me if I’m wrong since I’m not expert on this, I only touch LWJGL+JOGL in a month or so for porting my game engine to use it and never touch it anymore.
But for OpenAL you could use it separately apart from the OpenGL, this is another thought :slight_smile:

For the alpha blending, using OpenGL is even easier, I still use GraphicsConfiguration and createCompatibleImage same as with my Java2D implementation, it’s only in the rendering the LWJGL/JOGL is take over the job, take a look at my fake Graphics2D, you could use it if you like to.

Good luck! :slight_smile: