这样写也不行!
public int print(Graphics g,PageFormat pf,int page)throws PrinterException
{
Graphics2D g2=(Graphics2D)g;
g2.setPaint(Color.black);
pf.setOrientation(PageFormat.LANDSCAPE);
g2.translate(pf.getImageableX(),pf.getImageableY());
// if (str.equals("Preview"))drawCropMarks(g2,pf);
g2.clip(new Rectangle2D.Double(0,0,pf.getImageableWidth(),pf.getImageableHeight()));
g2.translate(0,-page*pf.getImageableHeight());
// int pages=getPageCount(g2,pf);
for (int i=0;i<=17;i++) drawString(g,item,setLocation,width);
return Printable.PAGE_EXISTS;
}
public void drawString(Graphics g,String st,Point p,double width)
{ wid=width;
int POINTS_PER_INCH = 72;
Graphics2D g2d = (Graphics2D) g;
TextLayout layout;
AttributedString paragraphText = new AttributedString (st);
Point2D.Double pen = new Point2D.Double (0.25 * POINTS_PER_INCH, 0.25 * POINTS_PER_INCH);
//--- Translate the origin to 0,0 for the top left corner
//--- Set the font for this text
paragraphText.addAttribute (TextAttribute.FONT, new Font ("serif", Font.PLAIN, 10));
LineBreakMeasurer lineBreaker = new LineBreakMeasurer (paragraphText.getIterator(),
new FontRenderContext (null, true, true));
//--- LineBreakMeasurer will wrap each line to correct length and
//--- return it as a TextLayout object
int first = 0;
while ((layout = lineBreaker.nextLayout ((float) wid)) != null)
{ //--- Align the Y pen to the ascend of the font, remember that
//--- the ascend is origin (0, 0) of a font. Refer to figure 1
if ( first == 0 )
{ pen.y += layout.getAscent () + (float)p.y;
pen.x += layout.getAscent () + (float)p.x;
first = -1;
}
else
{ pen.y += layout.getAscent ();
//pen.x += layout.getAscent () + (float)p.x;
}
//--- Draw the line of text
layout.draw (g2d, (float) pen.x, (float) pen.y);
//--- Move the pen to the next position adding the descent and
//--- the leading of the font
pen.y += layout.getDescent () + layout.getLeading ();
// g.setFont(oldFont);
}
}
}