Java Example Program / Sample Source Code
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedString;
public class DrawingMixedStyle {
public static void main(String[] args) {
Frame frame = new Frame("DrawingMixedStyle");
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 plainFont = new Font("Times New Roman", Font.BOLD, 24);
AttributedString attributedString = new AttributedString("JavaTips.net");
attributedString.addAttribute(TextAttribute.FONT, plainFont);
attributedString.addAttribute(TextAttribute.FONT, plainFont, 0, 5);
attributedString.addAttribute(TextAttribute.BACKGROUND, Color.blue, 0, 12);
// Draw mixed-style text
TextLayout textLayout = new TextLayout(attributedString.getIterator(), g2D.getFontRenderContext());
textLayout.draw(g2D, 10, 45);
}
} |
|
|