Java 2D Graphics questions

Hey everyone, I’ve been reading through the forums for the last few weeks and would like to sap some of your knowledge.

I want to write a simple online 2D game that (for now) doesn’t run in full screen mode, but is dynamic so it adapts to the user resizing the window (they aren’t allowed to make it too small). I have the online bit worked out and so far it is working well, I have a multithreaded socket-based server that can handle several clients. Data can be passed between the client and server in a satisfactory way.

Anyway on to the 2D graphics…

Here’s the basic plan for my GUI:

http://aycu11.webshots.com/image/35130/2000003063749521856_rs.jpg

I first made this using Swing and it worked well, but I couldn’t figure out how to customise the Swing components. What I would like is to have my own images for the border of the components (such as JTextField) and my own images for the background in the box, etc. Is this possible with Swing or any thing similar?

Another thought was to take control of this all myself. I set up a thread which renders the screen in a buffer and then paints the image straight to the JPanel. I could then draw what I want. I would have to code everything such as changing the mouse to the Cursor.TEXT_CURSOR when they go over the area that I designate as being the text area, then handle keyboard input and display the appropriate characters, etc. I’m assuming this isn’t the way to go as there’s so much work involved, all of which has been done before. Below is a screenshot of my first attempt at this method, where I draw the text and lines:

How can I get the custom graphics in the GUI how I want, but still have all the nitty-gritty bits done for me, like with Swing? Is using one of the freely available Graphics API’s the answer?

I hope this makes sense, sorry for the long post!

Cheers.

You could extend JTextfield and customize the painting of the control. That way you don’t have to write the keyboard handling etc.

Generally, this just requires overriding the paintComponent method.

I’m going a bit farther than that. In fact, my code is quickly moving away from Swing altogether. There’ll just be a bunch of custom “stuff” in a JFrame by the time I’m done. Unfortunately, that’s a long ways off.

I suggest writing your own text area, and then that solves the problem with drawing the images underneath. Also you can specify the color for each text. If you’re game needs to log the chat add the swing scrollbar to it. setValue() appropriately and store the text in a string list

I’ve decided to go with drawing my own components and everything else all inside a canvas, thanks for the replies and input.

I’ve read through a Java Sun tutorial on creating your own GUI components
(http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html)

They have a section on only painting things that have changed instead of painting the whole screen. My plan is to split the canvas into sections. The communication section (where text output is displayed, and where you enter messages) will only be painted when things change, such as the user typing/deleting something from the designated text input area.

Is this a good idea? and is it easily do-able when using active rendering in a “render loop”?

[quote]Generally, this just requires overriding the paintComponent method.
[/quote]
You can do a lot more then just override paintComponent though. You can write a completely custom component. You can override the look and feel by plugging in your own UI delegate. This can be completely custom or you can extend an existing one and just override the painting parts.

[quote]They have a section on only painting things that have changed instead of painting the whole screen. My plan is to split the canvas into sections.
[/quote]
Its usually easier to just repaint the whole screen. You don’t want to spend too much time figuring out what is stale and needs repainting. Especially when you in rendering loop.

Something like Swing only redraws based on events from keyboard and mouse so it only repaints affected areas. There is no reason why you couldn’t do this. Though I am not seeing what the benefit of this would be.

Also have you taken a look at FengGUI?

No I hadn’t heard of it before, it seems interesting and could save me lots of time but still produce great results.

I’m going to stick with using Java2D until I feel comfortable with it.

I’ve been trying to work out how to use hardware accelerated images and have come across something strange… I first tried loading an image outside of my game loop and then continually drew that image once each loop:

                // Before game loop
		BufferedImage normalImage = null;
		try {
			normalImage = ImageIO.read(new File("transparent.png"));	
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Inside game loop
		g2d.drawImage(normalImage,100,100,null);

This gave me good frames-per-second. I then tried making an accelerated image like this:

                // Before game loop
		BufferedImage normalImage = null;
		BufferedImage acceleratedImage = null;
		try {
			normalImage = ImageIO.read(new File("transparent.png"));	
			acceleratedImage = gc.createCompatibleImage(normalImage.getWidth(null),normalImage.getHeight(null),Transparency.BITMASK);
			acceleratedImage.getGraphics().drawImage(normalImage,0,0,null);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// Inside game loop
		g2d.drawImage(acceleratedImage,100,100,null);

This gave me lower FPS by around 10%! Does anyone know what is going on?

Using Java2D what is an efficient way of loading and displaying accelerated images? I’ve read up from some tutorials but I’m not clear on what the best solution is.

If anyone has information please let me know, I would appreciate it alot.