• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Graphics vs. JLabel??

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alex McCormick
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
He does not suffer fools gladly. But this tiny ad does:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic