Java – print to physical form – requires basic understanding
I'm not sure I understand g.drawstring
I have a program for writing pre - printed forms Users claim that printing is irregular... That is, the text on the form is higher / lower than the previous printing Personally, I think they filled out the form incorrectly, but because they paid me to write code, I'm measuring the form and converting the size to pixels and rewriting the printed part@ H_ 404_ 3@
To print the form correctly, c.getcostamount() must print a pixel above c.getappraisersamount() so that it displays a line below it However, each subsequent line is less than 4 mm (or about 15 pixels)@ H_ 404_ 3@
My problem is that I don't understand the vertical distance and why row 3 must be placed above the previous row to make it lower@ H_ 404_ 3@
Does anyone have a quick and simple explanation or a link to the tutorial / explanation@ H_ 404_ 3@
Thank you very much@ H_ 404_ 3@
Code (H / T Alex, Java: printing program output to a physical printer): @ h_ 404_ 3@
public int print(Graphics g,PageFormat pf,int page,Check c){ final double MILLIMETER_IN_PIXELS = 3.779527559; DecimalFormat df = new DecimalFormat("$#.00"); if (page > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D) g; int x = (int) pf.getImageableX(); int y = (int) pf.getImageableY(); g2d.translate(x,y + .5); Font font = new Font("Courier New",Font.PLAIN,10); g2d.setFont(font); FontMetrics metrics = g.getFontMetrics(font); g.drawString("CHECK #" + c.getCheckNumber(),((int) MILLIMETER_IN_PIXELS* 55),((int) MILLIMETER_IN_PIXELS*15)); int strWidth = SwingUtilities.computeStringWidth(metrics,df.format(c.getAppraisersAmount())); g.drawString(df.format(c.getAppraisersAmount()),((int) ((MILLIMETER_IN_PIXELS*62)-strWidth)),((int) MILLIMETER_IN_PIXELS*23)); Date d = c.getJavaDate(); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); g.drawString(sdf.format(d),((int) MILLIMETER_IN_PIXELS*90),((int) MILLIMETER_IN_PIXELS*24)); strWidth = SwingUtilities.computeStringWidth(metrics,df.format(c.getCostAmount())); g.drawString(df.format(c.getCostAmount()),((int) (MILLIMETER_IN_PIXELS*22))); strWidth = SwingUtilities.computeStringWidth(metrics,df.format(c.getRefundsAmount())); g.drawString(df.format(c.getRefundsAmount()),((int) (MILLIMETER_IN_PIXELS*26))); strWidth = SwingUtilities.computeStringWidth(metrics,df.format(c.getOfficersAmount())); g.drawString(df.format(c.getOfficersAmount()),((int) (MILLIMETER_IN_PIXELS*30))); Double totalLeft = c.getAppraisersAmount() + c.getCostAmount() + c.getRefundsAmount() + c.getOfficersAmount(); strWidth = SwingUtilities.computeStringWidth(metrics,df.format(totalLeft)); g.drawString(df.format(totalLeft),((int) (MILLIMETER_IN_PIXELS*44))); return PAGE_EXISTS; }
Solution
I'm not sure how you identified millimeter_ IN_ Pixels value, which is completely suspicious to me
However, the only error that puzzles you is the lack of braces: @ h_ 404_ 3@
You can use the following terms: ((int) millimeter_ IN_ PIXELS * 23),((int)MILLIMETER_ IN_ Pixels * 24) and ((int) (miller_in_pixels * 22))@ H_ 404_ 3@
Note that the first two terms lack the product millimeter_ IN_ Curly braces around pixels * means that you will millimeter first_ IN_ Pixels is converted to int to get the value 3 and then multiplied, instead of double multiplying and then converting to int. @ H_ 404_ 3@
Therefore, the rounding error is different The results were 69,72,83@ H_ 404_ 3@
Note that missing parentheses also occur elsewhere and the horizontal position will be affected@ H_ 404_ 3@
As an appendix, here's how you get the real DPI: @ h_ 404_ 3@
Point2D p = g2d.getDeviceConfiguration().getDefaultTransform() .transform(new Point2D.Float(72,72),null); // normally,both are the same int horizontalDPI=(int)p.getX(),verticalDPI=(int)p.getY();
Note: the default conversion is specified to convert 72 user space pixels to 1 inch, so when the conversion is applied to 72, the number of device pixels will match 1 inch@ H_ 404_ 3@