Java 2D Development tips for "lights and Shadows"

Source: Internet
Author: User
Tags final trim

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 middle image, we can use it to achieve the 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);


//Put our trim shapes on the image. Attention, we've activated the
.

//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]);


}


//Below is the implementation tip: In order to achieve the glow effect, we began to use an "internal" color thick pen


//And strokes are required for the shape. Then, we keep the pen thin,


//And constantly move to the "outside" color,


//And continuously increases the opacity of the color so that it looks dim toward the interior of the shape.


//We use the "trim shape" that has been generated to our destination image, so that since

The
//src_atop rule will trim the strokes that are 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);


//Remove Background to white


G.setcolor (Color.White);


g.fillrect (0, 0, width, height);


//Set trim shape


BufferedImage clipimage = Createclipimage (Clipshape);


graphics2d g2 = Clipimage.creategraphics ();


//Using gradient fill shape


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 boundary luminous effect


Paintborderglow (G2, 8);


G2.dispose ();


g.drawimage (clipimage, 0, 0, NULL);

Related Article

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.