ClipImage Example

import java.awt.*;
import java.awt.font.*;

public class ClipImage extends java.applet.Applet {

Image img;

public void init() {
img = getImage(getClass().getResource("jaeger.jpg"));
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
} catch (Exception e) {
}
}

public void paint (Graphics g) {
Graphics2D g2d = (Graphics2D)g;
FontRenderContext frc = g2d.getFontRenderContext();
Font f = new Font ("Serif", Font.ITALIC | Font.BOLD, getWidth()/4);
TextLayout tl = new TextLayout("Jaeger", f, frc);
float width = (float) tl.getBounds().getWidth();
Shape sha = tl.getOutline(null, (getWidth()-width)/2, getHeight()/3);
g2d.drawImage(img, 0, getHeight()/3, this);
g2d.clip(sha);
g2d.drawImage(img, 0, 0, this);
}
}

Comments

Popular Posts