主题:[原创]组件字体抗锯齿的一个例子
不知道喜欢研究记事本程序的朋友,是不是注意到字体边缘不够平滑的问题?
其实实现字体的反走样(抗锯齿)的功能很简单,代码如下。
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class AntiliasDemo {
public AntiliasDemo() {
JFrame frame = new JFrame();
Container con = frame.getContentPane();
con.setLayout(new BorderLayout());
JTextComponent text = new AntiliasText();
text.setFont(new Font("Dialog", Font.PLAIN, 14));
con.add(new JScrollPane(text), "Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 400, 400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
new AntiliasDemo();
}
private class AntiliasText extends JTextArea {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paint(g2);
}
}
}
补充一下,如果想实现多样式的记事本就需要继承JTextPane,另外,对于所有的组件
都可以用类似的方式实现抗锯齿,extends JButton,extends JMenu,
extends JTree 。。。。
其实实现字体的反走样(抗锯齿)的功能很简单,代码如下。
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class AntiliasDemo {
public AntiliasDemo() {
JFrame frame = new JFrame();
Container con = frame.getContentPane();
con.setLayout(new BorderLayout());
JTextComponent text = new AntiliasText();
text.setFont(new Font("Dialog", Font.PLAIN, 14));
con.add(new JScrollPane(text), "Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 400, 400, 300);
frame.setVisible(true);
}
public static void main(String[] args) {
new AntiliasDemo();
}
private class AntiliasText extends JTextArea {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paint(g2);
}
}
}
补充一下,如果想实现多样式的记事本就需要继承JTextPane,另外,对于所有的组件
都可以用类似的方式实现抗锯齿,extends JButton,extends JMenu,
extends JTree 。。。。