-
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
}
}
-
April 22nd, 2010, 04:38 AM
#2
Re: Graphic: Inner glow
Originally Posted by happy_hippie
I would be happy if you would try it and tell me what you think.
I think it won't compile because it calls a Glow constructor with 3 arguments, that doesn't exist in the code you posted.
God is in the details...
M. van der Rohe
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 22nd, 2010, 07:27 AM
#3
Re: Graphic: Inner glow
Whoops, that's correct.
It was from when i tested
Here is the correct test code, without the last parameter
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();
}
}
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);
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);
glow1.paint(g2, new Area(gp));
g2.setTransform(atBef);
// Circle end
}
}
-
April 22nd, 2010, 08:27 AM
#4
Re: Graphic: Inner glow
Am I missing something, or is it supposed to just display 2 objects with an internal shading gradient?
Looks OK, although I'm pretty sure I've seen similar graphics using RadialGradientPaint.
Don't ask what it means, but rather how it is used...
L. Wittgenstein
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 22nd, 2010, 11:53 AM
#5
Re: Graphic: Inner glow
The paint method you gave, is only round.
Mine gives a inner glow, no matter which shape you use, which is not the same.
-
April 22nd, 2010, 11:59 AM
#6
Re: Graphic: Inner glow
And yes, it is only showing 2 shapes, to display the effect on different shapes
-
April 22nd, 2010, 06:05 PM
#7
Re: Graphic: Inner glow
Originally Posted by happy_hippie
The paint method you gave, is only round.
what? I gave no paint method. What have you been smoking?
Mine gives a inner glow, no matter which shape you use, which is not the same.
Not the same as what?
When a programming language is created that allows programmers to program in simple English, it will be discovered that programmers cannot speak English...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 23rd, 2010, 01:19 AM
#8
Re: Graphic: Inner glow
Originally Posted by dlorde
Am I missing something, or is it supposed to just display 2 objects with an internal shading gradient?
Looks OK, although I'm pretty sure I've seen similar graphics using RadialGradientPaint.
Don't ask what it means, but rather how it is used...
L. Wittgenstein
It must be you that has been smoking weed.
As i read that post, you mensions RadialGradientPaint, which is a paint, right?
dlorde:Not the same as what? | It is not a paint, but a glow.
-
April 23rd, 2010, 04:12 AM
#9
Re: Graphic: Inner glow
Originally Posted by happy_hippie
It must be you that has been smoking weed.
As i read that post, you mensions RadialGradientPaint, which is a paint, right?
It's a class that implements the Paint interface. Did you follow the link?
It is not a paint, but a glow.
Why don't you explain what you mean by a 'paint' and a 'glow' in this context and what the significant differences are between them. I ran the code you posted and saw what looked like very slight radial shading gradients in two shapes.
The limits of your language are the limits of your world...
L. Wittgenstein
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 23rd, 2010, 05:07 AM
#10
Re: Graphic: Inner glow
Originally Posted by dlorde
It's a class that implements the Paint interface. Did you follow the link?
Why don't you explain what you mean by a 'paint' and a 'glow' in this context and what the significant differences are between them. I ran the code you posted and saw what looked like very slight radial shading gradients in two shapes.
The limits of your language are the limits of your world...
L. Wittgenstein
Yes i did follow the link, thats why i say RadialGradientPaint is a paint, a gradient paint that is painted in a circle.
What i have provided, the Glow, is like in Photoshop, where you defines an inner glow to a shape/graphic.
-
April 23rd, 2010, 05:13 AM
#11
Re: Graphic: Inner glow
OK. The terminology is still opaque to me (maybe it's a PhotoShop thing?).
I guess the demo doesn't show the 'glow' to best effect.
Don't ask what it means, but rather how it is used...
L. Wittgenstein
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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
|