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

All existing gradient paint implementations have issues with coordinates/sizes larger than Short.MAX_VALUE (exactly) on any Linux systems

XMLWordPrintable

    • 2d
    • b140
    • x86_64
    • linux

      FULL PRODUCT VERSION :
      java version "1.8.0_102"
      Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux dev4 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      A standard installation of Ubuntu 16.04
      Additionally using IntelliJ Idea IDE 13.1.6 with JDK 1.8.0u102

      A DESCRIPTION OF THE PROBLEM :
      All existing gradient paint implementations have issues with coordinates/sizes larger than Short.MAX_VALUE (exactly) on any Linux systems. I've got reports from different Ubuntu and Fedora versions.

      If larger value are used - gradient will cause an unexpected behavior. Sometimes it might simply disappear, sometimes it might be moved, sometimes it might glitch the graphics.

      Here is a short example of a gradient that doesn't paint properly:

      g2d.setPaint ( new GradientPaint ( 0, Short.MAX_VALUE + 100, Color.RED, 0, Short.MAX_VALUE + 200, Color.BLACK ) );
      g2d.fillRect ( 0, Short.MAX_VALUE + 100, getWidth (), 100 );

      You can try painting this on a JComponent with size larger than Short.MAX_VALUE and will see an issue on Linux systems under JDK 8.

      REGRESSION. Last worked in version 7u79

      ADDITIONAL REGRESSION INFORMATION:
      Issue doesn't appear under JDK 6 (tried u30 and u45) or JDK 7 (tried u79), but it seems that it can be reproduced in 100% of cases under any JDK 8 version (tried u11, u45, u77, u91, u102).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Get a Linux system, for example Ubuntu
      2. Get a JDK 8, for example u102
      3. Get an IDE, for example IntelliJ Idea (open-source version is fine)
      4. Launch the code example provided along with this ticket in IDE
      5. See nothing in the scroll OR see some visual glitches in the place where gradient should be

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Gradient should be painted without any issues even with coordinates and/or sizes larger than Short.MAX_VALUE.
      ACTUAL -
      Gradient is not painted and/or causes various visual glitches.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No errors appear in the log

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class GradientExample
      {
          public static void main ( final String[] args )
          {
              SwingUtils.invokeLater ( new Runnable ()
              {
                  @Override
                  public void run ()
                  {
                      final JComponent c = new JComponent ()
                      {
                          @Override
                          protected void paintComponent ( final Graphics g )
                          {
                              super.paintComponent ( g );

                              final Graphics2D g2d = ( Graphics2D ) g;
                              g2d.setPaint ( new GradientPaint ( 0, Short.MAX_VALUE + 100, Color.RED, 0, Short.MAX_VALUE + 200, Color.BLACK ) );
                              g2d.fillRect ( 0, Short.MAX_VALUE + 100, getWidth (), 100 );
                          }

                          @Override
                          public Dimension getPreferredSize ()
                          {
                              return new Dimension ( 1, Short.MAX_VALUE * 2 );
                          }
                      };

                      final JScrollPane scroll = new JScrollPane ( c );
                      scroll.getVerticalScrollBar ().setUnitIncrement ( 10 );
                      scroll.setPreferredSize ( new Dimension ( 500, 600 ) );

                      final JFrame frame = new JFrame ( "Gradient example" );
                      frame.add ( scroll );
                      frame.setDefaultCloseOperation ( WindowConstants.EXIT_ON_CLOSE );
                      frame.pack ();
                      frame.setLocationRelativeTo ( null );
                      frame.setVisible ( true );

                      c.scrollRectToVisible ( new Rectangle ( 0, Short.MAX_VALUE - 100, 500, 550 ) );
                  }
              } );
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Issue can be avoided by translating graphics context to avoid large values in gradient coordinates:

      g2d.translate ( 0, Short.MAX_VALUE + 100 );
      g2d.setPaint ( new GradientPaint ( 0, 0, Color.RED, 0, 100, Color.BLACK ) );
      g2d.fillRect ( 0, 0, getWidth (), 100 );

      This works, but it is really hard and frustrating thing to change in a large project that is already based on coordinates.

            ceisserer Clemens Eisserer
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: