Java printing

centering a title in the middle of the page

Page 1 of 1

2 Replies - 1460 Views - Last Post: 04 June 2007 - 01:07 PM Rate Topic: -----

#1 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Java printing

Post icon  Posted 03 June 2007 - 03:26 PM

Hi all! I am making a simple printing app in Java, and I want to have the title centered in the page, no matter how long it is. Here is a part of my code for printing:

public int print(Graphics gx, PageFormat pf, int page) throws PrinterException {
	//Translate graphics
	Graphics2D g = (Graphics2D)gx;
	g.translate(pf.getImageableX(), pf.getImageableY());

   //Variables
	int x = 10;
	int y = 10;

	//Rendering
	g.setFont(new Font("Bookman Old Style", Font.BOLD, 18));
	g.drawString(txtTitre.getText(), x, y);
			
	//Page exists
	return PAGE_EXISTS;
}


All right, the code draws the title at (10, 10) and it works fine. I want it centered in my sheet like any title, how can I do that? Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Java printing

#2 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Java printing

Posted 04 June 2007 - 05:23 AM

View Postalpha02, on 3 Jun, 2007 - 03:26 PM, said:

Hi all! I am making a simple printing app in Java, and I want to have the title centered in the page, no matter how long it is. Here is a part of my code for printing:

public int print(Graphics gx, PageFormat pf, int page) throws PrinterException {
	//Translate graphics
	Graphics2D g = (Graphics2D)gx;
	g.translate(pf.getImageableX(), pf.getImageableY());

   //Variables
	int x = 10;
	int y = 10;

	//Rendering
	g.setFont(new Font("Bookman Old Style", Font.BOLD, 18));
	g.drawString(txtTitre.getText(), x, y);
			
	//Page exists
	return PAGE_EXISTS;
}


All right, the code draws the title at (10, 10) and it works fine. I want it centered in my sheet like any title, how can I do that? Thanks.


Try a few of these links: http://www.google.co...G=Google+Search
Was This Post Helpful? 0
  • +
  • -

#3 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Java printing

Posted 04 June 2007 - 01:07 PM

View Postalcdotcom, on 4 Jun, 2007 - 08:23 AM, said:

View Postalpha02, on 3 Jun, 2007 - 03:26 PM, said:

Hi all! I am making a simple printing app in Java, and I want to have the title centered in the page, no matter how long it is. Here is a part of my code for printing:

public int print(Graphics gx, PageFormat pf, int page) throws PrinterException {
	//Translate graphics
	Graphics2D g = (Graphics2D)gx;
	g.translate(pf.getImageableX(), pf.getImageableY());

   //Variables
	int x = 10;
	int y = 10;

	//Rendering
	g.setFont(new Font("Bookman Old Style", Font.BOLD, 18));
	g.drawString(txtTitre.getText(), x, y);
			
	//Page exists
	return PAGE_EXISTS;
}


All right, the code draws the title at (10, 10) and it works fine. I want it centered in my sheet like any title, how can I do that? Thanks.


Try a few of these links: http://www.google.co...G=Google+Search



Graphics2D.getFontMetrics().stringWidth(String str) was the one I was looking for, thanks for the link:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1