April 21st, 2010 03:11 PM
#1
Graphic: Inner glow
Hey.
I have just finished some code, that will allow you to create an inner glow inside an Area.
I would be happy if you would try it and tell me what you think.
I also provides a test, that draws an ellipse and a star, with inner glows.
It uses JH Labs image API to create the glow effect, which can be gotten here: http://www.jhlabs.com/ip/filters/index.html
This is the "Inner glow" source code:
import com.jhlabs.image.GaussianFilter;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
/**
*
* @author Dan From <dan.from@gmail.com>
*/
public class Glow
{
private BufferedImage glow = null;
private Color color = null;
private int size = 0;
public Glow (Color c, int s)
{
color = c;
size = s;
}
public void paint (Graphics2D g, Area a) {
int width = a.getBounds().width + 2;
int height = a.getBounds().height + 2;
glow = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = glow.createGraphics();
Area area1 = new Area(new Rectangle(0, 0, width, height));
a.transform(AffineTransform.getTranslateInstance(1, 1));
area1.subtract(a);
BufferedImage tmp = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g3 = tmp.createGraphics();
g3.setPaint(new GradientPaint(0, 0, color, width, height, color));
g3.fill(area1);
GaussianFilter gf = new GaussianFilter(size+1);
glow = gf.filter(tmp, glow);
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_IN, (float)0.0);
g2.setColor(Color.blue);
g2.setComposite(ac);
g2.fill(area1);
g.drawImage(glow, null, -1, -1);
g.drawImage(glow, null, -1, -1);
}
}
This is the code for the test:
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JPanel;
import org.frog.effects.Glow;
public class GlowDemo extends JFrame
{
public GlowDemo ()
{
super("Inner glow demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PaintCanvas pc = new PaintCanvas();
pc.setPreferredSize(new Dimension(500, 500));
getContentPane().add(pc);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main (String args[])
{
new GlowDemo();
}
}
/**
*
* @author Skruppe
*/
class PaintCanvas extends JPanel
{
public PaintCanvas ()
{
super();
}
@Override
public void paintComponent (Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform atBef = g2.getTransform();
// Circle start
g2.translate(10, 10);
Area a = new Area(new Ellipse2D.Double(0, 0, 150, 150));
// Background
g2.setBackground(new Color(28, 28, 28));
g2.fill(a);
// Circle
g2.setColor(new Color(3, 56, 89));
g2.fillOval(0, 0, 150, 150);
Glow glow = new Glow(Color.black, 40, 150);
glow.paint(g2, a);
g2.setTransform(atBef);
// Circle end
// Star start
g2.translate(170, 10);
GeneralPath gp = new GeneralPath();
gp.moveTo(0, 51);
gp.lineTo(53, 51);
gp.lineTo(72, 0);
gp.lineTo(89, 51);
gp.lineTo(143, 51);
gp.lineTo(100, 84);
gp.lineTo(116, 135);
gp.lineTo(72, 105);
gp.lineTo(28, 135);
gp.lineTo(43, 84);
gp.lineTo(0, 51);
Area star = new Area(gp);
// Background
g2.setColor(new Color(86, 3, 89));
g2.fill(star);
// Star
Glow glow1 = new Glow(Color.red, 10, 150);
glow1.paint(g2, new Area(gp));
g2.setTransform(atBef);
// Circle end
}
}
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks