formatting in java

Hi,

I will get some data from a source.

Now what i want is to format this and make a document, programatically.

The format will be given.

Using Graphics2D class i think i can give only one format.

but what shld i do if i need to give different font size, colors, styles to different lines of the same document.

the code which i used with Graphics2D is

Graphics2D g2d = (Graphics2D) g;

// Translate the origin to 0,0 for the top left corner

g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());

// Set the default drawing color to black

g2d.setPaint (Color.black);

// Draw a border around the page

Rectangle2D.Double border =new Rectangle2D.Double (0,

0,

pageFormat.getImageableWidth (),

pageFormat.getImageableHeight ());

g2d.draw (border);

// Print the title

String titleText ="Printing in Java Part 2";

Font titleFont =new Font ("helvetica", Font.BOLD, 36);

g2d.setFont (titleFont);

// Compute the horizontal center of the page

FontMetrics fontMetrics = g2d.getFontMetrics ();

double titleX = (pageFormat.getImageableWidth () / 2) - (fontMetrics.stringWidth (titleText) / 2);

double titleY = 3 * POINTS_PER_INCH;

g2d.drawString (titleText, (int) titleX, (int) titleY);

[1769 byte] By [xemaa] at [2007-10-3 3:00:54]
# 1
Don't re-invent the wheel. http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html
CeciNEstPasUnProgrammeura at 2007-7-14 20:50:33 > top of Java-index,Java Essentials,Java Programming...
# 2

i don't want to display it some where.

what i want is to send this formated document to a printer.

see what i've done in continuation to the above code.

my inner class.

private class IntroPage implements Printable {

/**

* Method: print

*

* @param g a value of type Graphics

* @param pageFormat a value of type PageFormat

* @param page a value of type int

* @return a value of type int

*/

public int print (Graphics g, PageFormat pageFormat, int page) {

// Create the Graphics2D object

Graphics2D g2d = (Graphics2D) g;

// Translate the origin to 0,0 for the top left corner

g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());

// Set the default drawing color to black

g2d.setPaint (Color.black);

// Draw a border around the page

Rectangle2D.Double border = new Rectangle2D.Double (0,

0,

pageFormat.getImageableWidth (),

pageFormat.getImageableHeight ());

g2d.draw (border);

// Print the title

String titleText = "Printing in Java Part 2";

Font titleFont = new Font ("helvetica", Font.BOLD, 36);

g2d.setFont (titleFont);

// Compute the horizontal center of the page

FontMetrics fontMetrics = g2d.getFontMetrics ();

double titleX = (pageFormat.getImageableWidth () / 2) - (fontMetrics.stringWidth (titleText) / 2);

double titleY = 3 * POINTS_PER_INCH;

g2d.drawString (titleText, (int) titleX, (int) titleY);

return (PAGE_EXISTS);

}

}

This class is called like this

Book book = new Book();

book.append(new IntroPage(), printerJob.defaultPage());

printerJob.setPageable(book);

printerJob.print();

xemaa at 2007-7-14 20:50:33 > top of Java-index,Java Essentials,Java Programming...
# 3
1) But a JTextArea has a print() method2) StyledDocument was created for styled documents3) How are you passing your different styles to your method anyway? What does your input look like?
CeciNEstPasUnProgrammeura at 2007-7-14 20:50:33 > top of Java-index,Java Essentials,Java Programming...
# 4

My input will be like this for example

<Company Name>Date:19/8/2006

Name: <Name>

Age :24

Job Title: Software Engineer

This will be sent as a plain string.

i need to format it like for example

<Company Name>Date:19/8/2006 ->this should be bold.

The Fields like Name, Age and JobTitle should be bold and italics.

So how can i dothat and..

this formatted will go to a printer and get printed.

xemaa at 2007-7-14 20:50:33 > top of Java-index,Java Essentials,Java Programming...
# 5
isn't there anyone to comment..?
xemaa at 2007-7-14 20:50:33 > top of Java-index,Java Essentials,Java Programming...