Java Example Program/ Sample Source Code
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.GraphicAttribute;
import java.awt.font.ImageGraphicAttribute;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
public class GetAdvance_ImageGraphicAttribute {
public static void main(String[] args) {
Frame frame = new Frame("GetAdvance_ImageGraphicAttribute");
frame.setSize(400, 400);
frame.add(new CanvasToDisplay());
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class CanvasToDisplay extends Component {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Font font = new Font("Verdana", Font.BOLD, 16);
Image image = Toolkit.getDefaultToolkit().getImage("image.gif");
ImageGraphicAttribute imageAttribute = new ImageGraphicAttribute(image, GraphicAttribute.TOP_ALIGNMENT);
AttributedString as = new AttributedString("JavaTips.net");
as.addAttribute(TextAttribute.FONT, font);
as.addAttribute(TextAttribute.CHAR_REPLACEMENT, imageAttribute, 5, 6);
g2D.drawString(as.getIterator(), 20, 120);
}
} |
|
|