我已经编写了这个用于绘制填充弧的java代码,在每次循环迭代中,从0到360度,其端点角度增加1,但这不能正常工作所以请帮忙。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class A {
public static void main(String arg[]) throws Exception {
JFrame f = new JFrame();
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setUndecorated(true);
f.setVisible(true);
f.getContentPane().setBackground(Color.BLACK);
f.setLayout(new FlowLayout());
Circle c;
for(int i=0; i<=360; i++) {
c = new Circle(-i);
f.add(c);
Thread.sleep(6);
f.getContentPane().remove(c);
f.getContentPane().revalidate();
f.getContentPane().repaint();
}
}
}
class Circle extends JPanel {
int angle;
public Circle(int angle) {
this.angle=angle;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillArc(50, 50, 100, 100, 90, angle);
}
}
答案 0 :(得分:2)
我不会列出您代码中的所有错误。我修了大部分。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Scratch {
public static void main(String arg[]) throws Exception {
JFrame f = new JFrame();
f.setSize(600, 600);
f.setVisible(true);
f.getContentPane().setBackground(Color.BLACK);
f.setLayout(new BorderLayout());
for(int i=0; i<=360; i++) {
final int fi = i;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f.getContentPane().removeAll();
Circle c = new Circle(-fi);
f.add(c);
f.getContentPane().revalidate();
f.getContentPane().repaint();
}
});
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
}
}
}
class Circle extends JPanel {
int angle;
public Circle(int angle) {
this.angle=angle;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.RED);
g.fillArc(50, 50, 100, 100, 0, angle);
}
}
我建议您使用一个组件来更新它的图像,而不是删除/添加不同的组件。