title: Java 3 Color Gradient
body: <p>I have a <code>JPanel</code>, and I would like to paint a gradient within it. I have the code below, but that only paints a 2 color gradient. I would like to add a 3rd but don't know how. What I want is to have the top left of the panel as white, top right red, and both bottom corners black. What would I have to do to achieve this, something that looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/1rvz1.png" alt="Example Gradient"></p>
  
  <pre><code>package pocketshop.util;
  
  import java.awt.Color;
  import java.awt.GradientPaint;
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  import javax.swing.JPanel;
  
  public class ColorPicker extends JPanel{
  
      public ColorPicker(){
          repaint();
      }
  
      @Override
      public void paintComponent(Graphics g){
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D)g;
          int w = getWidth();
          int h = getHeight();
  
          GradientPaint gp = new GradientPaint(
                  0, 0, Color.white,
                  0, h, Color.black);
  
          g2d.setPaint(gp);
          g2d.fillRect(0, 0, w, h);
      }
  }
  </code></pre>
  
  <p><br><br>
  <strong>Edit: Possible solution</strong></p>
  
  <p>I was able to come up with using 2 gradients one horizontal and one vertical, like this:</p>
  
  <pre><code>    public void paintComponent(Graphics g){
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D)g;
          int w = getWidth();
          int h = getHeight();
  
          // Vertical
          GradientPaint gp = new GradientPaint(
                  0, 0, new Color(0,0,0,0),
                  0, h, Color.black);
  
          // Horizontal
          GradientPaint gp2 = new GradientPaint(
                  0, 0, Color.white,
                  w, 0, Color.red, true);
  
          g2d.setPaint(gp2);
          g2d.fillRect(0, 0, w, h);
          g2d.setPaint(gp);
          g2d.fillRect(0, 0, w, h);
      }
  </code></pre>