[java]ダーツ盤っぽいのを描いてみる。 2009/12/31

Javaです。ダーツ盤っぽいのを描いてみます。



package ggg;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;

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

public class TestA3 {

/**
* 中心座標から、指定された半径の円を描くx, y, w, hを求める。
*/
static Rectangle a(int cx, int cy, int r) {

int x = cx - r;
int y = cy - r;
int w = r + r;
int h = w;

return new Rectangle(x, y, w, h);
}

static GeneralPath b(/* 中心点 */int cx, /* 中心点 */int cy, int r, /* 扇の幅 */
int d_size,
/* 描く角度( から)*/ int startA,
int degree) {
Rectangle in_rec = a(cx, cy, r);
Rectangle out_rec = a(cx, cy, r + d_size);

Arc2D.Double out_arc = new Arc2D.Double(out_rec.x, out_rec.y,
out_rec.width, out_rec.height, startA, degree, Arc2D.OPEN);
Arc2D.Double in_arc = new Arc2D.Double(in_rec.x, in_rec.y,
in_rec.width, in_rec.height, startA + degree, degree * -1,
Arc2D.OPEN);

GeneralPath path = new GeneralPath();

path.append(out_arc, true);
path.lineTo(in_arc.getStartPoint().getX(), in_arc.getStartPoint()
.getY());
path.append(in_arc, true);
path.lineTo(out_arc.getStartPoint().getX(), out_arc.getStartPoint()
.getY());

return path;
}

static void p(Graphics2D g2d, int r, int w, int pos, Color[] cs) {
int pp = 360 / 20;
GeneralPath path = b(250, 250, r, w, /* スタート角度 */pp * pos, /* 角度 */pp);
if ((pos % 2) == 0) {
g2d.setColor(cs[0]);
} else {
g2d.setColor(cs[1]);
}
g2d.fill(path);

}

static class MyFrame extends JFrame {
{
getContentPane().add(new JPanel() {

public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D) g;

int[] r = { 0, 10, 30, 130, 150, 210 };
int[] w = { 10, 20, 120, 20, 60, 20 };
Color[][] cs = { { Color.BLACK, Color.BLACK },
{ Color.RED, Color.RED },
{ Color.BLACK, Color.YELLOW },
{ Color.RED, Color.GREEN },
{ Color.BLACK, Color.YELLOW },
{ Color.RED, Color.GREEN } };
for (int j = 0; j < r.length; j++)
for (int i = 0; i < 20; i++) {
p(g2d, r[j], w[j], i, cs[j]);
}
}

});
}

}

public static void main(String[] args) {

MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.setLocation(300, 200);
frame.setVisible(true);

}

}

: