我正在打印一些我在屏幕上绘制的矩形。矩形重叠,使其看起来像一个由行和列组成的网格。一切都按预期出现在屏幕上。
我尝试了各种打印方式(实现可打印),但我无法获得相同的质量。
- 使用 JComponents 打印方法是颗粒状的(即使缓冲关闭)。
- 直接重绘到打印方法 Graphics 对象会导致矩形重叠处的线条较暗,而矩形重叠处的线条较浅(无论 alpha 复合如何)。我用这种方法尝试了各种 RenderingHints。
- 将构建的缓冲图像直接打印到打印图形可提供一致的质量,但某些线条看起来比其他线条更粗,因此整个列或行将在一侧有一个粗边框。有谁知道为什么会发生这种情况?
@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;
}