Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8215940

LinearGradientPaint not extending start color when printing on OSX

    Details

    • Subcomponent:
      2d
    • CPU:
      x86
    • OS:
      os_x

      Description

      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


        Attachments

        1. mac.png
          mac.png
          97 kB
        2. TestGradient.java
          2 kB
        3. windows.png
          windows.png
          36 kB

          Activity

            People

            • Assignee:
              psadhukhan Prasanta Sadhukhan
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated: