File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Swing Graphics and JPanel setPrefrerredSize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing Graphics and JPanel setPrefrerredSize" Watch "Swing Graphics and JPanel setPrefrerredSize" New topic
Author

Swing Graphics and JPanel setPrefrerredSize

Ron Summer
Greenhorn

Joined: Feb 05, 2010
Posts: 8
Hi all,
I hope I can get some help with this question concerning the following code.



The problem I am having is the setPreferredSize on the DrawingPanel. When the DrawingPanel object is created pictures (image) are loaded along with a description (text) of each one using Graphics2D commands in pic.ViewNewPicturesReport(g2). The scrollPanel does not scroll to the bottom of the report but only shows what one screen would look like. When I set the panel.setPreferredSize to some large dimension like (1500, 3500), then the scrollPanel will scroll to the end of the report. If I have a short amount of pictures the end of the scroll area could be way down, likewise if I have a large number of pictures and text then the scroll would stop before the end of the report. i have tried letting the DrawingPanel set the preferred size using getPreferredSize but didn't even have a scrolling area.

I couple of other items, I have tried using pack, validate, and revalidate after returning from the new DrawingPanel. All those methods doesn't seem to have any positive effect on the proper scrolling range.

How can I get the scrollPane to correctly determine what the end of the DrawingPanel's view and scroll correctly?

Everything I've seen on setPreferredSize seems to preset the Dimension in code. Can this be done using a method of Graphics2D? some other method? or maybe some formula?

Thanks for any and all help.

Ron Summer
Greenhorn

Joined: Feb 05, 2010
Posts: 8
I'm new to posting on this forum, so I not sure if I did everything correctly.

But I've had several viewers but no help.
Is what I'm asking doable? Is their enough information given to receive an answer?
Thanks.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
i have tried letting the DrawingPanel set the preferred size using getPreferredSize


That is the proper approach.

Can this be done using a method of Graphics2D?


Not that I know of.

some other method? or maybe some formula?


What have you tried? I would guess that you multiply the width/height of your original image by the scaling factor.

I'm new to posting on this forum, so I not sure if I did everything correctly


A SSCCE always provides more information. You never know someone may use the SSCCE as a starting point to determine the forumula for you to use.
Ron Summer
Greenhorn

Joined: Feb 05, 2010
Posts: 8
Thanks for the post.
The location of the images are stored in a database, their may be none or many pictures. The size of the image can varying depending upon the resolution that the picture was taken in, we don't scale them.

In the DrawingPanel, I have tried setPreferredSize(getPreferredSize()) upon returning from the pic.ViewNewPicturesReport method. Which return (10,10 being returned and no scrollbars. Which is way to small, my main Frame is set to (853, 1450).

I have tired the setPreferredSize(getPreferredSize()) after adding the panel to the JScrollBar in the ctor of ConditionViewPrintFrame with the same value of (10,10) being returned and again scrollbars.

Thanks,
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
we don't scale them.


Your posted code uses the getScaledImage() method.

Which return (10,10


Yes, that is the size of the panel

I have tried setPreferredSize(getPreferredSize())


That is not what was suggested. You need to override the getPreferredSize() method to return the size of the image that you want to paint on the panel.

You haven't posted a SSCCE so I can't help anymore.
Ron Summer
Greenhorn

Joined: Feb 05, 2010
Posts: 8
Your posted code uses the getScaledImage() method.

I think you must be confusing my code with another you are helping. I don't use that method.

That is not what was suggested. You need to override the getPreferredSize() method to return the size of the image that you want to paint on the panel.

Again I think you must be confusing my code with another you are helping. You never made that suggestion concerning this thread.

Also I'm not always dealing with a single image, sometimes their maybe none others it could be as many as 15 - 20 or more.

But thanks for the effort.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Ron Summer wrote:

Looks like scaling to me.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Add the images to a JLabel and add the labels to a panel using a layout manager and you won't have to worry about this.
Ron Summer
Greenhorn

Joined: Feb 05, 2010
Posts: 8
Rob Prime you are correct, what I should have said is that the images are not scaled using the getScaledImage() method that Rob Camick was referencing.

Rob Carmick I don't have access to the image(s) just the method that fetches, writes some descriptive line(s) of text, and renders them. So a label is not an option.

Thanks all.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4544
    
    5

If the PicturesArea class doesn't expose an API to determine directly or indirectly the dimensions of the area it paints to, then there's no way you can achieve your goal. Or at least, not without a totally hackish and inefficient approach like painting to a BufferedImage sized larger than the maximum possible size and scanning the pixels to find the highest-value column and row with non-default color.

db


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing Graphics and JPanel setPrefrerredSize