//指針旋轉繪圖
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class zebra extends JFrame {
int rx = 300, ry = 300; //旋轉中心點
int crd = 133; //圓弧半徑
int arcAng = 10; //圓弧角度
int dAng = 0; //遞增角度
int sumAng = 300; //總共旋轉角度,若設0為一直轉
int startAng = 240; //起始角度
int rAng = 1;//動畫間隔旋轉角度 順:負值 ,逆:正值
boolean chk = false;
int cx,cy;//圓弧中心點
int cd = 10;//圓弧偏移旋轉中心點的距離
int ww = 660, hh = 550;
public static void main(String args[]){
new zebra();
}
public zebra(){
cx = (int)(Math.cos(startAng *(Math.PI / 180)) * (crd + cd));
cy = (int)(Math.sin(startAng * (Math.PI / 180)) * (crd + cd) * -1);
setTitle("Arc & Oval");
setSize(ww,hh);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
chk = !chk;
if (!chk) return;
(new Thread(){
public void run(){
while(chk){
dAng += rAng;
redraw();
if ((sumAng > 0) && (dAng % sumAng == 0)){
chk = false;
}
try{Thread.sleep(10);}catch(Exception e){}
}
}
}).start();
}
});
}
public void redraw(){
repaint();
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.clearRect(0,0,ww,hh);
g2d.translate(rx,ry);
g2d.setColor(Color.red);
g2d.fillOval(-5, -5, 10, 10);
g2d.setColor(Color.blue);
double rr = dAng * (Math.PI / 180);
int cpx= (int)(cx * Math.cos(rr) + cy * Math.sin(rr));
int cpy= (int)(cx * -1 * Math.sin(rr) + cy * Math.cos(rr));
g2d.fillArc(cpx-crd,cpy-crd,crd * 2,crd * 2, dAng + startAng - 180 - (arcAng / 2) ,arcAng);
}
}
此程式是以旋轉中心點為原點,抓角度-120距離133的點作為圓弧中心點
按滑鼠啟動,逆時針旋轉300度就會自動停止~大概是停在你要的位置