Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2006

Tip: Looking for answers? Try searching our database.

drawing an image in a JPanel

Thread view: 
Brandon McCombs - 18 Jul 2006 04:34 GMT
I have a custom class extending JDialog. Within this dialog window I'm
going to have multiple JPanels to organize the textfields. I want to
show a jpeg photo of people in this dialog and to have more control over
where the image goes (since it isn't possible to simply add an image to
a JPanel as far as I know) I created a private class extending JPanel
and it only holds an image.

I create an instance of this "photo" panel and add it to one of the
panels in my dialog window. So the photo is basically embedded in a
total of 2 JPanels and then the JDialog.  The problem is that I only get
a small white box when I display the image this way. If I instead place
the image directly into a JPanel in my dialog the image shows up.  This
method causes the photo to only be embedded in 1 JPanel instead of 2.

I've tried resizing the dialog window and it didn't help. The
ImageObserver for the photo is the custom JPanel class which is the
first JPanel the image is placed in and I've modified the paint() method
of the JPanel to attempt to make the photo show up.  When I use the
other method I modify the paint() method of the JDialog and it works.

any ideas?

thanks
Bart Cremers - 18 Jul 2006 06:55 GMT
Brandon McCombs schreef:

> I have a custom class extending JDialog. Within this dialog window I'm
> going to have multiple JPanels to organize the textfields. I want to
[quoted text clipped - 19 lines]
>
> thanks

My guess is you're having a layout manager problem, but without seeing
some actual code it's hard to help you out.

Regards,

Bart
swebb99@gmail.com - 18 Jul 2006 11:05 GMT
> I have a custom class extending JDialog. Within this dialog window I'm
> going to have multiple JPanels to organize the textfields. I want to
[quoted text clipped - 19 lines]
>
> thanks

I'd add a JLabel to the panel and set the labels icon to the image.
Remember that you may need scale the image for the size of the panel if
you want to keep aspect ratio's and have a certain size .....

I use code a bit like this :-

final Image scaledImage = scaleImage(originalOverviewImage,
overviewPanel.getWidth(), myPanel.getHeight());
final ImageIcon myIcon = new ImageIcon(scaledImage);
final JLabel label = new JLabel(myIcon, SwingConstants.CENTER);
myPanel.add(label, BorderLayout.CENTER);

private Image scaleImage(final Image originalImage, final int width,
            final int height) {
        Image newImage = null;
        if (originalImage != null) {
            final double originalHeight = originalImage.getHeight(this);
            final double originalWidth = originalImage.getWidth(this);
            final double heightScale = height / originalHeight;
            final double widthScale = width / originalWidth;
            final double scale = heightScale < widthScale
                    ? heightScale
                    : widthScale;
            final int newHeight = (int) (originalHeight * scale);
            final int newWidth = (int) (originalWidth * scale);
            newImage = originalImage.getScaledInstance(newWidth, newHeight,
                    Image.SCALE_DEFAULT);
        }
        return newImage;
}
Brandon McCombs - 26 Jul 2006 04:00 GMT
>> I have a custom class extending JDialog. Within this dialog window I'm
>> going to have multiple JPanels to organize the textfields. I want to
[quoted text clipped - 50 lines]
>         return newImage;
> }

thanks, that worked great.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.