/*----------------- ex13_1.java 図形:★の回転(アニメ版) ------------------*/ /* */ import java.applet.*; import java.awt.*; public class ex13_1 extends Applet implements Runnable{ Thread thd = null; Color col; int x[] = {100, 200, 120, 150, 180}; int y[] = {200, 200, 270, 170, 270}; int pt = 5, n = 70, acc, red, grn, blu; double theta = Math.PI / n * 4; // for文n回の繰り返しで一回転するように角度設定(単位:ラジアン) public void init() { // 初期化処理 red = 0; grn = 0; blu = 255; col = new Color(red, grn, blu); thd = new Thread(this); thd.start(); } public void update(Graphics g) { // 再描画で背景クリアをしない paint(g); } public void paint(Graphics g) { // 描画処理 g.setColor(col); g.drawPolygon(x, y, pt); } public void run() { // 実行処理 for (acc = 1; acc < n; acc++) { rotate(); red += 255/n; grn += 0/n; blu -= 255/n; col = new Color(red, grn, blu); repaint(); try { thd.sleep(500-7*acc); } catch (InterruptedException e) { } } } // 点(x,y)をangleラジアン回転 public void rotate() { // 注:y回転時,xは回転後の座標位置より,内側に縮むように回転 for (int j = 0; j < pt; j++) { x[j] -= 150; y[j] -= 120; // 原点中心になるように平行移動 x[j] = (int)(x[j] * Math.cos(theta) - y[j] * Math.sin(theta)); // x座標の回転 y[j] = (int)(x[j] * Math.sin(theta) + y[j] * Math.cos(theta)); // y座標の回転 x[j] += 150; y[j] += 120; // 平行移動分を戻す } } }