radial document visualizationっぽいのを描きたかった。
2009/01/05
java
visualization
Javaです。最近、お疲れ気味なので、さらに頭の切れ味が悪いです。
画面をキャプチャ
送信者 プログラマメモ2 |
radial document visualizationというのをみつけてかっこいいなぁと思いました。
ちなみに、radialは放射状の構造という意味だそうです。
ちょっと似た感じの描きたかったのですが。。。
ぜんぜん違うなぁ
package ggg;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestRadial {
/**
* 中心座標から、指定された半径の円を描く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 bbb(Graphics2D g2d, int[] is){
final int CENTER_X = 200, CENTER_Y = 200;
double sum = 0;
for (int i : is) {
sum += i;
}
int pos = 0;
for (int i : is) {
GeneralPath path = b(CENTER_X, CENTER_Y, 80,
60 , pos, (int)(360 * (i/sum)) - 2);
draw(g2d, path, new Color(30,50, 80), Color.BLACK);
pos += (int)(360 * (i/sum)) - 1;
}
pos = 30;
for (int i : is) {
GeneralPath path = b(CENTER_X, CENTER_Y, 50,
30 , pos, (int)(360 * (i/sum)) - 2);
draw(g2d, path, new Color(100,150, 80), Color.BLACK);
pos += (int)(360 * (i/sum)) - 1;
}
}
static void draw(Graphics2D g2d, Shape shape, Color c1, Color c2){
g2d.setColor(c1);
g2d.fill(shape);
g2d.setColor(c2);
g2d.draw(shape);
}
static class MyFrame extends JFrame {
{
getContentPane().add(new JPanel() {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
bbb(g2d, new int[]{3,8,2,3,4,5});
}
});
}
}
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);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestRadial {
/**
* 中心座標から、指定された半径の円を描く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 bbb(Graphics2D g2d, int[] is){
final int CENTER_X = 200, CENTER_Y = 200;
double sum = 0;
for (int i : is) {
sum += i;
}
int pos = 0;
for (int i : is) {
GeneralPath path = b(CENTER_X, CENTER_Y, 80,
60 , pos, (int)(360 * (i/sum)) - 2);
draw(g2d, path, new Color(30,50, 80), Color.BLACK);
pos += (int)(360 * (i/sum)) - 1;
}
pos = 30;
for (int i : is) {
GeneralPath path = b(CENTER_X, CENTER_Y, 50,
30 , pos, (int)(360 * (i/sum)) - 2);
draw(g2d, path, new Color(100,150, 80), Color.BLACK);
pos += (int)(360 * (i/sum)) - 1;
}
}
static void draw(Graphics2D g2d, Shape shape, Color c1, Color c2){
g2d.setColor(c1);
g2d.fill(shape);
g2d.setColor(c2);
g2d.draw(shape);
}
static class MyFrame extends JFrame {
{
getContentPane().add(new JPanel() {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
bbb(g2d, new int[]{3,8,2,3,4,5});
}
});
}
}
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);
}
}
: