使用 Graphics2D 在 Java 中将文本打印到页面
这是我现在的代码:
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException
{
if(pageIndex > 0)
{
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
String lineText;
int lineSpace = 12;
//Draw out each seperate line.
for(int i = 0; i < storyText.length; i++)
{
//I split the text at "\n" and stored the lines in storyText.
lineText = storyText[i];
lineSpace += 15;
graphics.drawString(lineText, 0, lineSpace);
//What about g2d? Could or should I use that instead?
//g2d.drawString(lineText, 0, lineSpace);
}
return PAGE_EXISTS;
}
绘制一条线,然后向下移动一些空格(不确定使用哪种单位),然后绘制下一条线。这是可行的,但问题是,在水平方向上,文本会被左侧的边距吃掉。
有人有解决办法吗?我最近刚刚开始尝试用 Java 打印到页面。我应该在文本中插入换行符以绘制较短的文本行吗?我如何确定何时插入换行符?
或者我只是做错了这一切?也许有不同的方法?
This is the code I have now:
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException
{
if(pageIndex > 0)
{
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
String lineText;
int lineSpace = 12;
//Draw out each seperate line.
for(int i = 0; i < storyText.length; i++)
{
//I split the text at "\n" and stored the lines in storyText.
lineText = storyText[i];
lineSpace += 15;
graphics.drawString(lineText, 0, lineSpace);
//What about g2d? Could or should I use that instead?
//g2d.drawString(lineText, 0, lineSpace);
}
return PAGE_EXISTS;
}
This draws a line, then moves down some spaces (not sure what kind of units are used), then draws the next line. This is works, but the problem is that, horizontally, the text gets eaten by the margin on the left.
Does anyone have a solution to this? I just recently started tinkering with printing to pages with Java. Should I insert a newline character in the text to draw a shorter line of text? How would I figure out when to insert the newline character?
Or am I just doing this all wrong? Maybe a different approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
drawString()
方法中的第二个参数控制该文本行开头的 x 坐标。它使用的单位是点,每英寸 72 点。因此,要使该行从距离纸张左侧更远的地方开始,请将 0 更改为更大的数字,例如 36,这将导致它从距离物理页面左侧半英寸的地方开始。另一种选择是使用 pf.setImageable(x, y, sizeX, sizeY) ,其中 x 和 y 是代表可打印页面左上角坐标的双精度数,sizeX 和 sizeY 是双精度数表示可打印页面的尺寸。然后使用 g2d.translate(pf.getImageableX(), pf.getImageableY()) 将 Graphics2D 对象的原点设置为可打印页面的原点。这种方式可能更容易。
The second argument in the
drawString()
method controls the x-coordinates of the beginning of that line of text. The unit it uses is points, with 72 points per inch. So to make the line start farther away from the left side of the paper, change that 0 to a larger number, perhaps 36, which would cause it to start half an inch from the left side of the physical page.Another option is to use
pf.setImageable(x, y, sizeX, sizeY)
, where x and y are doubles representing the coordinates of the top left corner of the printable page, and sizeX and sizeY are doubles representing the dimensions of the printable page. Then useg2d.translate(pf.getImageableX(), pf.getImageableY())
to set the origin of theGraphics2D
object to the origin of the printable page. This way is probably easier.