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 / General / March 2006

Tip: Looking for answers? Try searching our database.

Scale a Graphic

Thread view: 
rpond - 08 Mar 2006 22:31 GMT
I want to print the contents of a Component.  The problem is that if
the size of the component is greater than the size of the page, it does
not print the whole component.  I want to be able to scale down the
Graphic in the print() function so that it fits the page.  I plan to
use the Graphic2d.scale() method.  How can I get the page size to
determine the correct scale?

Thanks.
Alex Hunsley - 10 Mar 2006 11:51 GMT
> I want to print the contents of a Component.  The problem is that if
> the size of the component is greater than the size of the page, it does
> not print the whole component.  I want to be able to scale down the
> Graphic in the print() function so that it fits the page.  I plan to
> use the Graphic2d.scale() method.  How can I get the page size to
> determine the correct scale?

To get the size of a (realised) JComponent, then call getSize() on it.
It will return a Dimension containing pixel size, let us call that d1.
Now suppose the dimension (in pixels) of your screen or display area
(printout) is d2.

Now, I'm assuming you want conservative scaling, i.e. you can see all of
the component on the screen, and you wish to maintain the aspect ratio...

Work out the width/height ratio of the two dimensions:

r1 = (float)d1.getWidth()/(float)d1.getHeight();
r2 = (float)d2.getWidth()/(float)d2.getHeight();

Now you examine these two ratios to decide whether to scale your graphic
to fit on the X or the Y scale:

float scaleFactor = 1;
if (r1 > r2) {
 // we need to scale by the width of the
 // component, as it is proportionally wider/fatter than
 // the screen area
 scaleFactor = (float)d2.getWidth() / (float)d1.getWidth();
}
else {
 // we need to scale by the height of the
 // component, as it is proportionally taller than
 // the screen area
 scaleFactor = (float)d2.getHeight() / (float)d1.getHeight();
}

Then set the scaleFactor on the Graphics2D object before rendering:

g2d.setScale(scaleFactor, scaleFactor);

I've not actually tested this code, but you get the idea...
alex


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.