• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

paintComponent usage

 
Lorenzo Levrini
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
This works great mate. Thanks loads.
 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yer welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic