Total members 11893 |It is currently Tue Nov 05, 2024 12:28 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





draw Round gradient
Code:
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(5, 5, 150, 150);
    RoundGradientPaint roundGradientPaint = new RoundGradientPaint(
     75, 75, Color.red,new Point2D.Double(0, 85), 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(200, 200);
    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(0, 0);
          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(0, 0, width, height, data);
      return writableRaster;
    }
  }
}





Attachments:
File comment: the output
roundGradient.gif
roundGradient.gif [ 11.18 KiB | Viewed 5236 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : draw Round gradient
 Draw Oval,Arc,Polygon,string,Line,Round and 3D Rectangle     -  
 Gradient Color in java     -  
 Calculating image Gradient     -  
 gradient background color using CSS     -  
 Implementation of FCFS, SJFS, Round Robin and priority algo     -  
 an ammeter ( round gauge ) sortof widget built using Swing     -  
 How to draw a gun     -  
 Draw An Arc in java     -  
 Draw pie chart php     -  
 Draw 3d cube using openGL     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com