English 中文(简体)
印本
原标题:Printing Text to a Page in Java with Graphics2D

这是我现在提出的法典:

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 "
" 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;
}

这条线然后缩小一些空间(不确定使用哪类单位),然后排下下线。 这是行之有效的,但问题是,从横向来看,案文在左边的边 margin上吃。

是否有办法解决这一问题? 我最近刚刚开始用印刷到与 Java的网页上。 我是否应在案文中添加一条新路线,以缩短案文的行文。 我在什么时候加上新的线性?

或者,我只是这样做吗? 可能采取不同的做法?

问题回答

<代码>drawString()方法中的第二个论点控制了该行案文一开始的x-coordination。 它使用的单位是点数,每小时72点。 为使这一条线从纸面左侧走得更远,将纸面的0到更多,或许是36,这将使其从物理页左侧开始半英寸。

另一种选择是使用pf.setImageable(x, y, smallX, smallY),其中x和 y为代表可打印页上左上角坐标的两倍,以及大小 X和大小 Y是可打印页数的两倍。 然后使用<代码>g2d.translate(pf.getImageableX()、pf.getImageableY(),以设定“Gracode>phics2D的原产地。 这种做法可能比较容易。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签