Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Question about Java 2D

  Asked By: Jason    Date: Apr 04    Category: Java    Views: 1505

I just used the Java 2D environment to get a nice printout. My code
looks like this: public class Model implements Printable { private static Font
fnt = new Font("Monospaced",Font.BOLD,12); public int print(Graphics g,
PageFormat pf, int pageIndex)
throws PrinterException {
// pageIndex 0 to 4 corresponds to page numbers 1 to 5.
if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D) g;
g2.drawString("" + (pageIndex+1)+".", 480, 80);
String franPapperet = new String(""+View.papper.getText());
// TextLayout textlayout = new TextLayout(franPapperet, fnt,
//New below AttributedString attributedString= new
int y=90;
int x=90;
TextAttribute.FONT, fnt); // Get iterator for string
AttributedCharacterIterator characterIterator =
// Get font context from graphics
FontRenderContext fontRenderContext =
// Create measurer
LineBreakMeasurer measurer =
new LineBreakMeasurer(characterIterator,
while (measurer.getPosition() <
characterIterator.getEndIndex()) {
// Get line
TextLayout textLayout =
measurer.nextLayout(440); // That is as far as I know the width
// Move down to baseline
y += textLayout.getAscent();
// Draw line
textLayout.draw(g2, x, y);
// Move down to top of next line
y += textLayout.getDescent() +
} return Printable.PAGE_EXISTS;
} The problem is that the LineBreakMeasurer class (or is it partiallly the
AttributedString who is the bad guy) messes everything up. For example: Hi. My
Name is Linnea. SincerelyLinnea becomes: Hi. My Name is Linnea. Sincerely
Linnea. There is no way to keep tabs, spaces (more than one) or "\n" line feeds
in my printout. How do I override this? Or is there another way to print a
simple string?



No Answers Found. Be the First, To Post Answer.

Didn't find what you were looking for? Find more on Question about Java 2D Or get search suggestion and latest updates.