KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > util > tree > TreeVisualizer


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.util.tree;
11
12 //
13
// TreePrinter.java
14
// MetaEvolve
15
//
16
// Version 1
17
//
18
// Created by Brian Risk of Geneffects on March 15, 2004.
19
// Last Modified on March 19, 2004.
20
// www.geneffects.com
21
//
22

23 import java.io.*;
24 import java.awt.*;
25 import java.awt.image.*;
26 import javax.imageio.*;
27 import javax.swing.tree.*;
28
29 public class TreeVisualizer {
30   /** String containing the CVS revision. Read out via reflection!*/
31   private final static String JavaDoc CVS_REVISION = "$Revision: 1.1 $";
32
33   private int side = 512; //the height and width of the image
34

35   private double circleDiminishFactor = 0.875;
36
37   private double branchStartWidth = 16.0;
38
39   private double branchDiminshFactor = 0.6666;
40
41   private TreeBranchRenderer tbr = null;
42
43   private TreeNodeRenderer tnr = null;
44
45   private Color bkgndColor = Color.white;
46
47   private Color arenaColor = Color.black;
48
49   private Color branchColor = Color.white;
50
51   private Color nodeColor = Color.red;
52
53   private boolean renderNodes = true;
54
55   private int ignorePastLevel = -1; //if set to less than 0, all levels are drawn
56

57   public TreeVisualizer() {}
58
59   //TreeNode is an interface found under java.swing.tree.*
60
public BufferedImage renderTree(TreeNode tn) {
61     BufferedImage bufferedImage = new BufferedImage(side, side,
62         BufferedImage.TYPE_INT_RGB);
63     // Create a graphics contents on the buffered image
64
Graphics2D g2d = bufferedImage.createGraphics();
65     // Making the graphic object draw anti-aliased shapes
66
g2d.addRenderingHints(new RenderingHints(
67         RenderingHints.KEY_ANTIALIASING,
68         RenderingHints.VALUE_ANTIALIAS_ON));
69     g2d.setColor(bkgndColor);
70     g2d.fillRect(0, 0, side, side);
71     g2d.setColor(arenaColor);
72     g2d.fillOval(0, 0, side, side);
73     g2d.setColor(branchColor);
74     drawBranches(g2d, tn, 0, 0.0, 2.0 * Math.PI);
75     if (renderNodes) {
76       g2d.setColor(nodeColor);
77       drawNodes(g2d, tn, 0, 0.0, 2.0 * Math.PI);
78     }
79     g2d.dispose();
80     return bufferedImage;
81   }
82
83   public void writeImageFile(RenderedImage ri, File f) {
84     try {
85       ImageIO.write(ri, "png", f);
86     } catch (IOException e) {
87       e.printStackTrace();
88     }
89   }
90
91   private Point drawBranches(Graphics2D g, TreeNode tn, int level, double start,
92                              double finish) {
93     double span = finish - start;
94     double middle = start + (span / 2.0);
95     Point middlePoint = radToCart(getR(level), middle, side / 2, side / 2);
96     Stroke strokeSize = getStroke(level);
97     if (!tn.isLeaf()) {
98       if (ignorePastLevel >= 0) {
99         if (ignorePastLevel < level + 1) {
100           return middlePoint;
101         }
102       }
103       double subSection = span / (double) tn.getChildCount();
104       double s1 = start;
105       double s2 = start + subSection;
106       for (int i = 0; i < tn.getChildCount(); i++) {
107         TreeNode tn2 = tn.getChildAt(i);
108         Point connectPoint = drawBranches(g, tn2, level + 1, s1, s2);
109         g.setStroke(strokeSize);
110         if (tbr != null) {
111           Color nc = tbr.getBranchColor(tn, level);
112           if (nc != null) {
113             g.setColor(nc);
114           }
115         }
116         g.drawLine( (int) middlePoint.getX(), (int) middlePoint.getY(),
117                    (int) connectPoint.getX(), (int) connectPoint.getY());
118         s1 += subSection;
119         s2 += subSection;
120       }
121     }
122     return middlePoint;
123   }
124
125   private void drawNodes(Graphics2D g, TreeNode tn, int level, double start,
126                          double finish) {
127     double span = finish - start;
128     double middle = start + (span / 2.0);
129     Point middlePoint = radToCart(getR(level), middle, side / 2, side / 2);
130     double x = middlePoint.getX();
131     double y = middlePoint.getY();
132     g.setStroke(new BasicStroke(0));
133     double r = branchStartWidth * Math.pow(branchDiminshFactor, level);
134     if ( (int) (2 * r) > 0) {
135       if (tnr != null) {
136         Color nc = tnr.getNodeColor(tn, level);
137         if (nc != null) {
138           g.setColor(nc);
139         }
140       }
141       g.fillOval( (int) (x - r), (int) (y - r), (int) (2 * r), (int) (2 * r));
142     }
143     if (!tn.isLeaf()) {
144       if (ignorePastLevel >= 0) {
145         if (ignorePastLevel < level + 1) {
146           return;
147         }
148       }
149       double subSection = span / (double) tn.getChildCount();
150       double s1 = start;
151       double s2 = start + subSection;
152       for (int i = 0; i < tn.getChildCount(); i++) {
153         TreeNode tn2 = tn.getChildAt(i);
154         drawNodes(g, tn2, level + 1, s1, s2);
155         s1 += subSection;
156         s2 += subSection;
157       }
158     }
159   }
160
161   private Stroke getStroke(int level) {
162     return new BasicStroke( (float) (branchStartWidth *
163                                      Math.pow(branchDiminshFactor, level)),
164                            BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
165   }
166
167   //x and y are the origin, the pixel location of the center of the coordinate system
168
private Point radToCart(double r, double theta, int x, int y) {
169     theta += Math.PI / 2.0;
170     int nx = (int) (r * Math.cos(theta)) + x;
171     int ny = (int) (r * Math.sin(theta)) + y;
172     return new Point(nx, ny);
173   }
174
175   private double getR(int level) {
176     double r = (double) (side / 2) -
177         ( (double) (side / 2) * Math.pow(circleDiminishFactor, level));
178     return r;
179   }
180
181   //mutating and accessing methods
182
public void setSide(int s) {
183     side = s;
184   }
185
186   public int getSide() {
187     return side;
188   }
189
190   public void setCircleDiminishFactor(double c) {
191     circleDiminishFactor = c;
192   }
193
194   public double getCircleDiminishFactor() {
195     return circleDiminishFactor;
196   }
197
198   public void setBranchStartWidth(double b) {
199     branchStartWidth = b;
200   }
201
202   public double getBranchStartWidth() {
203     return branchStartWidth;
204   }
205
206   public void setBranchDiminishFactor(double s) {
207     branchDiminshFactor = s;
208   }
209
210   public double getBranchDiminshFactor() {
211     return branchDiminshFactor;
212   }
213
214   public void setBkgndColor(Color c) {
215     bkgndColor = c;
216   }
217
218   public Color getBkgndColor() {
219     return bkgndColor;
220   }
221
222   public void setArenaColor(Color c) {
223     arenaColor = c;
224   }
225
226   public Color getArenaColor() {
227     return arenaColor;
228   }
229
230   public void setBranchColor(Color c) {
231     branchColor = c;
232   }
233
234   public Color getBranchColor() {
235     return branchColor;
236   }
237
238   public void setNodeColor(Color c) {
239     nodeColor = c;
240   }
241
242   public Color getNodeColor() {
243     return nodeColor;
244   }
245
246   public void setTreeBranchRenderer(TreeBranchRenderer ntbr) {
247     tbr = ntbr;
248   }
249
250   public TreeBranchRenderer getTreeBranchRenderer() {
251     return tbr;
252   }
253
254   public void setTreeNodeRenderer(TreeNodeRenderer ntnr) {
255     tnr = ntnr;
256   }
257
258   public TreeNodeRenderer getTreeNodeRenderer() {
259     return tnr;
260   }
261
262   public void setRenderNodes(boolean r) {
263     renderNodes = r;
264   }
265
266   public boolean getRenderNodes() {
267     return renderNodes;
268   }
269
270   public void setIgnorePastLevel(int i) {
271     ignorePastLevel = i;
272   }
273
274   public int getIgnorePastLevel() {
275     return ignorePastLevel;
276   }
277 }
278
Popular Tags