Skip to content

IllegalArgumentException while printing #443

@scrawny1302

Description

@scrawny1302

I have to print the chart with additional text and logos. For this I use a Printable class to override the print method. My preview dialog uses the same method to add the additional text. And that works. But when I print the chart in Landscape orientation a IllegalArgumentExceprion will be thrown:

java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0

	at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
	at java.desktop/java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:184)
	at org.jfree.chart.ChartPanel.paintChartToBuffer(ChartPanel.java:1457)

The problem is the Transform in PeekGraphics which will be constructed in RasterPrintJob.printPage(...). The values there are:
AffineTransform[[0.0, 8.333333333333334, 0.0], [-8.333333333333334, 0.0, 6600.000000000001]]

In case of this the getScaleX and getScaleY in ChartPanel.paintComponent is 0.0 .

My workaround is:

new ChartPanel(
		defaultChart,
	            20,
	            10,
	            20,
	            10,
	            3000,
	            2000,
	            true,
	            true,
	            true,
	            true,
	            true,
	            true
){
@Override
			protected BufferedImage paintChartToBuffer(Graphics2D g2, Dimension bufferSize, Dimension chartSize, Point2D anchor, ChartRenderingInfo info) {
				if(bufferSize.getWidth() == 0.0){

					Dimension size = this.getSize();
					Insets insets = this.getInsets();
					int availableWidth = size.width - insets.left - insets.right;
					int availableHeight = size.height - insets.top - insets.bottom;
					AffineTransform globalTransform = g2.getTransform();
					double globalScaleX = globalTransform.getShearX();
					double globalScaleY = globalTransform.getShearY();
					bufferSize = new Dimension(Math.abs((int)Math.ceil(availableWidth * globalScaleX)), Math.abs((int)Math.ceil(availableHeight * globalScaleY)));
				}
				return super.paintChartToBuffer(g2, bufferSize, chartSize, anchor, info);
			}
		};

Is there a better way to fix this or should it fixed here?

See the TestClass to reproduce. First test with direct set of the Transform parameters and second Test with the PrinterJob usage.

DefaultChartPanelTest.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions