Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Swing Graphics and JPanel setPrefrerredSize

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3214
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3214
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Sheriff
Posts: 22080
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron Summer wrote:


Looks like scaling to me.
 
Rob Camick
Rancher
Posts: 3214
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic