下面列出了怎么用java.awt.RadialGradientPaint的API类实例代码及写法,或者点击链接到github查看源代码。
private static BufferedImage createTestImage() {
int w = 1024;
int h = 768;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createSrcImage() {
BufferedImage img = createImage();
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createSrcImage() {
BufferedImage img = createImage();
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createTestImage() {
int w = 1024;
int h = 768;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createTestImage() {
int w = 1024;
int h = 768;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createSrcImage() {
BufferedImage img = createImage();
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createSrcImage() {
BufferedImage img = createImage();
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createSrcImage() {
BufferedImage img = createImage();
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createTestImage() {
int w = 1024;
int h = 768;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createTestImage() {
int w = 1024;
int h = 768;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createTestImage() {
int w = 1024;
int h = 768;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createSrcImage() {
BufferedImage img = createImage();
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createTestImage() {
int w = 1024;
int h = 768;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createSrcImage() {
BufferedImage img = createImage();
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createSrcImage() {
BufferedImage img = createImage();
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static BufferedImage createTestImage() {
int w = 1024;
int h = 768;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
return img;
}
private static void createTestFile() {
int w = 1280;
int h = 1024;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
try {
System.out.println("Create test image " + file.getAbsolutePath());
boolean b = ImageIO.write(img, "JPEG", file);
if (!b) {
throw new RuntimeException("Failed to create test image.");
}
} catch (IOException e) {
throw new RuntimeException("Test failed", e);
}
}
private RadialGradientPaint makeRadial(int numColors, boolean alpha) {
float interval = 1.0f / (numColors - 1);
float[] fractions = new float[numColors];
for (int i = 0; i < fractions.length; i++) {
fractions[i] = i * interval;
}
Color[] colors = makeGradientColors(numColors, alpha);
return new RadialGradientPaint(0.0f, 0.0f, 10.0f,
fractions, colors,
CycleMethod.REFLECT);
}
private Paint createPaint(PaintType type, int startx, int starty,
int w, int h)
{
// make sure that the blue color doesn't show up when filling a
// w by h rect
w++; h++;
int endx = startx + w;
int endy = starty + h;
Rectangle2D.Float r = new Rectangle2D.Float(startx, starty, w, h);
switch (type) {
case COLOR: return Color.red;
case GRADIENT: return
new GradientPaint(startx, starty, Color.red,
endx, endy, Color.green);
case LINEAR_GRADIENT: return
new LinearGradientPaint(startx, starty, endx, endy,
new float[] { 0.0f, 0.999f, 1.0f },
new Color[] { Color.red, Color.green, Color.blue });
case RADIAL_GRADIENT: return
new RadialGradientPaint(startx, starty,
(float)Math.sqrt(w * w + h * h),
new float[] { 0.0f, 0.999f, 1.0f },
new Color[] { Color.red, Color.green, Color.blue },
CycleMethod.NO_CYCLE);
case TEXTURE: {
BufferedImage bi =
new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setPaint(createPaint(PaintType.LINEAR_GRADIENT, 0, 0, w, h));
g.fillRect(0, 0, w, h);
return new TexturePaint(bi, r);
}
}
return Color.green;
}
/**
* Returns an SVG color string based on the current paint. To handle
* {@code GradientPaint} we rely on the {@code setPaint()} method
* having set the {@code gradientPaintRef} attribute.
*
* @return An SVG color string.
*/
private String svgColorStr() {
String result = "black;";
if (this.paint instanceof Color) {
return rgbColorStr((Color) this.paint);
} else if (this.paint instanceof GradientPaint
|| this.paint instanceof LinearGradientPaint
|| this.paint instanceof RadialGradientPaint) {
return "url(#" + this.gradientPaintRef + ")";
}
return result;
}
protected RadialGradientPaint getAnotherObject() {
return null; /* TODO: could not update property
float[] f = { 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f };
Color[] c = { Color.RED, Color.GREEN, Color.BLUE, Color.RED, Color.GREEN, Color.BLUE };
return new RadialGradientPaint(
new Point2D.Float(f[0], f[1]), 100.0f,
new Point2D.Float(f[2], f[3]),
f, c, REFLECT, LINEAR_RGB,
new AffineTransform(f));*/
}
private Paint createPaint(PaintType type, int startx, int starty,
int w, int h)
{
// make sure that the blue color doesn't show up when filling a
// w by h rect
w++; h++;
int endx = startx + w;
int endy = starty + h;
Rectangle2D.Float r = new Rectangle2D.Float(startx, starty, w, h);
switch (type) {
case COLOR: return Color.red;
case GRADIENT: return
new GradientPaint(startx, starty, Color.red,
endx, endy, Color.green);
case LINEAR_GRADIENT: return
new LinearGradientPaint(startx, starty, endx, endy,
new float[] { 0.0f, 0.999f, 1.0f },
new Color[] { Color.red, Color.green, Color.blue });
case RADIAL_GRADIENT: return
new RadialGradientPaint(startx, starty,
(float)Math.sqrt(w * w + h * h),
new float[] { 0.0f, 0.999f, 1.0f },
new Color[] { Color.red, Color.green, Color.blue },
CycleMethod.NO_CYCLE);
case TEXTURE: {
BufferedImage bi =
new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setPaint(createPaint(PaintType.LINEAR_GRADIENT, 0, 0, w, h));
g.fillRect(0, 0, w, h);
return new TexturePaint(bi, r);
}
}
return Color.green;
}
private static void createTestFile() {
int w = 1280;
int h = 1024;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
try {
System.out.println("Create test image " + file.getAbsolutePath());
boolean b = ImageIO.write(img, "JPEG", file);
if (!b) {
throw new RuntimeException("Failed to create test image.");
}
} catch (IOException e) {
throw new RuntimeException("Test failed", e);
}
}
private static void createTestFile() {
int w = 1280;
int h = 1024;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
try {
System.out.println("Create test image " + file.getAbsolutePath());
boolean b = ImageIO.write(img, "JPEG", file);
if (!b) {
throw new RuntimeException("Failed to create test image.");
}
} catch (IOException e) {
throw new RuntimeException("Test failed", e);
}
}
@Override
public RadialGradientPaint create() {
Point2D center = ((Point2DSerializationWrapper) map.get(KEY_CENTER))
.create();
Point2D focus = ((Point2DSerializationWrapper) map.get(KEY_FOCUS))
.create();
float radius = ((Number) map.get(KEY_RADIUS)).floatValue();
return new RadialGradientPaint(center, radius, focus, getFractions(),
getColors(), getCycleMethod(), getColorSpace(), getTransform());
}
static void setPaint(RenderQueue rq, SunGraphics2D sg2d,
Paint paint, int ctxflags)
{
if (sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) {
setColor(rq, sg2d.pixel);
} else {
boolean useMask = (ctxflags & BufferedContext.USE_MASK) != 0;
switch (sg2d.paintState) {
case SunGraphics2D.PAINT_GRADIENT:
setGradientPaint(rq, sg2d,
(GradientPaint)paint, useMask);
break;
case SunGraphics2D.PAINT_LIN_GRADIENT:
setLinearGradientPaint(rq, sg2d,
(LinearGradientPaint)paint, useMask);
break;
case SunGraphics2D.PAINT_RAD_GRADIENT:
setRadialGradientPaint(rq, sg2d,
(RadialGradientPaint)paint, useMask);
break;
case SunGraphics2D.PAINT_TEXTURE:
setTexturePaint(rq, sg2d,
(TexturePaint)paint, useMask);
break;
default:
break;
}
}
}
/**
* Create a new (if possible, darker) <code>Paint</code> of the same Type.
* If the Type is not supported, the original <code>Paint</code> is returned.
* <p>
* @param paint a <code>Paint</code> implementation
* (e.g. {@link Color}, {@link GradientPaint}, {@link TexturePaint},..)
* <p>
* @return a (usually new, see above) <code>Paint</code>
*/
public static Paint darker(Paint paint) {
if (paint instanceof Color) {
return darker((Color) paint);
}
if (legacyAlpha == true) {
/*
* Legacy? Just return the original Paint.
* (this corresponds EXACTLY to how Paints used to be darkened)
*/
return paint;
}
if (paint instanceof GradientPaint) {
return darker((GradientPaint) paint);
}
if (paint instanceof LinearGradientPaint) {
return darkerLinearGradientPaint((LinearGradientPaint) paint);
}
if (paint instanceof RadialGradientPaint) {
return darkerRadialGradientPaint((RadialGradientPaint) paint);
}
if (paint instanceof TexturePaint) {
try {
return darkerTexturePaint((TexturePaint) paint);
}
catch (Exception e) {
/*
* Lots can go wrong while fiddling with Images, Color Models
* & such! If anything at all goes awry, just return the original
* TexturePaint. (TexturePaint's are immutable anyway, so no harm
* done)
*/
return paint;
}
}
return paint;
}
static void setPaint(RenderQueue rq, SunGraphics2D sg2d,
Paint paint, int ctxflags)
{
if (sg2d.paintState <= SunGraphics2D.PAINT_ALPHACOLOR) {
setColor(rq, sg2d.pixel);
} else {
boolean useMask = (ctxflags & BufferedContext.USE_MASK) != 0;
switch (sg2d.paintState) {
case SunGraphics2D.PAINT_GRADIENT:
setGradientPaint(rq, sg2d,
(GradientPaint)paint, useMask);
break;
case SunGraphics2D.PAINT_LIN_GRADIENT:
setLinearGradientPaint(rq, sg2d,
(LinearGradientPaint)paint, useMask);
break;
case SunGraphics2D.PAINT_RAD_GRADIENT:
setRadialGradientPaint(rq, sg2d,
(RadialGradientPaint)paint, useMask);
break;
case SunGraphics2D.PAINT_TEXTURE:
setTexturePaint(rq, sg2d,
(TexturePaint)paint, useMask);
break;
default:
break;
}
}
}
private static void createTestFile() {
int w = 1280;
int h = 1024;
BufferedImage img = new
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
Color[] colors = { Color.red, Color.green, Color.blue };
float[] dist = {0.0f, 0.5f, 1.0f };
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
RadialGradientPaint p =
new RadialGradientPaint(center, 0.5f * w, dist, colors);
g.setPaint(p);
g.fillRect(0, 0, w, h);
g.dispose();
try {
System.out.println("Create test image " + file.getAbsolutePath());
boolean b = ImageIO.write(img, "JPEG", file);
if (!b) {
throw new RuntimeException("Failed to create test image.");
}
} catch (IOException e) {
throw new RuntimeException("Test failed", e);
}
}
/**
* Sets the Paint in the current graphics state.
* @param paint The Paint object to be used to generate color in
* the rendering process.
* @see java.awt.Graphics#setColor
* @see GradientPaint
* @see TexturePaint
*/
public void setPaint(Paint paint) {
if (paint instanceof Color) {
setColor((Color) paint);
return;
}
if (paint == null || this.paint == paint) {
return;
}
this.paint = paint;
if (imageComp == CompositeType.SrcOverNoEa) {
// special case where compState depends on opacity of paint
if (paint.getTransparency() == Transparency.OPAQUE) {
if (compositeState != COMP_ISCOPY) {
compositeState = COMP_ISCOPY;
}
} else {
if (compositeState == COMP_ISCOPY) {
compositeState = COMP_ALPHA;
}
}
}
Class<? extends Paint> paintClass = paint.getClass();
if (paintClass == GradientPaint.class) {
paintState = PAINT_GRADIENT;
} else if (paintClass == LinearGradientPaint.class) {
paintState = PAINT_LIN_GRADIENT;
} else if (paintClass == RadialGradientPaint.class) {
paintState = PAINT_RAD_GRADIENT;
} else if (paintClass == TexturePaint.class) {
paintState = PAINT_TEXTURE;
} else {
paintState = PAINT_CUSTOM;
}
validFontInfo = false;
invalidatePipe();
}