Results 1 to 1 of 1
  1. #1
    happy_hippie is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default Inner glow on Area (source code)

    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: Jerry's Java Image Processing Pages

    This is the "Inner glow" source code:

    Java 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:

    Java Code:
    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
      }
    
    }
    Last edited by happy_hippie; 04-22-2010 at 02:56 PM. Reason: The test code used a Glow with 3 parameters, which only existed in my test. Now it would compile without errors :)

Similar Threads

  1. How can i add a new count to this source code ?
    By mm2236 in forum New To Java
    Replies: 2
    Last Post: 01-31-2010, 04:32 AM
  2. Replies: 1
    Last Post: 11-28-2008, 07:27 PM
  3. Help me out in compiling the source code
    By aks.nitw in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 09:33 AM
  4. Need a source code
    By vissu007 in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 08:08 PM

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
  •