10.4. Transparent Drawing

Java 2D permits you to assign transparency (alpha) values to drawing operations so that the underlying graphics partially shows through when you draw shapes or images. You set a transparency by creating an AlphaComposite object and then passing the AlphaComposite object to the setComposite method of the Graphics2D object. You create an AlphaComposite by calling AlphaComposite.getInstance with a mixing rule designator and a transparency (or “alpha”) value. For example,

float alpha = 0.75f;
int type = AlphaComposite.SRC_OVER;
AlphaComposite composite =
   AlphaComposite.getInstance(type, alpha);

The AlphaComposite API provides eight built-in mixing rules, but the one normally used for drawing with transparency settings ...

Get Core Web Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.