Skip to main content

how to draw an image on a JPanel

2 replies [Last post]
renzord
Offline
Joined: 2007-10-18
Points: 0

how to draw an image on a JPanel?

This is a very newbie question but I have already searched the web looking for a solution and I have not found the answer. I have seen how to draw it over a JFrame but I dont understand how to put it on a JPanel.

Thanks in advance

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Nidel, Mike

If it's a small image, you can load it into an ImageIcon and then pass
it into the constructor of a JLabel.
Then you can place the JLabel in your JPanel.

Just make sure that if you load the image using Toolkit.getImage() you
need to wait for it to load using a MediaTracker...
;)

> -----Original Message-----
> From: Discussion list for Java 2D API
> [mailto:JAVA2D-INTEREST@JAVA.SUN.COM] On Behalf Of
> java2d@JAVADESKTOP.ORG
> Sent: Friday, November 16, 2007 5:46 PM
> To: JAVA2D-INTEREST@JAVA.SUN.COM
> Subject: [JAVA2D] how to draw an image on a JPanel
>
> how to draw an image on a JPanel?
>
>
> This is a very newbie question but I have already searched
> the web looking for a solution and I have not found the
> answer. I have seen how to draw it over a JFrame but I dont
> understand how to put it on a JPanel.
>
> Thanks in advance
> [Message sent by forum member 'renzord' (renzord)]
>
> http://forums.java.net/jive/thread.jspa?messageID=245886
>
> ==============================================================
> =============
> To unsubscribe, send email to listserv@java.sun.com and
> include in the body of the message "signoff JAVA2D-INTEREST".
> For general help, send email to listserv@java.sun.com and
> include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

renzord
Offline
Joined: 2007-10-18
Points: 0

here is the answer

ImageIcon icon = new ImageIcon("drawing.jpg") ;
g.drawImage(icon.getImage(),0,0,null) ;