1 package demo; 2 3 import java.awt.Color; 4 import java.awt.Graphics; 5 6 import javax.swing.JFrame; 7 import javax.swing.JPanel; 8 9 /**10 * Java 动态太极图 DynamicTaiChi (整理)11 *12 * 2016-1-2 深圳 南山平山村 曾剑锋13 */14 15 public class DynamicTaiChi extends JPanel{16 private static final long serialVersionUID = 1L;17 /*18 * 所有的数据都是按比例依照centerX进行改变的,所以19 * 如果想要更改面板大小的话,可以直接改centerX的值.20 */21 static int centerX = 600/2;22 static int centerY = centerX;23 static int cicle = centerX;24 static int angle = 0;25 26 public DynamicTaiChi() {27 start();28 }29 private void start() {30 new Thread(new Runnable() {31 32 @Override33 public void run() {34 while (true) {35 try {36 angle++;37 Thread.sleep(10);38 repaint();39 } catch (Exception e) {40 e.printStackTrace();41 }42 }43 44 }45 }).start();46 }47 @Override48 public void paint(Graphics graphics) {49 super.paint(graphics);50 this.setBackground(Color.red);51 graphics.setColor(Color.white);52 graphics.fillArc(centerX-cicle/2, centerY-cicle/2, cicle, cicle, angle, 180);53 graphics.setColor(Color.black);54 graphics.fillArc(centerX-cicle/2, centerY-cicle/2, cicle, cicle, 180+angle, 180);55 graphics.fillArc(centerX+(int)(cicle/2/2*(Math.cos(angle*Math.PI/180)))-cicle/2/2, centerY-(int)(cicle/2/2*(Math.sin(angle*Math.PI/180)))-cicle/2/2, cicle/2, cicle/2, 0, 360);56 graphics.setColor(Color.white);57 graphics.fillArc(centerX+(int)(cicle/2/2*(Math.cos((angle+180)*Math.PI/180)))-cicle/2/2, centerY-(int)(cicle/2/2*(Math.sin((angle+180)*Math.PI/180)))-cicle/2/2, cicle/2, cicle/2, 0, 360);58 graphics.setColor(Color.black);59 graphics.fillArc(centerX+(int)(cicle/2/2*(Math.cos((angle+180)*Math.PI/180)))-cicle/2/2/2, centerY-(int)(cicle/2/2*(Math.sin((angle+180)*Math.PI/180)))-cicle/2/2/2, cicle/2/2, cicle/2/2, 0, 360);60 graphics.setColor(Color.white);61 graphics.fillArc(centerX+(int)(cicle/2/2*(Math.cos(angle*Math.PI/180)))-cicle/2/2/2, centerY-(int)(cicle/2/2*(Math.sin(angle*Math.PI/180)))-cicle/2/2/2, cicle/2/2, cicle/2/2, 0, 360);62 }63 64 public static void main(String[] args) {65 JFrame jFrame = new JFrame();66 jFrame.setTitle("DynamicTaiChi");67 jFrame.setSize(centerX*2, centerY*2);68 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);69 jFrame.setLocationRelativeTo(null);70 71 DynamicTaiChi jPanel = new DynamicTaiChi();72 jFrame.add(jPanel);73 74 jFrame.setVisible(true);75 }76 }