AttributedString: addAttribute(Attribute attribute, Object value)
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainClass{
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(300, 200);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
String s = "\"www.java2s.com,\" www.java2s.com";
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font plainFont = new Font("Times New Roman", Font.PLAIN, 24);
AttributedString as = new AttributedString(s);
as.addAttribute(TextAttribute.FONT, plainFont);
g2.drawString(as.getIterator(), 24, 70);
}
}
Related examples in the same category