import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.io.*;

public class GradientPaintFill extends Frame
{
   public static void main(String[] args)
   {
      final GradientPaintFill f = new GradientPaintFill();
      f.setSize(1024, 768);
      f.setLocation(0, 0);
      f.addWindowListener(new WindowAdapter()
                            {
                               public void windowClosing(WindowEvent e)
                               {
                                  f.dispose();
                                  System.exit(0);
                               }
                            }
                       );
      f.setVisible(true); 
   }

   public void paint(Graphics g)
   {
      //put image in bi so we can save it to a file
      BufferedImage bi = (BufferedImage) createImage(1024,768);
      Graphics2D big = bi.createGraphics();
      //g2 is the graphics object that we see on the screen
      Graphics2D g2 = (Graphics2D) g;
      Rectangle2D e = new Rectangle2D.Float(0, 0, 1024, 768);
      GradientPaint gp = new GradientPaint(75, 75, new Color(147,112,219), 95, 95, new Color(128, 0, 128), true);
      big.setPaint(gp);
      big.fill(e);
      g2.setPaint(gp);
      g2.fill(e);

      try
      {
          File file = new File(".", "image/purple.jpg");
          FileOutputStream out = new FileOutputStream(file);

            // encode bi as a JPEG data stream          
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
            param.setQuality(1.0f, false);
            encoder.setJPEGEncodeParam(param);
            encoder.encode(bi);

            FileInputStream in = new FileInputStream(file);

            // decodes the JPEG data stream into a BufferedImage
            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
        } 
        catch (Exception ex) 
        {
            g2.setColor(Color.red);
        }

   }
}
Return to list of programs