Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Round gradient Paint Example 
 

The radial gradient defines a color at a point, and blends into another color. We are providing you an example which displays the radial gradient.

 

Round gradient Paint Example

                         

This section illustrates you how to create round or radial gradient.

The radial gradient defines a color at a point, and blends into another color. We are providing you an example which displays the radial gradient. The RoundGradientPaint  class describes the center of the gradient, radius and background color with the defined points and colors. The RoundGradientPaint class returns a RoundGradient from its createContext() method. The gradient blends color from the center point to the background color.

To translate a pixel value into color components and an alpha component, class ColorModel is used. The class AffineTransform provides transparency, scaling, rotation, shearing.

The getTransparency() method returns the translucent or opaque depending on the color specified that were passed to the constructor of the class RoundGradientPaint. The dispose() method is called when the PageContext is no longer needed. 

To calculate the distance from the center point and iterates over each point in the ellipse, the getRaster() method is used. The class WritableRaster provide pixel writing capabilities.

Here is the code of RoundGradientPaintExample.java

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;

public class RoundGradientPaintExample extends JPanel{
 public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Ellipse2D ellipse = new Ellipse2D.Float(55150150);
    RoundGradientPaint roundGradientPaint = new RoundGradientPaint(
     75
75, Color.red,new Point2D.Double(085), Color.cyan);
    g2d.setPaint(roundGradientPaint);
    g2d.fill(ellipse);
  }
 public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new RoundGradientPaintExample());
    frame.setSize(200200);
    frame.show();
  }
 class RoundGradientPaint implements Paint {
   Point2D point1;
   Point2D point2;
   Color color1, color2;

 public RoundGradientPaint(double x, double y, Color pcolor,
        Point2D radius, Color bcolor) {
      point1 = new Point2D.Double(x, y); 
    color1 = pcolor;
      point2 = radius;
      color2 = bcolor;
    }
 public PaintContext createContext(ColorModel colorModel,
          AffineTransform affineTransform) {

      Point2D transPoint = affineTransform.transform(point1, null);
      Point2D transRadius = affineTransform.deltaTransform(point2, null);
      return new RoundGradient(transPoint, color1,transRadius, color2);
    }
      public int getTransparency() {
      int a = color1.getAlpha();
      int b = color2.getAlpha();
      return (((a & b) == 0xff) ? OPAQUE : TRANSLUCENT);
    }
  }
  public class RoundGradient implements PaintContext {
    Point2D point3, point4;
    Color color3, color4;
      public RoundGradient(Point2D p, Color c1, Point2D r, Color c2) {
      point3 = p;
      color3 = c1;
      point4 = r;
      color4 = c2;
    }
 public void dispose() {}
 public ColorModel getColorModel() {
    return ColorModel.getRGBdefault();
    }
 public Raster getRaster(int p, int q, int width, int height) {
      WritableRaster writableRaster = getColorModel()
       .createCompatibleWritableRaster(width, height);

      int[] data = new int[width * height * 4];
      for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
          double distance = point3.distance(p + i,q + j);
          double radius = point4.distance(00);
          double num = distance / radius;
          if (num > 1.0)
          num = 1.0;
      int value = (j * width + i) * 4;
          data[value + 0] = (int) (color3.getRed() + num
              * (color4.getRed() - color3.getRed()));
          data[value + 1] = (int) (color3.getGreen() + num
              * (color4.getGreen() - color3.getGreen()));
          data[value + 2] = (int) (color3.getBlue() + num
              * (color4.getBlue() - color3.getBlue()));
          data[value + 3] = (int) (color3.getAlpha() + num
              * (color4.getAlpha() - color3.getAlpha()));
        }
      }
      writableRaster.setPixels(00, width, height, data);
      return writableRaster;
    }
  }
}

Output will be displayed as:

Download Source Code

                         

» View all related tutorials
Related Tags: c ide class methods method get icon height vi width change using int id ai create js define show paint

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.