
/*
 * @(#) DrawingCanvas.java 1.1 99/10/11
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package app;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

import java.net.*;
import java.util.*;
import java.beans.*;
import javax.swing.*;
import java.awt.print.*;
import java.awt.geom.*;
public class DrawGradientPaint extends JComponent {
   protected BufferedImage offScrIm;
   protected Graphics2D offScrGc;

   public DrawGradientPaint() {
      this.setBackground(Color.black);
      offScrIm = new BufferedImage(100,100, BufferedImage.TYPE_INT_RGB);
      offScrGc = offScrIm.createGraphics();
      setSize(300, 140);
   }

   protected void paintComponent(Graphics g){
      Graphics2D g2d = (Graphics2D)g;

      //Set the background color
      g2d.setColor(Color.black);
      Rectangle rect = this.getBounds();
      g2d.fillRect(0,0,rect.width, rect.height);
      //Cyclic paint example
      int x =20; int y =20;
      int wid = 100; int ht =100;
      GradientPaint gp = new GradientPaint( x+20.0f, y+20.0f, Color.red, x+60.0f, y+20.0f, Color.green);
      g2d.setPaint(gp);
      g2d.fillRect(x,y, wid,ht);
      //Acyclic paint example
      x= 140; y= 20;
      gp = new GradientPaint( x+20.0f, y+20.0f, Color.red, x+60.0f, y+20.0f, Color.green, true);
      g2d.setPaint(gp);
      g2d.fillRect(x,y, wid,ht);
      x= 20;
      //Drawing text example.
      g2d.setColor(Color.blue);
      String str = "Cyclic";
      g2d.drawString(str,
                     x+ (wid-g2d.getFontMetrics().stringWidth(str))/2,
                     y+ht+20);
      str = "Acyclic";
      g2d.drawString(str,
                     x+wid+20+(wid-g2d.getFontMetrics().stringWidth(str))/2,
                     y+ht+20);

   }

   public static void  main(String[] args){
      DrawGradientPaint cv = new DrawGradientPaint();
      JFrame fr = new JFrame();
      fr.addWindowListener(
            new WindowAdapter(){
               public void windowClosing(WindowEvent e){
                  System.exit(0);
               }
             }
      );
      fr.setTitle("Gradient Paint");
      fr.getContentPane().add(cv);
      fr.pack();
      fr.setSize(260,200);
      fr.show();
      cv.repaint();
    }
 }
