• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPanel vs. JFrame

 
Ranch Hand
Posts: 57
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When displaying graphics, is it not possible to draw them directly to the container of JFrame? All the examples I've seen in my text involve using JPanel, but I was still curious. Does JPanel provide a separate container from that of JFrame?

I am quite the graphics novice, so forgive my naivete.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is possible to draw directly to the frame, using frame's paint(..) method,
but better to use a JPanel because of its paintComponent(..)

although, in this recent thread (with example of using JFrame and paint())
https://coderanch.com/t/343029/GUI/java/repaint-method
it was suggested paintComponent(..) should be used for the JFrame.

I still can't find a paintComponent() method for a JFrame, so I'll leave it
for you to experiment
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does JPanel provide a separate container from that of JFrame?
Yes. Confining graphics to a separate graphics component simplifies things.
You can draw graphics using a containers "paint" method but it is more complicated. The tutorial has an article (with links to others) that can help you get started:
Lesson: Performing Custom Painting
Since this is lower-level work there are a lot of things you have to figure out/put together on you own. This example tries to point out some of them.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it does not matter whether you draw your picture on Frame or Panel but you should be careful that while drawing on Frame use paint(Graphics g) method and while drawing on Panel use paintComponent(Graphics g)method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic