• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

paintComponent usage

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I understand that all custom painting should be done inside an override of paintComponent(Graphics g)...

BUT

I have a GUI class which extends JFrame. It is essentially a google earth-like map app, and I want to have a mini map showing the current position, like in video games. I intend to do this by painting on the graphics context of the JLabel containing the image of the mini map.

If I redefine paintComponent(Graphics g) in the body of the class, I am not dealing with the graphics context of the JLabel, which is instantiated inside the class. I want to paint on the label, not on the window overall.

I could call Jlabel.getGraphics() and then draw on that but I gather that this is not a good way of doing it.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inherit from JLabel and override its paintComponent and do your custom drawing in there. Be sure to call the super.paintComponent() as your first statement in your custom method. However, from what you say it sounds like what you should do is to replace the JLabel with a JPanel instead, and just draw your graphics and text in it (using the same approach discussed above). HTH
 
Lorenzo Levrini
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much Chris. Of course, I really should have thought of that. I'll try that now and let you know how it goes.
 
Lorenzo Levrini
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works great mate. Thanks loads.
 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yer welcome!
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic