Java Graphic Printing Uniformity

I print a few rectangles that I draw on the screen. The rectangles overlap so that they look like a grid of rows and columns. On the screen, everything looks as expected.

I tried various printing methods (printing implementation), but I cannot get the same quality.

  • Using the JComponents printing method is grainy (even with buffering).
  • Redrawing directly to the printing method A graphic object causes darker lines where the rectangles overlap, and lighter lines where they do not (regardless of the alpha composite). I tried various rendering methods using this method.
  • Printing the configured buffered image directly on the print schedule provides consistent quality, but some lines appear thicker than others, so that the entire column or row will have one tight border along one side. Does anyone know why this could happen?

@Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if(pageIndex > 2) { return Printable.NO_SUCH_PAGE; } RepaintManager currentManager = RepaintManager.currentManager(this); currentManager.setDoubleBufferingEnabled(false); Graphics2D g2d = (Graphics2D) graphics; g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); g2d.scale(.5, .5); if(pageIndex == 0) { this.paint(g2d); } else if(pageIndex == 1) { g2d.drawImage(onScreenBuffer, null, 0, 0); } else { g2d.setColor(new Color(51, 98, 140)); g2d.setStroke(new BasicStroke(1f)); //GridCell inherits from Rectangle2D.Double for (final GridCell cell : model.getCells()) { g2d.draw(cell); } } return Printable.PAGE_EXISTS; } 
+4
source share
1 answer

I had the same problem. It is impossible to remember all the details, but the main issue is DPI printing. You need to make sure that you print using the highest possible DPI, but not the default DPI.

(By the way, you will need to generate graphics with a large DPI value).

There is javax.print.attribute.standard.PrinterResolution :

The PrinterResolution class is a print attribute class that indicates the exact resolution supported by the printer or print job. This attribute assumes that printers have a small set of resolution devices under which they can operate, rather than a continuum.

PrinterResolution is used in several ways:

  • missed
  • When a client must print a job using the desired client (no more, no less), the client indicates an instance of the PrinterResolution class as an attribute of the print job. This will fail if the print job does not support this exact resolution and Fidelity is set to true.

There are many examples, for example: Printing to a hard printer in java with a resolution of 300 dpi

+1
source

Source: https://habr.com/ru/post/1495763/


All Articles