ADDITIONAL SYSTEM INFORMATION :
The issue is specific to OSX. I can confirm that in Windows the shape is correctly printed.
A DESCRIPTION OF THE PROBLEM :
From the javadoc on LinearGradientPaint: in the event that the user does not set the first keyframe value equal to 0 and/or the last keyframe value equal to 1, keyframes will be created at these positions and the first and last colors will be replicated there. When a nonzero start keyframe is provided and cycle = NO_CYCLE, the start keyframe is rendered correctly on screen but not when printing.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Fill a shape with a solid color. Then set the paint to a LinearGradientPaint with the first keyframe = non zero, and fill again. The first keyframe is not extended to 0, revealing the solid color underneath, but only when printing. It is rendered correctly to the screen.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
It is expected that the printed result will look like the screen.
ACTUAL -
The printed result is different that the screen rendering.
---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.print.*;
import javax.swing.*;
public class TestGradient {
public static class TwoRectangles extends JComponent {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.RED);
Rectangle r = new Rectangle(0, 75, 500, 150);
g2d.fill(r); //paint a red opaque rectangle
LinearGradientPaint p = new LinearGradientPaint(100, 100, 400, 100, new float[]{0.0f, 1.0f}, new Color[]{Color.GREEN, Color.BLUE}, MultipleGradientPaint.CycleMethod.NO_CYCLE);
g2d.setPaint(p);
g2d.fill(r); //paint a green to blue gradient
}
}
public static final TwoRectangles rect = new TwoRectangles();
public static void main(String[] args) throws PrinterException {
JFrame f = new JFrame("Test Gradient");
f.setLayout(new BorderLayout());
f.add(rect);
f.setSize(600, 300);
f.setVisible(true);
PrinterJob printJob = PrinterJob.getPrinterJob();
Printable prt = new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0)
return(NO_SUCH_PAGE);
rect.print(graphics);
return PAGE_EXISTS;
}
};
printJob.setPrintable(prt);
if (printJob.printDialog())
printJob.print();
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Manually add a keyframe at zero with the color equal to the color of the previous first keyframe.
FREQUENCY : always