Skip to main content

Question to Chris Campbell's Java 2D Trickery: Soft Clipping

1 reply [Last post]
ylzhao
Offline
Joined: 2004-08-22
Points: 0

Hi Chris,
below is your soft clipping code:
=========================================================
g.setColor(Color.BLACK);
g.fillRect(0, 0, width, height);
BufferedImage img = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
Graphics2D g2 = img.createGraphics();

g2.setComposite(AlphaComposite.Src);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.WHITE);
g2.fillOval(width/4, height/4, width/2, height/2);
g2.setComposite(AlphaComposite.SrcAtop);
g2.setPaint(new GradientPaint(0, 0, Color.RED, 0, height, Color.YELLOW));
g2.fillRect(0, 0, width, height);
g2.dispose();

g.drawImage(img, 0, 0, null);
=========================================================

I set the width = 16 and height = 16, and commnet out the line that enables antialiasing, then ouput the alpha value of the BufferedImage object (16 x 16):

=========================================================
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 255 0 0 0 0 0 0 0
0 0 0 0 0 255 255 255 255 255 255 0 0 0 0 0
0 0 0 0 0 255 255 255 255 255 255 255 0 0 0 0
0 0 0 0 0 255 255 255 255 255 255 255 0 0 0 0
0 0 0 0 255 255 255 255 255 255 255 255 0 0 0 0
0 0 0 0 0 255 255 255 255 255 255 255 0 0 0 0
0 0 0 0 0 255 255 255 255 255 255 0 0 0 0 0
0 0 0 0 0 0 255 255 255 255 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
=========================================================

If use antialiasing, the the alpha output is:
=========================================================
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 48 183 231 239 191 52 0 0 0 0 0
0 0 0 0 48 243 255 255 255 255 251 56 0 0 0 0
0 0 0 0 187 255 255 255 255 255 255 195 0 0 0 0
0 0 0 0 231 255 255 255 255 255 255 239 0 0 0 0
0 0 0 0 231 255 255 255 255 255 255 239 0 0 0 0
0 0 0 0 187 255 255 255 255 255 255 195 0 0 0 0
0 0 0 0 48 243 255 255 255 255 251 56 0 0 0 0
0 0 0 0 0 48 187 235 239 195 56 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
=========================================================

My question: What does the antiliasing algorithm do? I see the antiliasing algorithm has altered the destination's alpha value, and I want to understand that why when the antiliasing is on, then the alpha value of the image will be altered.

Message was edited by: ylzhao

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Chris Campbell

On Sep 29, 2006, at 3:28 AM, java2d@javadesktop.org wrote:
> My question: What does the antiliasing algorithm do?

It basically blends the source pixels at the edge of a shape with the
corresponding destination pixels:
http://download.java.net/jdk6/docs/api/java/awt/
RenderingHints.html#KEY_ANTIALIASING

> Does the antiliasing algorithm alter the destination's alpha value?

In this case, yes: the destination is fully transparent, so the
pixels near the edge will have an alpha value that falls somewhere
between 0 and 1.

Does this help?

Thanks,
Chris

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".