Hi all, I've got what's probably a dumb question on using the Graphics class in my GUI. Let's say I'm building a simple card game like war. Should I be using Graphics to "draw" the card images onto my GUI? (I've got a .gif image for each card in the deck) I'm currently using JLabel's to display the cards, and although it works, I'm not sure that's the best way to do it. I've never done this before, so I'm not sure if Graphics is the "standard" way to display things on the GUI. Thanks for any help!
Originally posted by Alex McCormick: Let's say I'm building a simple card game like war.
...quite a statement! JLabels *do* support images, with or without text. You could also use ImageIcon class, though some people prefer JLabel in certain cases. IMHO, you're doing the the right thing. If you needed to dynamically generate or alter an image, you would probably have to go to Graphics, but it sounds like you just need to display the images you have. As a general rule, maintenance-wise, and performance-wise, it is almost always better to use the Java classes when you can. hth
Just to extend on what clio said the only time you would really want to use the Graphics class for displaying images is if the component you want graphics on doesn't already accept images by default (i.e. JLabel, JButton, etc) or if you need to manipulate said image prior to displaying it. One example of when to use the Graphics class for displaying images is if you want to add an image onto a JPanel or a JFrame for a background image. Then you would override the paintComponent(Graphics g) method of said component and draw you image their. I agree with clio that you are on the right track. Another helpful hint if you are making a card game, the CardLayout layout manager is good for stacking components (cards) one on top of the other. Good luck.
Thanks guys! I will definitely check out CardlayoutManager. One other quetsion: I've seen a number of java card games on the web (applets, I believe) and they often have a bit of animation along with the displaying of the cards. For instance, when choosing which cards to discard from your hand, clicking on a card will cause the card to move or change position a bit. So far, I'm unable to figure out how to do that using JLabels. -which is kinda what got me thinking about using Graphics. My JLabels seem very "stationary," and if I wanted to move the cards around a bit I'm not sure how I would do that. Anyway, any additional ideas would be great! Also, what do you mean by "using the Java classes when you can"? Is Graphics not a native Java class or something? Thanks again!
If you want to do a lot of animations and movement, you might be better off not using JLabel, but just using Graphics directly. This doesn't mean that *all* your code has to go into one class's paint() method. What I usually do is make simple classes that model objects in my game, and have methods that the parent class can pass coordinates and it's Graphics object into, then the specific game object can paint itself, but you don't get all the extra stuff that you don't need from a full-blown component class like JLabel.
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joined: Apr 30, 2004
Originally posted by Alex McCormick:
Also, what do you mean by "using the Java classes when you can"? Is Graphics not a native Java class or something?
You're right - I should have said "java components" rather than "java classes". The components tend to be optimized in harmony with each new compiler release, and the documentation usually gives you an indication of specific optimizations made in various releases. Further, you can solicit deprecation warnings and consult the docs for anything that might be on it's way out ... Of course that doesn't mean you shouldn't go ahead and work on your own custom classes for your specific needs. But it sounds like you're making great progress already!