How to draw a completly transparent Shape

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Add a Alpha/matte channel to the image first!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
sierrashirley78
Posts: 1
Joined: 2011-09-15T22:29:31-07:00
Authentication code: 8675308

Re: How to draw a completly transparent Shape

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to draw a completly transparent Shape

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply