import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.ColorSpaceType;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class GradTest {
  private static final Color[] colors = {
      new Color(255, 0, 0),
      new Color(0, 255, 0),
      new Color(0, 0, 255)
  };

  private static float[] fractions = {
      0.0f, 0.7f, 1.0f
  }; 

  private static final int TESTW = 600;
  private static final int TESTH = 500;
  private static final BufferedImage bi =
      new BufferedImage(TESTW, TESTH, BufferedImage.TYPE_INT_ARGB);

  public static void main(String [] args) {
    JFrame f = new JFrame();
    SwingUtilities.invokeLater(()->{
      JPanel jp = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;

          int startX   = 0;
          int startY   = TESTH/2;
          int endX     = TESTW;
          int endY     = TESTH/2;

          Paint paint =
              new LinearGradientPaint(new Point2D.Float(startX, startY),
                  new Point2D.Float(endX, endY),
                  fractions, colors,
                  CycleMethod.NO_CYCLE, ColorSpaceType.SRGB,
                  new AffineTransform());

          g2.setPaint(g2.getBackground());
          g2.fillRect(0, 0, TESTW, TESTH);

          Graphics2D big = bi.createGraphics();
          big.setPaint(g2.getBackground());
          big.fillRect(0, 0, TESTW, TESTH);

          big.setPaint(paint);
          g2.setPaint(paint);
          big.fillOval(0, 0, TESTW, TESTH - f.getInsets().top);
          g2.setPaint(paint);
          g.fillOval(0, 0, TESTW, TESTH - f.getInsets().top);
          g2.drawImage(bi, 0, TESTH - f.getInsets().top, null);
        }
      };

      f.add(jp);
      f.setPreferredSize(new Dimension(TESTW, TESTH*2));
      f.pack();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.setVisible(true);
    });
  }
}
