class AutoResizeVisualizer:
幾何学的オブジェクトをさっくりそれなりに可視化するために作った Java のクラス。
座標値が大きくても小さくても自動で大きさを調整する。
指定したサイズの画像を出力可能。
ソースコード
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
// 自動拡縮描画機: データヲ手軽ニ可視化スル
public class AutoResizeVisualizer {
private ArrayList<Shape> shapes = new ArrayList<Shape>();
private ArrayList<Color> colors = new ArrayList<Color>();
private Area allshapes = new Area();
private ColorCounter cc = new ColorCounter();
public static void main(String[] args) throws IOException {
// for sample
AutoResizeVisualizer arv = new AutoResizeVisualizer();
// イロイロナデータヲ入レテミル
arv.add(new Point2D.Double(0, 0));
arv.add(new Point2D.Double(10, 10));
arv.add(new Point2D.Double(20, 20));
arv.add(new Line2D.Double(-100, -200, 300, 400));
arv.add(new Line2D.Double(123, 456, 543, 210));
arv.add(new Rectangle2D.Double(100, 200, 300, 400));
arv.add(new Rectangle2D.Double(18, 152, 229, 291));
arv.add(new Polygon(
new int[]{123, 345, 567, 432, 210},
new int[]{135, 246, 333, -100, -200}, 5));
arv.add(new Polygon(
new int[]{0, 100, -100},
new int[]{0, 100, 100}, 3));
arv.add(new Ellipse2D.Double(-147, -258, 369, 159));
arv.add(new Ellipse2D.Double(-50, -50, 100, 100));
// 画像ヲ出力スル
arv.visualize(300, 300, "20081206_arv.png");
}
public void add(Shape s, Color c) {
Area a = new Area(s);
if (a.isEmpty()) { // for Line2D
// Line2Dナドノ閉ジテイナイオブジェクト対策
BasicStroke bs = new BasicStroke();
s = bs.createStrokedShape(s);
a = new Area(s);
}
shapes.add(s);
colors.add(c);
allshapes.add(a);
}
public void add(Point2D p, Color c) {
add(new Ellipse2D.Double(p.getX(), p.getY(), 1, 1), c);
}
public void add(Shape s) {
add(s, cc.next());
}
public void add(Point2D p) {
add(p, cc.next());
}
public void visualize(int w, int h, String outputFilePath)
throws IOException {
BufferedImage bi = visualize(w, h);
ImageIO.write(bi, "png", new File(outputFilePath));
}
public BufferedImage visualize(int w, int h) {
BufferedImage bi =
new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
// g.setTransform(getTransform(w, h));
AffineTransform at = getTransform(w, h);
for (int i = 0; i < shapes.size(); i++) {
g.setColor(colors.get(i));
// Graphics2DニセットシタAffineTransformト挙動ガコトナル怪奇。
Shape s = at.createTransformedShape(shapes.get(i));
g.fill(s);
g.draw(s); // 細スギル線ガ消エテシマワナイヨウニdrawモスル
}
g.dispose();
return bi;
}
private AffineTransform getTransform(int w, int h) {
Rectangle2D b = allshapes.getBounds2D();
double sx = w / b.getWidth();
double sy = h / b.getHeight();
// 縦横方向ノ拡大率ヲ同ジニスル
// 縦横比ヲ変エタイ場合ッテアルノカ?
sx = Math.min(sx, sy);
sy = sx;
double tx = -b.getMinX();
double ty = -b.getMinY();
// 移動シテカラ拡大縮小
AffineTransform at = new AffineTransform();
at.scale(sx, sy);
at.translate(tx, ty);
return at;
}
public static Color createTransparentColor(Color c, int a) {
return new Color(c.getRed(), c.getGreen(), c.getBlue(), a);
}
private static class ColorCounter {
private static final Color[] COLOR_LIST = {
createTransparentColor(Color.BLUE, 100), // 0000ff
createTransparentColor(Color.CYAN, 100), // 00ffff
createTransparentColor(Color.DARK_GRAY, 100), // 404040
createTransparentColor(Color.GRAY, 100), // 808080
createTransparentColor(Color.GREEN, 100), // 00ff00
createTransparentColor(Color.LIGHT_GRAY, 100), // c0c0c0
createTransparentColor(Color.MAGENTA, 100), // ff00ff
createTransparentColor(Color.ORANGE, 100), // ffc800
createTransparentColor(Color.PINK, 100), // ffafaf
createTransparentColor(Color.RED, 100), // ff0000
createTransparentColor(Color.YELLOW, 100), // ffff00
};
private int counter = -1;
private Color next() {
counter++;
return COLOR_LIST[counter % COLOR_LIST.length];
}
}
}
サンプル出力画像
tags: zlashdot Java Java
Posted by NI-Lab. (@nilab)