JAVA2D Lighting and shadow effects __java

Source: Internet
Author: User

Introduction: In this article, we will show you how to add a lighting effect to a flat shape to achieve a class 3D appearance. Perhaps you are more satisfied with your ability to express words, but a picture is often able to produce better results.

  First, the introduction

In this article, we'll show you how to add a lighting effect to a flat shape to achieve a class 3D appearance. Perhaps you are more satisfied with your ability to express words, but a picture is often able to produce better results. This is also true for graphics processing, please refer to the two graphs in Figure 1. In this article, I'll show you how to overcome the annoyance of the flat left shape and replace it with a smoother, more comfortable shape.


Figure 1. Plain flat shape and the shape after Java 2D effect is applied


  Second, the realization of technical analysis

With the appropriate color, you can use the techniques described in this article to simulate a colored light that shines "over" your shape, creating a subtle glow effect. How did we achieve this effect? Please analyze the following code; The annotation above in Method Drawborderglow () Describes the key implementation methods in more detail:

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; Help round the corner of the class sharp
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) {
Create a translucent intermediate image that we can use to achieve a soft trim effect
GraphicsConfiguration gc = g.getdeviceconfiguration ();
BufferedImage img = gc.createcompatibleimage (width, height, transparency.translucent);
graphics2d g2 = Img.creategraphics ();
Clears the image so that all pixels have 0 alpha
G2.setcomposite (alphacomposite.clear);
G2.fillrect (0, 0, width, height);
Build our trim shape onto the image. Attention, we're booting up.
Anti-aliasing function to achieve soft trim effect. You can
Try commenting out the line that starts anti-aliasing, then
You will see the usual stiff trim effect.
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]);
}
Here's how to implement it: in order to achieve the glow effect, we started using an "inside" color thick pen
And the shape needed for the stroke. And then we kept on getting the pen thinner,
and constantly moving to the "outside" color,
And constantly increase the opacity of the color so that it looks dim inside the shape.
We use the "trim shape" that has been generated to our destination image, and since then,
The Src_atop rule trims the Stroke section outside our shape.
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);
Clear the background to white
G.setcolor (Color.White);
G.fillrect (0, 0, width, height);
Set Trim shapes
BufferedImage clipimage = Createclipimage (Clipshape);
graphics2d g2 = Clipimage.creategraphics ();
Fill a shape with a gradient
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);
Apply a boundary glow effect
Paintborderglow (G2, 8);
G2.dispose ();
G.drawimage (clipimage, 0, 0, NULL);


Notice that in the example above, I've added some optional lines of code to the annotation. You can remove these annotations and observe their effects on the build effect.

Note: Smart readers may have noticed that the techniques applied to the Paintborderglow () method can also be used to add a projection effect along the shape. You might as well start by guessing how to achieve this ... Okay, TIME's up. Instead of generating edges at the top of the shape (remember, pruning ensures that the stroke affects only the interior of the shape), we can create a variable gray boundary around our shape in advance. This means that the shadow strokes will appear outside of our shape, and the interior of the shadow strokes will be effectively generated through our shapes.

You can insert some of the following code into the example above to add a shaded boundary effect to the corresponding shape:

private void Paintbordershadow (graphics2d G2, int shadowwidth) {
G2.setrenderinghint (renderinghints.key_antialiasing,renderinghints.value_antialias_on);
int SW = shadowwidth*2;
for (int i=sw i >= 2; i-=2) {
Float pct = (float) (sw-i)/(SW-1);
G2.setcolor (Getmixedcolor (Color.light_gray, Pct,color.white, 1.0f-pct));
G2.setstroke (New Basicstroke (i));
G2.draw (Clipshape);
}
}
Apply the shadow effect of the boundary before we draw another part of the shape.
Paintbordershadow (g, 6);


Figure 2 Below is the final result image:


Figure 2 The final result shape after applying the Java 2D effect


  Third, summary

In this article, I'll just introduce you to a quick way to add a shadow effect. If I had the time, I would probably use a bright gray and a non-linear bevel to achieve a more realistic effect. Also note that this is only one of many ways to implement a projection effect using Java 2D. Note that Romain has discussed a number of different projection implementations in his blog. Swinglabs members also provide a Dropshadowborder implementation in SWINGX engineering, while Dropshadowpanel is currently under development

Original from: http://www.pcworld.com.cn/how_to_use/1/2006/0926/8062.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.