Java/2D Graphics GUI/TextAttribute
Содержание
- 1 TextAttribute.BACKGROUND: Set background for AttributedString
- 2 TextAttribute.FONT: Set font for AttributedString
- 3 TextAttribute.FOREGROUND: Set color for AttributedString
- 4 TextAttribute.SIZE: Set SIZE for AttributedString
- 5 TextAttribute.STRIKETHROUGH: Set STRIKETHROUGH for AttributedString
- 6 TextAttribute.SUPERSCRIPT: Set SUPERSCRIPT for AttributedString
- 7 TextAttribute.UNDERLINE: Set underline for AttributedString
TextAttribute.BACKGROUND: Set background for AttributedString
import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
public class TextAttributesBackground extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AttributedString as1 = new AttributedString("1234567890");
as1.addAttribute(TextAttribute.BACKGROUND, Color.LIGHT_GRAY, 2, 9);
g2d.drawString(as1.getIterator(), 15, 60);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Text attributes");
frame.add(new TextAttributesBackground());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(620, 190);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
TextAttribute.FONT: Set font for AttributedString
import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
public class TextAttributesFont extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Serif", Font.PLAIN, 40);
AttributedString as1 = new AttributedString("1234567890");
as1.addAttribute(TextAttribute.FONT, font);
g2d.drawString(as1.getIterator(), 15, 60);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Text attributes");
frame.add(new TextAttributesFont());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(620, 190);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
TextAttribute.FOREGROUND: Set color for AttributedString
import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
public class TextAttributesColor extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AttributedString as1 = new AttributedString("1234567890");
as1.addAttribute(TextAttribute.FOREGROUND, Color.red, 0, 2);
g2d.drawString(as1.getIterator(), 15, 60);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Text attributes");
frame.add(new TextAttributesColor());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(620, 190);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
TextAttribute.SIZE: Set SIZE for AttributedString
import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
public class TextAttributesSize extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AttributedString as1 = new AttributedString("1234567890");
as1.addAttribute(TextAttribute.SIZE, 40);
g2d.drawString(as1.getIterator(), 15, 60);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Text attributes");
frame.add(new TextAttributesSize());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(620, 190);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
TextAttribute.STRIKETHROUGH: Set STRIKETHROUGH for AttributedString
import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
public class TextAttributesStrikeThrough extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AttributedString as1 = new AttributedString("1234567890");
as1.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 2, 8);
g2d.drawString(as1.getIterator(), 15, 60);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Text attributes");
frame.add(new TextAttributesStrikeThrough());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(620, 190);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
TextAttribute.SUPERSCRIPT: Set SUPERSCRIPT for AttributedString
import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
public class TextAttributesSuperscript extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AttributedString as1 = new AttributedString("1234567890");
as1.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 5, 7);
g2d.drawString(as1.getIterator(), 15, 60);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Text attributes");
frame.add(new TextAttributesSuperscript());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(620, 190);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
TextAttribute.UNDERLINE: Set underline for AttributedString
import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
public class TextAttributesUnderline extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AttributedString as1 = new AttributedString("1234567890");
as1.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 3, 4);
g2d.drawString(as1.getIterator(), 15, 60);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Text attributes");
frame.add(new TextAttributesUnderline());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(620, 190);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}