I'd like to submit a small Swing program that shows the effects of the
various AlphaComposite variants on the classic red-green-blue circles.
        I don't have a lot of experience with OpenGL, but I am aware that the
equivalent of what this program does can also be programmed with it. If
anyone on this list with previous OpenGL experience would be so kind as
to write a version of this program in OpenGL, I'd be very grateful.
        The point here is that I have seen different behavior between Java 2D
and OpenGL when using equivalent alpha compositing rules. This has been
detected with two image viewing programs under development here at work,
one is Java 2D based and the other is OpenGL based. As those programs
are a bit complex, I'd like to determine if the source of this
difference is due to disparities between Java 2D and OpenGL or problems
in the programs themselves.
        Maybe this small program could be used as an example of alpha
compositing in a tutorial somewhere, if anyone thinks it is worthy. I
give you all full permission to use this code as you wish, no
guarantees. 8-)


------------ Alpha.java ------------
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import javax.swing.*;
import javax.swing.event.*;

/* This class is a Swing program that shows the effect of alpha
compositing on
   three circles (red, green and blue) with various compositing rules.
*/
public class Alpha extends JComponent
{
        /* the names of each of the alpha compositing modes */
        static final String[] names = {
                "Source Over Destination",
                "Source Held out by Destination",
                "Source in Destination",
                "Source",
                "Destination Over Source",
                "Destination Held out by Source",
                "Destination in Source",
                "Clear",
                null
        };

        /* the alpha compositing modes */
        static final AlphaComposite[] composite = {
                AlphaComposite.SrcOver,
                AlphaComposite.SrcOut,
                AlphaComposite.SrcIn,
                AlphaComposite.Src,
                AlphaComposite.DstOver,
                AlphaComposite.DstOut,
                AlphaComposite.DstIn,
                AlphaComposite.Clear,
                null
        };

        /* The circle radius, calculated in drawComponent. */
        double radius;

        /* The alpha value used for the circle colors. */
        int alpha = 128;

        /* The repaint method. */
        public void paintComponent(Graphics g)
        {
                Graphics2D g2d = (Graphics2D) g;
                Font font = g2d.getFont();
                FontRenderContext frc = g2d.getFontRenderContext();

                super.paintComponent(g);

                /* clear background */
                g2d.setPaint(Color.white);
                g2d.fillRect(0, 0, getWidth(), getHeight());

                /* calculate circle radius to fit all */
                radius = Math.min(getWidth(), getHeight()) / 12.0;

                /* draw circles */
                for (int i=0; i<3; i++) {
                        double y = (double)i * radius * 4.0 + radius * 2.0;

                        for(int j=0; j<3; j++) {
                                double x = (double)j * radius * 4.0 + radius * 2.0;

                                Composite comp = composite[i*3 + j];
                                if (comp == null) continue;

                                /* draw mode name */
                                g2d.setPaint(Color.black);
                                g2d.setComposite(AlphaComposite.SrcOver);

                                double namewidth = 
g2d.getFont().getStringBounds(names[i*3 + j],
frc).getWidth();

                                g2d.drawString(
                                        names[i*3 + j],
                                        (float)(x - radius * 2.0 + (radius * 4 - 
namewidth) / 2.0),
                                        (float)(y - radius * 1.8)
                                );

                                /* draw circles */
                                g2d.setComposite(comp);
                                drawCircles(g2d, x, y);
                        }
                }
        }

        /* The method that draws the circles around a given reference point. */
        void drawCircles(Graphics2D g, double x, double y)
        {
                Color color;

                /* red with alpha = 0.5 */
                color = new Color(255, 0, 0, alpha);
                g.setPaint(color);
                g.fill(
                        new Ellipse2D.Double(
                                x - 1.5 * radius,
                                y - 1.5 * radius,
                                radius*2.0,
                                radius*2.0
                        )
                );

                /* green with alpha = 0.5 */
                color = new Color(0, 255, 0, alpha);
                g.setPaint(color);
                g.fill(
                        new Ellipse2D.Double(
                                x - radius,
                                y - 0.5 * radius,
                                radius*2.0,
                                radius*2.0
                        )
                );

                /* blue with alpha = 0.5 */
                color = new Color(0, 0, 255, alpha);
                g.setPaint(color);
                g.fill(
                        new Ellipse2D.Double(
                                x - 0.5 * radius,
                                y - 1.5 * radius,
                                radius*2.0,
                                radius*2.0
                        )
                );

                /* black with alpha = 0.5 */
                color = new Color(255, 255, 255, alpha);
                g.setPaint(color);
                g.fill(new Ellipse2D.Double(x - radius/8.0, y - radius/8.0,
radius/4.0, radius/4.0));
        }

        /* Entry point method. */
        public static void main(String[] args)
        {
                JFrame frame = new JFrame("Alpha Composite Test");

                frame.addWindowListener(
                        new WindowAdapter() {
                                public void windowClosing(WindowEvent e)
                                {
                                        System.out.println("Exiting...");
                                        System.exit(0);
                                }
                        }
                );
                frame.getContentPane().add(new Alpha(), BorderLayout.CENTER);
                frame.setSize(600, 600);
                frame.setVisible(true);
        }
}
------------ Alpha.java ------------


--
---------------------------------------------------------------
  Alexandre Sieira Vilar - http://www.tecgraf.puc-rio.br/~asv
 Computer Engineering Student at PUC-Rio - GNU and Linux rock!
     3rd kyu Aikikkai Aikidoka (as of November 28, 1998)
---------------------------------------------------------------
"Contemplate the workings of this world, listen to the words
of the wise, and take  all  that  is good as your own.  With
this as your base, open your own  door  to  truth.   Do  not
overlook the truth that is right before you."

                                   Morihei Ueshiba

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to