Java 2D开辟本领之"灯光与阴影"[Java编程]
本文“Java 2D开辟本领之"灯光与阴影"[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
1、引言
在本文中,我们将向你展示如作甚扁平形状增添一种灯光效果以实现一种类3D外观.
大概你对比称心于自己的文字表达本领,但一幅图片常常可以产生更好的效果.关于图形处理来说,也是如此;无妨请参考一下图1中的两种图形.在本文中,我将向你展示若何降服左边扁平形状所带来的懊恼而以一种更为光滑的更具舒服感的形状替换.
图1.普通扁平形状与施加Java 2D效果后的形状
2、实现技术解析
借助于得当的颜色,你可以利用本文中介绍的技术来模拟一种彩色光闪耀"越过"你的形状,从而生成一种奇妙的发光效果.我们是若何实现这一效果的呢?请解析下面的代码;在办法drawBorderGlow()上面的注释更为具体地介绍了关键实现办法:
import java.awt.geom.*;
import java.awt.image.*;
private static final Color clrHi = new Color(255, 229, 63);
private static final Color clrLo = new Color(255, 105, 0);
private static final Color clrGlowInnerHi = new Color(253, 239, 175, 148);
private static final Color clrGlowInnerLo = new Color(255, 209, 0);
private static final Color clrGlowOuterHi = new Color(253, 239, 175, 124);
private static final Color clrGlowOuterLo = new Color(255, 179, 0);
private Shape createClipShape() {
float border = 20.0f;
float x1 = border;
float y1 = border;
float x2 = width - border;
float y2 = height - border;
float adj = 3.0f; //帮忙圆化类锐的拐角
float arc = 8.0f;
float dcx = 0.18f * width;
float cx1 = x1-dcx;
float cy1 = 0.40f * height;
float cx2 = x1+dcx;
float cy2 = 0.50f * height;
GeneralPath gp = new GeneralPath();
gp.moveTo(x1-adj, y1+adj);
gp.quadTo(x1, y1, x1+adj, y1);
gp.lineTo(x2-arc, y1);
gp.quadTo(x2, y1, x2, y1+arc);
gp.lineTo(x2, y2-arc);
gp.quadTo(x2, y2, x2-arc, y2);
gp.lineTo(x1+adj, y2);
gp.quadTo(x1, y2, x1, y2-adj);
gp.curveTo(cx2, cy2, cx1, cy1, x1-adj, y1+adj);
gp.closePath();
return gp;
}
private BufferedImage createClipImage(Shape s) {
// 成立一半透明的中间图象,我们可以利用它来实现软修剪效果
GraphicsConfiguration gc = g.getDeviceConfiguration();
BufferedImage img = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
Graphics2D g2 = img.createGraphics();
//排除图象,这样全部的像素都具有零alpha
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(0, 0, width, height);
// 把我们的修剪形状生成到图象上.注意,我们启动了
// 反走样功效以实现软修剪效果.你可以
//尝试注释掉启动反走样的这一行,那么
//你会看到普通的僵硬的修剪效果.
g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fill(s);
g2.dispose();
return img;
}
private static Color getMixedColor(Color c1, float pct1, Color c2, float pct2) {
float[] clr1 = c1.getComponents(null);
float[] clr2 = c2.getComponents(null);
for (int i = 0; i < clr1.length; i++) {
clr1[i] = (clr1[i] * pct1) + (clr2[i] * pct2);
}
return new Color(clr1[0], clr1[1], clr1[2], clr1[3]);
}
//下面是实现本领:为了实现发光效果,我们开始利用一种"内部"颜色粗笔
//和笔画需求的形状.然后,我们不断地把笔变细,
//并且不断地移向"外部"颜色,
//并且不断地提高颜色的不透明度以便使其朝向形状的内部看上去暗淡.
//我们利用已经生成到我们的目的图象上的"修剪形状",这样以来,
//SRC_ATOP法则就会修剪在我们的形状外部的笔画部份.
private void paintBorderGlow(Graphics2D g2, int glowWidth) {
int gw = glowWidth*2;
for (int i=gw; i >= 2; i-=2) {
float pct = (float)(gw - i) / (gw - 1);
Color mixHi = getMixedColor(clrGlowInnerHi, pct,clrGlowOuterHi, 1.0f - pct);
Color mixLo = getMixedColor(clrGlowInnerLo, pct,clrGlowOuterLo, 1.0f - pct);
g2.setPaint(new GradientPaint(0.0f, height*0.25f, mixHi,0.0f, height, mixLo));
//g2.setColor(Color.WHITE);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, pct));
g2.setStroke(new BasicStroke(i));
g2.draw(clipShape);
}
}
Shape clipShape = createClipShape();
//Shape clipShape = new Ellipse2D.Float(width/4, height/4, width/2, height/2);
//把后台排除为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//设置修剪形状
BufferedImage clipImage = createClipImage(clipShape);
Graphics2D g2 = clipImage.createGraphics();
//利用突变填充形状
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setComposite(AlphaComposite.SrcAtop);
g2.setPaint(new GradientPaint(0, 0, clrHi, 0, height, clrLo));
g2.fill(clipShape);
//利用边界发光效果
paintBorderGlow(g2, 8);
g2.dispose();
g.drawImage(clipImage, 0, 0, null);
以上是“Java 2D开辟本领之"灯光与阴影"[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |