[java] java2D and swing

Started by
6 comments, last by Glak 23 years, 10 months ago
I was wondering how the two work together. I''m making a multiplayer turn based strategy game. The basic turn structure is as follows: input: player can scroll around map and enter commands, player can send and recieve messages in the text part of the interface input synch: all commands entered are sent to other players action: there will be 61 action phases, the interface is disabled, things are completely automated based on players input So I was wondering how it works, do I make a JFrame and then designate parts of it to work with java 2D and have the rest swing? or is the whole thing java2d and I just throw swing parts on and try not to draw over them?
Advertisement
I would do it like this:
JFrame: GUI, takes in all key-inputs and makes them happen.
Canvas/JPanel: All graphics here, handles some mouse clicks targetted to the game like targetting enemy vehicle or recieving info.


I don''t know what do you mean by:

...designate parts of it to work with java 2D and have the rest swing? or is the whole thing java2d and I just throw swing parts on and try not to draw over them...

If you draw everything on Canvas you don''t have to take care of other components. I mean you can just layout them with null-layout or borderlayout and handle their events.

Time comes, time goes and I only am.
Make sure you use JPanel and not Canvas though. I had horrible problems with JMenus and other Swing stuff until I found something on Sun''s website telling me not to mix Swing and AWT components.

Neil
Java2D is not really separate from AWT or Swing. AWT is the (older) heavyweight graphics model, Swing is the (newer) lightweight graphics model, and Java2D is the new Graphics wrapper that allows you to use new functions with either. A big no-no is to mix heavyweight and lightweight components. This means if you use AWT you should avoid Swing components and vice-versa. Another heavyweight API is the Java3D API, so using Java3D with Swing is difficult at best.

One thing to consider is that if you are using Java2D and/or Swing, you are locking yourself into the Java2 environment. This means your program will not run in a typical browser without the Java Plugin (or modification to the browser). If you are deploying an application with a runtime evironment, this isn''t a problem, just distribute the 1.2.2 or 1.3 environment. If you are planning an applet distro, this will force your players to download the Java Plugin before they can play. This may be unacceptable, which will jam you into AWT and 1.1

If you do wind up using Java2D and Swing, check out the the following classes:
JLayeredPane: this is a panel that supports layered graphics.
JViewport: a viewport class for virtual panning (scrolling)
JComponent: The base item for any Swing component. Good to subclass for your own items. (Arguably better than JPanel...but it is ultimately your descision.)
BufferedImage: The cornerstone of Java2D, an image stored in memory
ImageIcon: A quick and dirty way to get JPGs and PNGs into the BufferedImage class.
AffineTransform: Java2D class for rotation, translation, skew, scale, good stuff.

I could go on and on (but won''t), if you need more to get started, drop me an email.

ManaSink@hotmail.com
Thanks everyone. I suppose I was kind of vague. From the answers it seems that I''ll need two areas that are BufferedImages, one for the main map and one for the minimap, and then I''ll just make another part to hold the swing components. So it sounds like I need a JFrame to be the whole game window, then I''ll need to add some sort of canvas-like (but not canvas) place for my main map, which I''ll use to send BufferedImages to. I''ll need basically the same thing for the minimap. Then I''ll have another swing thing that holds all of the swing components. The swing parts should basically be responsible for drawing themselves but I''ll be the one who decides what the main and minimaps show. Ok thanks, oh and I plan on making an .exe out of it (I already figured that out, whoohoo) so I shouldn''t have that problem, at least not for a while.
A lot of this is basic stuff that I haven''t seen anywhere game related(like panning using a JViewport). Perhaps a Java specific game tutorial is in order. I''d love to put together something, as it would give me a chance to get back into the ''swing'' of things (grin) and would help a lot of others out.

Hmmmm....

Any comments?

ManaSink
I read about that viewport thing and some of the other classes. Apparently the viewport only lets you have one object, the reason must be to make it more flexible, the one object can be a container that can hold lots of objects. Does the viewport do scrolling on its own? (like in an RTS) or do you have to make it work yourself? If it does it on its own then how do you set the scroll speed? If it doesn''t then what is the point of a viewport?

Assuming that I use a viewport, would this be a good way to do things: I make the one item a container, maybe a JPanel or something like that and it holds all the squares. Then each of my map tiles would be a subclass of some swing component that I could draw on. So the viewport would only call the paint method for those squares that would be on the screen, and it doesn''t bother drawing the rest of the squares. The squares will need custom drawing methods because the stuff on the squares won''t exactly have paint methods. Oh and since the squares themselves would be swing components that might make some of the interface easier.

Oh and yes a tutorial would be very nice.
Glak-
I would suggest the following for starters:

Your game tiles extend JComponent, overriding paintComponent() to paint themselves. Often they will just draw a buffered image onto the graphics context. If their state changes, they can paint themselves differently (animated tiles, tiles that can be damaged, etc.)

Create a JPanel instance that uses a grid layout with no horizontal or vertical gap. You can also set the layout to null and just specify the tile locations manually. Add instances of all the tiles. You now have a large panel that holds your map. It may be bigger than the screen because you will use a viewport to view its contents.

Create an instance of JViewport, setting the size of your viewport. This will be smaller than your map (JPanel). Set the JPanel created above as the View with setView(). To move the extent that the viewport is windowed on, call setViewPosition() with the point you''d like to move to (in the JPanel''s coordinate system).

Does it scroll itself? I''m not sure I understand the question, but it is easy to set it up to scroll however you''d like. Track the mouse and update the position based on what edge the mouse is on, tie scrolling to the arrow keys, fix the viewport on a moving sprite, etc. Just call setViewPosition when you want it to move.

You can also use multiple JViewport instances with JLayeredPane to create layers of graphics that you can scroll individually. This is great for parallax scrolling, fixing your main sprite in the center, drawing your UI over everything else, etc..

The actual code that you created here was just your custom tile and whatever main class is setting it all up, everything else is standard Swing. As a result, your deliverabe code is very small, which is good if it will be downloaded over the net, and you get to reap the performance benefits of any enhancements to the Swing package (from 1.2 to the new 1.3 for example). Plus the simplification of being able to focus on coding gameplay rather than reinventing the ''2D scroller wheel''...

I''ll start some leg-work on the tutorial, if you have any requests / ideas , post em up.

ManaSink

This topic is closed to new replies.

Advertisement