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

[macos] Printing LinearGradientPaint with transparency does not work on Mac

XMLWordPrintable

    • x86
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      macOS High Sierra 10.13.6

      A DESCRIPTION OF THE PROBLEM :
      If you print a shape with transparent linear gradient on Mac, the colors will look opaque.

      REGRESSION : Last worked in version 11.0.4

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached test case.
      Press the "print" button.
      Save as pdf.
      Open the pdf with any pdf viewer.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The printed shape should be filled with a gradient that uses transparency (as on Windows). See attached pdf file "LinearGradient-Win.pdf".
      ACTUAL -
      The printed shape is filled with a gradient that uses opaque colors. See attached pdf file "LinearGradient-Mac.pdf".

      ---------- BEGIN SOURCE ----------
      import javax.swing.JButton;
      import javax.swing.JComponent;
      import javax.swing.JFrame;
      import javax.swing.JOptionPane;
      import javax.swing.JPanel;
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.EventQueue;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.LinearGradientPaint;
      import java.awt.geom.Path2D;
      import java.awt.geom.Rectangle2D;
      import java.awt.print.PageFormat;
      import java.awt.print.Printable;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;

      public class PrintTest
      {
        public static void main(String[] args)
        {
          EventQueue.invokeLater(() -> {
            JFrame frame = new PrintTestFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
          });
        }
      }

      /**
       * This frame shows a panel with 2D graphics and buttons to print the graphics and to set up the
       * page format.
       */
      class PrintTestFrame extends JFrame {
        private PrintComponent canvas;
        
        PrintTestFrame() {
          setTitle("PrintTest");
          setSize(300, 300);

          canvas = new PrintComponent();
          add(canvas, BorderLayout.CENTER);

          JPanel buttonPanel = new JPanel();
          JButton printButton = new JButton("Print");
          buttonPanel.add(printButton);
          printButton.addActionListener(event -> {
            try {
              PrinterJob job = PrinterJob.getPrinterJob();
              job.setPrintable(canvas);
              if (job.printDialog()) job.print();
            } catch (PrinterException e) {
              JOptionPane.showMessageDialog(PrintTestFrame.this, e);
            }
          });

          add(buttonPanel, BorderLayout.NORTH);
        }
      }

      /**
       * This component generates a 2D graphics image for screen display and printing.
       */
      class PrintComponent extends JComponent implements Printable {
        public void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          drawPage(g2);
        }

        public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
          if (page >= 1) return Printable.NO_SUCH_PAGE;
          Graphics2D g2 = (Graphics2D) g;
          g2.translate(pf.getImageableX(), pf.getImageableY());

          drawPage(g2);
          return Printable.PAGE_EXISTS;
        }

        /**
         * This method draws the page both on the screen and the printer graphics context.
         * @param g2 the graphics context
         */
        private void drawPage(Graphics2D g2) {
          double x = 0;
          double y = 0;
          double w = 100;
          double h = 100;

          Rectangle2D.Double rect = new Rectangle2D.Double(x, y, w, h);
          g2.setPaint(Color.ORANGE);
          g2.fill(rect);

          Path2D path = new Path2D.Double();
          path.moveTo(x, y);
          path.lineTo(x + w, y);
          path.lineTo(x, y + h);
          path.closePath();

          float[] dist = {0f, 0.8f};
          Color[] colors = {new Color(255, 255, 255, 200), new Color(255, 255, 255, 20)};
          LinearGradientPaint gradient = new LinearGradientPaint(0, 0, 50, 50, dist, colors);

          g2.setPaint(gradient);
          g2.fill(path);
        }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: