Custom GUI

Howdy. I want to write a custom GUI for my game and not use swing or awt components (or just subclass the main super class, is fine.)

I am not sure where to begin - should I use a Canvas, or a Container and do my drawing on my screen there? I just need a step in the right direction. Thank you!

If you don’t want to use any Components except for the main frame, you can just override its paint method. The main frame would probably be either a JFrame or a JApplet.

Once you override the paint method, you can draw whatever you want to the Graphics parameter. Unless you disable the repainting, the paint method will be called automatically by the JVM - you need only override the method with the painting behavior you want.

[quote]I want to write a custom GUI for my game and not use swing or awt components
[/quote]
Hi, if your games is opengl based you can try FengGUI (www.fenggui.org)

Johannes

Thank you for your replies. I have managed to make a lot of headway but have stumbled onto a small problem.

I am trying to display text on the screen and put say, a rectangle around it. The problem is I do not know how to get the bounding box of a string of text without using the graphics component. I would like to generate the bounding box in the objects constructor instead of in the paint method.

For example:

public myclass(String text, int x, int y)
{
Rectangle bounds = new Rectangle();
int width, height;

     //code here to get the bounding of text
     width = text.getWidthInPixels();
      height = text.getHeightInPixels();

     bounds.x = x;
      bounds.y = y; ....etc

}

Anyways, it seems the only way that I know of doing is in the paint type methods of the swing components. Is there another way?

Thanks

The Font class has several “getLineMetrics”, but they require a FontRenderContext. I don’t know what a FontRenderContext is, but if you can get an appropriate one, you should be able to get a LineMetrics object. The LineMetrics class looks like it can give you the information you need.

From the java API about the font render context:

“Typically, instances of FontRenderContext are obtained from a Graphics2D object. A FontRenderContext which is directly constructed will most likely not represent any actual graphics device, and may lead to unexpected or incorrect results.”

lol, it seems the only way to do it is to get a graphics object. so what i did was to just ghetto it up in the paint method with the graphics object as a subclass. kinda sux. i dont see why java needs a graphics object to get the width and height of characters…oh well! if anyone knows a better way, do tell!

Hi,
Some guru can correct me if I’m wrong but I think that java needs a graphics because you can use AA, scaling or any kind of transform that could modifiy the bounding box of you text line.So it makes sense to me. But it’s also sure that sometimes it’s a pain in the neck to forward a g2d object everywhere in your painting code. Good design can reduce this pain nevertheless…

makes sense, but there still should be a way to get a simple bounding box for reference purposes. like if you were doing a null layout and wanted to position a component +5 the width of the length of the text of the previous component, etc.