import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.TextAttribute;
import java.awt.font.TextLayout;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BasicShapes extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int x = 10, y = 10, start = 2, end = 4;
AttributedString astr = new AttributedString("aString");
astr.addAttribute(TextAttribute.FONT, new Font("", 1, 1), start, end);
astr.addAttribute(TextAttribute.BACKGROUND, Color.red, start, end);
// Draw mixed-style text
TextLayout tl = new TextLayout(astr.getIterator(), g2d.getFontRenderContext());
tl.draw(g2d, x, y);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Basic Shapes");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new BasicShapes());
frame.setSize(350, 250);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
|