Warm tip: This article is reproduced from serverfault.com, please click

String.format() not working correctly with Printable (Using Graphics2D)

发布于 2020-11-18 03:23:43

I am trying to write some code that will print some numbers right-aligned but have noticed that when there is more than one digit, the formatting gets messed up and throws everything off. I also noticed that the Strings I am trying to print are not aligning correctly either.

Here is my code:

public class Printer implements Printable
{
    Object[][] data;
    String lines;
    
    public Printer(Object[][] data)
    {
        this.data = data;
        lines = convertArrayToString();
        
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        Paper paper = new Paper();
        paper.setImageableArea(9, 9, paper.getWidth() - (9 * 2), paper
                .getHeight());
        PrintRequestAttributeSet attributes = 
                new HashPrintRequestAttributeSet();
        attributes.add(OrientationRequested.LANDSCAPE);
        job.setPrintable(this, pf);
        boolean ok = job.printDialog();
        if (ok)
        {
            try
            {
                pf.setOrientation(PageFormat.LANDSCAPE);
                job.print(attributes);
            } catch (PrinterException ex)
            {
                /* The job did not successfully complete */
            }
        }
    }

    @Override
    public int print(Graphics g, PageFormat pf, int page) 
            throws PrinterException {
        if (page > 0)
            return NO_SUCH_PAGE;
        
        Graphics2D g2d = (Graphics2D) g;
        g2d.translate(pf.getImageableY(), pf.getImageableX());
        g.setFont(new Font("ARIAL", Font.PLAIN, 11));
        drawString(g, lines, 10, 10);
        
        return PAGE_EXISTS;
    }
    
    private void drawString(Graphics g, String message, int x, int y)
    {
        for (String line : message.split("\n"))
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
    }

    private String convertArrayToString() {
        String line =  "ID |   REVERB |     GAIN |     PRESENCE |     MIDDLE | "
                + "   BASS |           ARTIST NAME |           AMPLIFIER NAME "
                + "|\n" 
                + "------------------------------------------------------------"
                + "------------"
                + "------------------------------------------------------------"
                + "-------------------------\n";
        
        for (int i = 0; i < data.length; i++)
        {
            String currentLine = "";
            
            currentLine = String.format(" %2d |              %2d |           "
                    + "%2d |                     %2d |                %2d |"
                    + "           %2d |%30s |%30s |\n", data[i][0], data[i][1], 
                    data[i][2], data[i][3], data[i][4], data[i][5], data[i][6], 
                    data[i][7]);
            line += currentLine;
        }
        
        return line;
    }  
}

And here is what the code prints: Example

If anyone has any idea as to why using the String.format() method only works sometimes, any input would be appreciated! Thank you!

Questioner
Jared
Viewed
0
Jared 2020-11-29 06:54:20

The fix was to change the Font object to new Font(Font.MONOSPACED, Font.PLAIN, 11), as VGR suggested above.