File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How to Add Graphics to Jpanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to Add Graphics to Jpanel" Watch "How to Add Graphics to Jpanel" New topic

How to Add Graphics to Jpanel

Behrouz Hosseini
Ranch Hand

Joined: Feb 15, 2010
Posts: 31
Hi again,
I just wondering how I can add a Graphics object like rectangle to a JPanel?
I have following classes and I can add the Label object into MyPanel but I cant add drawing object (MyRect) into Jpanel.I know that I am not constructing any thing in MyRect() constructor! so it make sence for me that nothing will happen in MyPanel when I call for MyRect constructor but I don't' know how to call the paintComponent with it's parameters in MyPanel class!
Could you please let me know is it possible to add a drawing into a panel?
so what's wrong with this code?

Best Regards,
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
debugging 101:

after adding/displaying, print out the panel's size
I agree. Here's the link:
subject: How to Add Graphics to Jpanel
It's not a secret anymore!