my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes How to include a jpg in a Graphic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to include a jpg in a Graphic" Watch "How to include a jpg in a Graphic" New topic

How to include a jpg in a Graphic

Paul muellerl

Joined: Dec 09, 2012
Posts: 7
I have a big problem.
I started some weeks ago with swing and now I dont know how I can include a jpg in a Graphic.
Here what I allready have:

but the jpg is in another window and not in the same.
Thanks4 your help
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Here what I allready have:

delete it and start again - never create a JFrame inside paint()
(and paint() should not be used - override paintComponent() in Swing)
Rob Spoor

Joined: Oct 27, 2005
Posts: 19655

You've already created an ImageIcon. You can retrieve an Image from it. That Image can be drawn on the Graphics object.

Note however that you shouldn't load the ImageIcon each time paint or paintComponent is invoked. This could occur many times a second, and you don't want all that I/O all the time. You should create the ImageIcon in the constructor and assign it (or its Image) to an instance field, and use that instance field when painting.

How To Ask Questions How To Answer Questions
It is sorta covered in the JavaRanch Style Guide.
subject: How to include a jpg in a Graphic
Similar Threads
Image in JFrame ???
java swing repaint
GUI Labels and Buttons
Working with Image Icon
how to paint some shape over another JComponent