Java Reference
In-Depth Information
Figure 11•10. Effects created with AlphaComposite
image) that has an “alpha channel” and supports transparent colors. Be sure to
check out how this off-screen BufferedImage is created.
Example 11-12 also illustrates the type of effects that are possible when you set a
clipping region. Java 2D allows any Shape to be used as a clipping region; graph-
ics are displayed only if they fall within this shape. The example uses the
java.awt.geom.Area class to define a complex shape, combining two ellipses and
a rectangle, and then uses this shape as a clipping region.
Example 11•12: CompositeEffects.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
public class CompositeEffects implements GraphicsExample {
Image cover; // The image we'll be displaying, and its size
static final int COVERWIDTH = 127, COVERHEIGHT = 190;
/** This constructor loads the cover image */
public CompositeEffects() {
java.net.URL imageurl = this.getClass().getResource("cover.gif");
cover = new javax.swing.ImageIcon(imageurl).getImage();
}
// These are basic GraphicsExample methods
public String getName() {return "Composite Effects";}
public int getWidth() { return 6*COVERWIDTH + 70; }
public int getHeight() { return COVERHEIGHT + 35; }
/** Draw the example */
public void draw(Graphics2D g, Component c) {
// fill the background
g.setPaint(new Color(175, 175, 175));
g.fillRect(0, 0, getWidth(), getHeight());
// Set text attributes
g.setColor(Color.black);
g.setFont(new Font("SansSerif", Font.BOLD, 12));
// Draw the unmodified image
Search WWH ::




Custom Search