Page 1 of 1

Posted: 2006-03-14T21:44:53-07:00
by anthony
Add a Alpha/matte channel to the image first!

Re: How to draw a completly transparent Shape

Posted: 2011-09-29T18:40:27-07:00
by sierrashirley78
Download the source: TransparencyExample.java (plus WindowUtilities.java and ExitListener.java if you don't have them from the previous examples).

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

/** An illustration of the use of AlphaComposite to make partially
* transparent drawings.
*
* From tutorial on learning Java2D at
* http://www.apl.jhu.edu/~hall/java/Java2D-Tutorial.html
*
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/

public class TransparencyExample extends JPanel {
private static int gap=10, width=60, offset=20,
deltaX=gap+width+offset;
private Rectangle
blueSquare = new Rectangle(gap+offset, gap+offset, width, width),
redSquare = new Rectangle(gap, gap, width, width);

private AlphaComposite makeComposite(float alpha) {
int type = AlphaComposite.SRC_OVER;
return(AlphaComposite.getInstance(type, alpha));
}

private void drawSquares(Graphics2D g2d, float alpha) {
Composite originalComposite = g2d.getComposite();
g2d.setPaint(Color.blue);
g2d.fill(blueSquare);
g2d.setComposite(makeComposite(alpha));
g2d.setPaint(Color.red);
g2d.fill(redSquare);
g2d.setComposite(originalComposite);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
for(int i=0; i<11; i++) {
drawSquares(g2d, i*0.1F);
g2d.translate(deltaX, 0);
}
}

public static void main(String[] args) {
String title = "Transparency example: alpha of the top (red) " +
"square ranges from 0.0 at the left to 1.0 at " +
"the right. Bottom (blue) square is opaque.";
WindowUtilities.openInJFrame(new TransparencyExample(),
11*deltaX + 2*gap, deltaX + 3*gap,
title, Color.lightGray);
}
}
____________________________________________________________________--
Auroinfo

Re: How to draw a completly transparent Shape

Posted: 2011-09-29T18:45:38-07:00
by anthony
Arrgghhh.... The author deleted his original post.

This is a NO-NO. now we have a discussion which does not contain the problem that is being discussed!
Nor how the above java, which contains not Imagemagick at all, is related to that discussion