How to draw a completly transparent Shape
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Add a Alpha/matte channel to the image first!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
-
- Posts: 1
- Joined: 2011-09-15T22:29:31-07:00
- Authentication code: 8675308
Re: How to draw a completly transparent Shape
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
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
- 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
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
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/
https://imagemagick.org/Usage/