プログラマメモ2 - programmer no memo2

角度の練習 2008/09/03

回転の練習です。

参考にしたのはJavaによる図形処理入門 - 1998 - プログラマの本棚の時計のプログラムです。




package t004a;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class TestKAITEN {

public static void main(String[] args) {

final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setBackground(new Color(200, 200, 170, 190));
final JPanel panel = new JPanel() {

@Override
protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
Graphics2D g2 = (Graphics2D) g;
g2.clearRect(0, 0, getWidth(), getHeight());
g2.setColor(Color.BLACK);

g2.drawLine(w / 2, 0, w / 2, h);
g2.drawLine(0, h / 2, w, h / 2);

float dash[] = { 10.0f, 3.0f };
final BasicStroke dashStroke = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 3.0f,
dash, 0.0f);
final BasicStroke stroke = new BasicStroke(1.f);
int x;
int y;
int ang = 60;
final double RAD = Math.PI / 180d;//radian
int r = 200;// 半径

Point center = new Point(w / 2, h / 2);// 中心点
for (int i = 1; i <= 12; i++) {
x = (int) ((r) * Math.cos(RAD * ang) + (w / 2));
y = (int) ((r) * Math.sin(RAD * ang) + (h / 2));
g2.drawString("" + ang, x, y);

g2.setStroke(dashStroke);
// 線が重ならないようにする
if ((ang % 90) != 0)
g2.drawLine(center.x, center.y, x, y);
ang -= 30;
}
g2.setStroke(stroke);
g2.drawArc(center.x - r, center.y - r , r*2, r*2, 0, 360);

}

};

frame.getContentPane().add(panel);
frame.setLocation(100, 200);
frame.setSize(500, 500);
frame.setVisible(true);
}

}

: