您的位置:首页 > 其它

16.12

2016-07-05 21:47 141 查看
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Test_16_12 extends JFrame{

public Test_16_12(){
add(new JP());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test_16_12 t1 = new Test_16_12();
t1.setTitle("Test_16.12");
t1.setLocationRelativeTo(null);
t1.setSize(300,300);
t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t1.setVisible(true);
}

static class JP extends JPanel{
private int h =0;
public JP(){
Timer timer = new Timer(10,new TimerListener());
timer.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);

int xCenter = getWidth()/2;
int yCenter = getHeight()/2;
int radius = (int)(Math.min(getWidth(), getHeight())*0.4);
int x = xCenter - radius;
int y = yCenter - radius;

g.fillArc(x, y, 2*radius, 2*radius, 0+h, 30);
g.fillArc(x, y, 2*radius, 2*radius, 90+h, 30);
g.fillArc(x, y, 2*radius, 2*radius, 180+h, 30);
g.fillArc(x, y, 2*radius, 2*radius, 270+h, 30);
}
class TimerListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
h += 3;
repaint();
}
}
}
}


Test_16_12.java
需要注意的: Timer的创建应该放在JP的构造函数中



效果图:下图实际上是动态的

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: