title: How to make smoother gradient background colors
body: <p>Using GradientPaint for gradient background colors is not always satisfactory, especially in certain sizes. For example this code:</p>
  
  <pre><code>public class TestPanel extends JPanel {
  
      protected void paintComponent( Graphics g ) {
          Graphics2D g2d = (Graphics2D) g;
          int w = getWidth();
          int h = getHeight();
          Color color1 = Color.BLACK;
          Color color2 = Color.GRAY;
          GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
          g2d.setPaint(gp);
          g2d.fillRect(0, 0, w, h);
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  JFrame frame = new JFrame();
                  TestPanel panel = new TestPanel();
                  frame.add(panel);
                  frame.setSize(200,200);
                  frame.setLocationRelativeTo(null);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setVisible(true);
              }
          });
      }
  }
  </code></pre>
  
  <p>produces the following:</p>
  
  <p><img src="https://i.stack.imgur.com/kTv6M.jpg" alt="non-cyclic"></p>
  
  <p>The cyclic version looks even worse than that:</p>
  
  <p><img src="https://i.stack.imgur.com/0jajh.jpg" alt="cyclic"></p>
  
  <p>How can I make the gradient look smoother (in both cases)?</p>
  
  <p>EDIT:</p>
  
  <p>It seems that it is (at least partially) a monitor problem. The gradient colors look awful on my netbook (1024 x 600, True Color 32-bit) while they look a lot better on my desktop pc (1280 x 1024, True Color 32-bit). But the results are still not so smooth even with the desktop's monitor.</p>
  
  <p>Both are using Java Version 6 Update 33.</p>
  
  <p>Does that mean that an application should only use gradient backgrounds when it is viewed with higher resolutions?</p>
  
  <p>EDIT 2:</p>
  
  <p>Anyway, for those facing simlar problem or are just interested in this, I think that the only solution for a gradient color to look smoother is just higher resolution (assuming that the monitor is already set to true color of course) - which is not really a solution. Like I said in a comment, I thought that a 1024 x 600 resolution would be sufficient for a simple black-to-gray gradient color but it seems that I was wrong. When the same code is run on a computer with a monitor that supports higher resolution the gradient looks better, like through my desktop's monitor, 1280 x 1024. Unfortunately I dont have an option for better resolution but I believe it would look even smoother. I also noticed that the two images that I uploaded (taken from my netbook) when they are viewed through a better monitor these same images look smoother... so it must be just the resolution.</p>
  
  <p>Since there is no solution I think that the only way to use specific gradient steps that would always look smooth (like black-to-gray, which even that seems to look bad in lower resolutions) is to have the gui program test for resolution on start-up and make the choice to show the appropriate gradient but I'm not sure if it is worth it. And using less gradient steps is just a compromise.</p>
  
  <p>Due to lack of more/better responses, I've accepted the use of pre-dithered images as an answer.</p>