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