下面的代码绘制矩形和 2 个椭圆。
虽然应该画3个椭圆。
我的操作系统是 Windows 7 prof 64 位
我的 Java 是 1.6 x86 也经过 1.7 x64 测试。
为什么?
package tests;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
public class AntialiacingScaleTester {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JPanel circlePanel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.RED);
g2d.setStroke(new BasicStroke(1));
//g2d.drawOval(0, 0, 200, 200);
g2d.draw(new Ellipse2D.Double(0, 0, 200, 200));
AffineTransform old = g2d.getTransform();
g2d.setColor(Color.GREEN);
g2d.scale(1000, 1000);
g2d.setStroke(new BasicStroke(0.001f));
g2d.draw(new Ellipse2D.Double(0, 0, 0.225, 0.225));
g2d.setColor(Color.BLUE);
g2d.scale(10, 10);
g2d.setStroke(new BasicStroke(0.001f));
g2d.draw(new Ellipse2D.Double(0, 0, 0.025, 0.025));
g2d.setTransform(old);
}
};
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLayout(new MigLayout("fill"));
//frame.add(circlePanel, "w 300, h 300, grow");
//frame.add(circlePanel);
frame.setLayout(null);
circlePanel.setBounds(new Rectangle(0, 0, 300, 300));
frame.add(circlePanel);
frame.setBounds(0, 0, 350, 300);
//frame.pack();
frame.setVisible(true);
}
});
}
}