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 / February 2005

Tip: Looking for answers? Try searching our database.

Centering

Thread view: 
freesoft_2000@yahoo.com - 03 Feb 2005 12:45 GMT
Hi everyone,

                 I am trying to center a page number on the bottom
center on a printed page but i am having some trouble doing it.

I am using the windows default page dialog where the user can select
the desired page size(ie, a1,a2,a4) but i am unable to center the page
number on the bottom center.

For now i am using hard-coded values but these values only work for the
a4 paper format.

public int print(Graphics graphics, PageFormat pageFormat, int
pageIndex)
{
//other codes
//other codes

Font f = new Font("Arial", Font.PLAIN, 12);
graphics2D = (Graphics2D) graphics;
graphics2D.setFont(f);

graphics2D.drawString(("Page " + (pageIndex + 1)), 223, 643);

}

I know centering the page number involves the FontMetrics class but i
can't seem to get it right. What i would like is for the user to select
any page format using the windows page dialog and the page number is
drawn on the bottom center of the page when it is printed

I hope someone can help me with this problem

Thank You

Yours Sincerely

Richard West
Thomas Fritsch - 03 Feb 2005 13:15 GMT
> Hi everyone,
>
[quoted text clipped - 19 lines]
>
> graphics2D.drawString(("Page " + (pageIndex + 1)), 223, 643);

A slightly less hard-coded way would be:
  graphics2D.drawString(("Page " + (pageIndex + 1)),
                        pageFormat.getWidth() / 2 - 50,
                        pageFormat.getHeight() - 200);
(See also the javadoc of java.awt.print.PageFormat)

> }
>
[quoted text clipped - 4 lines]
>
> I hope someone can help me with this problem

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')

Birkemose - 03 Feb 2005 21:06 GMT
"Thomas Fritsch" wrote...

> A slightly less hard-coded way would be:
>   graphics2D.drawString(("Page " + (pageIndex + 1)),
>                         pageFormat.getWidth() / 2 - 50,
>                         pageFormat.getHeight() - 200);
> (See also the javadoc of java.awt.print.PageFormat)

I personally would not advertice this as good programming practice.....
opinions might differ

You need to find the size of your printing area, make sure you dont get
fooled by margins..
To get the size of a text, this snippet will do, based on a graphics context
Code stripped down for clarity

public Point textSize( String S, Graphics Graph ) {
   Graphics2D Graph2D;
   TextLayout Layout;

   Graph2D = ( Graphics2D )Graph;
   Layout = new TextLayout( S, Graph.getFont( ),
Graph2D.getFontRenderContext( ) );
   return( new Point( ( int )Layout.getBounds( ).getWidth( ), (
int )Layout.getBounds( ).getHeight( ) ) );
}

Regards
LB


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.