import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class testarc
{
public static void main(String[] args)
{
FillFrame frame = new FillFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class FillFrame extends JFrame
{
public FillFrame()
{
setTitle("FillTest");
setSize(400,400); // add panel to frame DrawPanel panel = new DrawPanel();
add(panel); }
}
class DrawPanel extends JPanel implements Runnable
{ private Thread t;
private int k=0;
public DrawPanel(){
setBackground(Color.white);
t = new Thread(this);
t.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
double xCenter=getWidth()/2;//中心点x
double yCenter=getHeight()/2;//中心点y
double radius=(int)(getWidth()*0.4);//半径
Ellipse2D circle = new Ellipse2D.Double(xCenter-radius,yCenter-radius,2*radius,2*radius);
g2.draw(circle); //画一个风车
for(int i=0;i<4;i++)
{
Arc2D arc1=new Arc2D.Double(xCenter-radius,yCenter-radius,2*radius,2*radius,-30+90*i+30*k,30,Arc2D.PIE);
g2.setPaint(Color.RED);
g2.fill(arc1);
} } public void run()
{
for(int i=0;i<3 ;i=(i++)%3)
{
k = (k++)%3 ;
repaint();
try{
t.sleep(1000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}}
要想让风车转,需要定时改变风车的位置(旋转角度),即
在timer事件中设置旋转的角度,然后在paint中根据新的角度计算风车的位置,再按照新位置画
这样风车就动了
timer事件中每次设置的角度增量影响决定风车的旋转快慢