我必须在java中绘制精确的拱门。我正在使用 Graphics2D.fillArc()。问题是它只接受整数并且拱门不精确,我不能让拱门的度数平滑增加。有谁知道解决方法?
问问题
141 次
1 回答
3
这是我使用 Arc2D的SSCCE 。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Arc2D;
import javax.swing.*;
@SuppressWarnings("serial")
public class ChangingArcs extends JPanel {
private static final Color ARC_FILL_COLOR = Color.RED;
private static final int TIMER_DELAY = 20;
private static final int ARC_X = 100;
private static final int ARC_Y = 100;
private static final int ARC_W = 500;
private static final int ARC_H = 500;
protected static final double DELTA_EXTEND = 0.5;
private Arc2D arc;
private double extend = 0;
public ChangingArcs() {
new Timer(TIMER_DELAY, new ActionListener() {
public void actionPerformed(ActionEvent e) {
extend += DELTA_EXTEND;
extend %= 360;
double start = -extend/2;
arc = new Arc2D.Double(ARC_X, ARC_Y, ARC_W, ARC_H, start, extend, Arc2D.PIE);
repaint();
}
}).start();
}
public Dimension getPreferredSize() {
return new Dimension(ARC_W + 2 * ARC_X, ARC_H + 2 * ARC_Y);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
if (arc != null) {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(ARC_FILL_COLOR);
g2.fill(arc);
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("ChangingArcs");
frame.getContentPane().add(new ChangingArcs());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
于 2011-06-04T13:26:13.503 回答