wikipedia apiを使ってみたい。- JGraphで表示 その2
2008/05/04
java
jgraph
wikipediaapi
表示をかえてみました。対象語を中心に、関連後が円上にひろがってさらにそこから、ひろがった語から、関連する語がひろがるっという感じかな。
GetThesaurusDS4NetworkViewを使っています。
package util;
import static util.WikipedialabUtils.getThesaurusDS4NetworkView;
import static util.WikipedialabUtils.getTopCandidateIDFromKeyword;
import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.xml.sax.SAXException;
import util.WikipedialabUtils.MNode;
public class TestT {
static JGraph graph() {
GraphModel model = new DefaultGraphModel();
JGraph graph = new JGraph(model);
graph.getGraphLayoutCache().setFactory(new DefaultCellViewFactory());
// Control-drag should clone selection
graph.setCloneable(true);
// Enable edit without final RETURN keystroke
graph.setInvokesStopCellEditing(true);
// When over a cell, jump to its default port (we only have one, anyway)
graph.setJumpToDefaultPort(true);
return graph;
}
static DefaultGraphCell createDefaultGraphCell(String name, int px, int py, int w, int h, Color color){
DefaultGraphCell cell = new DefaultGraphCell(name);
GraphConstants.setBounds(cell.getAttributes(),
new Rectangle2D.Double(px,py,w,h));
GraphConstants.setGradientColor(cell.getAttributes(), color);
GraphConstants.setOpaque(cell.getAttributes(), true);
cell.addPort();
return cell;
}
static Point[] points(int n, final int cx, final int cy, final int R) {
final double d = 360.;
final double d2 = d / n;
Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
int x = (int) (cx + (R * Math.sin(Math.toRadians((d2 * i)))));
int y = (int) (cy + (R * Math.cos(Math.toRadians((d2 * i)))));
points[i] = new Point(x, y);
}
return points;
}
static void c(JGraph graph, MNode parent, DefaultGraphCell cellParent, int cx, int cy, int R, List<DefaultGraphCell> list, Color color) {
int n = parent.child.size();
Collection<MNode> collection = parent.child.values();
list.add(cellParent);
int i = 0;
Point[] points = points(n, cx, cy, R);
for (MNode node : collection) {
Point point = points[i++];
DefaultGraphCell cell = createDefaultGraphCell(node.name, point.x, point.y, 80, 20, color);
c(graph, node, cell, point.x, point.y, R - 100, list, Color.lightGray);
list.add(cell);
DefaultEdge edge = new DefaultEdge();
edge.setSource(cellParent.getChildAt(0));
edge.setTarget(cell.getChildAt(0));
int arrow = GraphConstants.ARROW_NONE;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
list.add(edge);
}
}
static void e(JGraph graph, MNode parent){
int cx = 500;
int cy = 500;
int R = 200;
List<DefaultGraphCell> list = new ArrayList<DefaultGraphCell>();
DefaultGraphCell cellParent = createDefaultGraphCell(parent.name, cx, cy, 80, 20, Color.orange);
c(graph, parent, cellParent, cx, cy, R, list, Color.orange);
graph.getGraphLayoutCache().insert(list.toArray());
}
public static void main(String[] args) throws UnsupportedEncodingException, XPathExpressionException, SAXException, IOException, ParserConfigurationException {
JGraph graph = graph();
String skey = "apple";
int i = getTopCandidateIDFromKeyword(WikipedialabUtils.JAPANESE, skey);
String s = getThesaurusDS4NetworkView(WikipedialabUtils.JAPANESE, i);
MNode node = WikipedialabUtils.createDS4NetworkView(skey, i, s);
e(graph, node);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(graph));
frame.pack();
frame.setVisible(true);
}
}
import static util.WikipedialabUtils.getThesaurusDS4NetworkView;
import static util.WikipedialabUtils.getTopCandidateIDFromKeyword;
import java.awt.Color;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphModel;
import org.xml.sax.SAXException;
import util.WikipedialabUtils.MNode;
public class TestT {
static JGraph graph() {
GraphModel model = new DefaultGraphModel();
JGraph graph = new JGraph(model);
graph.getGraphLayoutCache().setFactory(new DefaultCellViewFactory());
// Control-drag should clone selection
graph.setCloneable(true);
// Enable edit without final RETURN keystroke
graph.setInvokesStopCellEditing(true);
// When over a cell, jump to its default port (we only have one, anyway)
graph.setJumpToDefaultPort(true);
return graph;
}
static DefaultGraphCell createDefaultGraphCell(String name, int px, int py, int w, int h, Color color){
DefaultGraphCell cell = new DefaultGraphCell(name);
GraphConstants.setBounds(cell.getAttributes(),
new Rectangle2D.Double(px,py,w,h));
GraphConstants.setGradientColor(cell.getAttributes(), color);
GraphConstants.setOpaque(cell.getAttributes(), true);
cell.addPort();
return cell;
}
static Point[] points(int n, final int cx, final int cy, final int R) {
final double d = 360.;
final double d2 = d / n;
Point[] points = new Point[n];
for (int i = 0; i < n; i++) {
int x = (int) (cx + (R * Math.sin(Math.toRadians((d2 * i)))));
int y = (int) (cy + (R * Math.cos(Math.toRadians((d2 * i)))));
points[i] = new Point(x, y);
}
return points;
}
static void c(JGraph graph, MNode parent, DefaultGraphCell cellParent, int cx, int cy, int R, List<DefaultGraphCell> list, Color color) {
int n = parent.child.size();
Collection<MNode> collection = parent.child.values();
list.add(cellParent);
int i = 0;
Point[] points = points(n, cx, cy, R);
for (MNode node : collection) {
Point point = points[i++];
DefaultGraphCell cell = createDefaultGraphCell(node.name, point.x, point.y, 80, 20, color);
c(graph, node, cell, point.x, point.y, R - 100, list, Color.lightGray);
list.add(cell);
DefaultEdge edge = new DefaultEdge();
edge.setSource(cellParent.getChildAt(0));
edge.setTarget(cell.getChildAt(0));
int arrow = GraphConstants.ARROW_NONE;
GraphConstants.setLineEnd(edge.getAttributes(), arrow);
GraphConstants.setEndFill(edge.getAttributes(), true);
list.add(edge);
}
}
static void e(JGraph graph, MNode parent){
int cx = 500;
int cy = 500;
int R = 200;
List<DefaultGraphCell> list = new ArrayList<DefaultGraphCell>();
DefaultGraphCell cellParent = createDefaultGraphCell(parent.name, cx, cy, 80, 20, Color.orange);
c(graph, parent, cellParent, cx, cy, R, list, Color.orange);
graph.getGraphLayoutCache().insert(list.toArray());
}
public static void main(String[] args) throws UnsupportedEncodingException, XPathExpressionException, SAXException, IOException, ParserConfigurationException {
JGraph graph = graph();
String skey = "apple";
int i = getTopCandidateIDFromKeyword(WikipedialabUtils.JAPANESE, skey);
String s = getThesaurusDS4NetworkView(WikipedialabUtils.JAPANESE, i);
MNode node = WikipedialabUtils.createDS4NetworkView(skey, i, s);
e(graph, node);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(graph));
frame.pack();
frame.setVisible(true);
}
}
: