i'm creating a small swing appln. in that i need to display my photo(jpg file) in the frame's WEST region. for that i created MyPanel class(inner class) to display it in the frame. i need the code snippet inside the paintComponent method to get my picture from a particular path.
Originally posted by pascal betz: you should be abble to create an JLabel with an ImageIcon out of a JPG file, then just add this label wherever you want to. no need to write your custom paintComponentn method.
About loading an image from inside the paintComponent method: this is not recommended. Java calls this method often according to its own needs. To demonstrate this you can include a System.out.println statement inside the method and see what happens as you disturb the parent JFrames environment (minimize, restore, cover/uncover with other apps).
You need to load the image only once. The place to load it is in the class constructor or a method called from the constructor. Save a reference to it in class scope (member variable) and it's ready whenever you want to use it.
Here's a demo that shows how to display an image in two ways:
[ September 02, 2004: Message edited by: Craig Wood ]