1 import java.awt.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 5 import javax.swing.*; 6 7 public class Test_16_12 extends JFrame{ 8 9 public Test_16_12(){ 10 add(new JP()); 11 } 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 Test_16_12 t1 = new Test_16_12(); 15 t1.setTitle("Test_16.12"); 16 t1.setLocationRelativeTo(null); 17 t1.setSize(300,300); 18 t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 t1.setVisible(true); 20 } 21 22 static class JP extends JPanel{ 23 private int h =0; 24 public JP(){ 25 Timer timer = new Timer(10,new TimerListener()); 26 timer.start(); 27 } 28 public void paintComponent(Graphics g){ 29 super.paintComponent(g); 30 31 int xCenter = getWidth()/2; 32 int yCenter = getHeight()/2; 33 int radius = (int)(Math.min(getWidth(), getHeight())*0.4); 34 int x = xCenter - radius; 35 int y = yCenter - radius; 36 37 38 g.fillArc(x, y, 2*radius, 2*radius, 0+h, 30); 39 g.fillArc(x, y, 2*radius, 2*radius, 90+h, 30); 40 g.fillArc(x, y, 2*radius, 2*radius, 180+h, 30); 41 g.fillArc(x, y, 2*radius, 2*radius, 270+h, 30); 42 } 43 class TimerListener implements ActionListener 44 { 45 @Override 46 public void actionPerformed(ActionEvent arg0) { 47 // TODO Auto-generated method stub 48 h += 3; 49 repaint(); 50 } 51 } 52 } 53 }
Test_16_12.java
需要注意的: Timer的创建应该放在JP的构造函数中
效果图:下图实际上是动态的
时间: 2024-10-23 21:11:28