2016-06-28 6 views
2

기본적으로 vertices.At의 목록에서 3D undirected 그래프를 플로팅해야합니다. 현재 2D 그래프에서 무향 그래프를 그릴 수 있으며 다음 코드를 사용했습니다.JGraphT 자바 라이브러리를 사용하여 3D 그래프를 만들 수 있습니까?

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Rectangle; 
import java.awt.geom.Rectangle2D; 
import java.util.HashMap; 
import java.util.Map; 

import javax.swing.JApplet; 
import javax.swing.JFrame; 

import org.jgraph.JGraph; 
import org.jgraph.graph.DefaultGraphCell; 
import org.jgraph.graph.GraphConstants; 

import org.jgrapht.ListenableGraph; 
import org.jgrapht.ext.JGraphModelAdapter; 
import org.jgrapht.graph.ListenableDirectedGraph; 
import org.jgrapht.graph.DefaultEdge; 


public class JGraphAdapterDemo extends JApplet { 
private static final Color  DEFAULT_BG_COLOR = Color.decode("#FAFBFF"); 
private static final Dimension DEFAULT_SIZE = new Dimension(530, 320); 

// 
private JGraphModelAdapter m_jgAdapter; 

/** 
* @see java.applet.Applet#init(). 
*/ 
public void init() { 
    // create a JGraphT graph 
    ListenableGraph g = new ListenableDirectedGraph(DefaultEdge.class); 

    // create a visualization using JGraph, via an adapter 
    m_jgAdapter = new JGraphModelAdapter(g); 

    JGraph jgraph = new JGraph(m_jgAdapter); 

    adjustDisplaySettings(jgraph); 
    getContentPane().add(jgraph); 
    resize(DEFAULT_SIZE); 

    // add some sample data (graph manipulated via JGraphT) 
    g.addVertex("v1"); 
    g.addVertex("v2"); 
    g.addVertex("v3"); 
    g.addVertex("v4"); 

    g.addEdge("v1", "v2"); 
    g.addEdge("v2", "v3"); 
    g.addEdge("v3", "v1"); 
    g.addEdge("v4", "v3"); 

    // position vertices nicely within JGraph component 
    positionVertexAt("v1", 50, 50); 
    positionVertexAt("v2", 120, 120); 
    positionVertexAt("v3", 200,200); 
    positionVertexAt("v4", 20, 20); 

    // that's all there is to it!... 
} 


private void adjustDisplaySettings(JGraph jg) { 
    jg.setPreferredSize(DEFAULT_SIZE); 

    Color c  = DEFAULT_BG_COLOR; 
    String colorStr = null; 

    try { 
     colorStr = getParameter("bgcolor"); 
    } 
    catch(Exception e) {} 

    if(colorStr != null) { 
     c = Color.decode(colorStr); 
    } 

    jg.setBackground(c); 
} 


private void positionVertexAt(Object vertex, int x, int y) { 
    DefaultGraphCell cell = m_jgAdapter.getVertexCell(vertex); 
    Map attr = cell.getAttributes(); 
    Rectangle2D b = GraphConstants.getBounds(attr); 

    GraphConstants.setBounds(attr, new Rectangle(x, y, b.OUT_BOTTOM, b.OUT_TOP)); 

    Map cellAttr = new HashMap(); 
    cellAttr.put(cell, attr); 
    m_jgAdapter.edit(cellAttr, null, null, null); 
} 
} 

지금 내가 알고 싶은 것은 그것이 우리가이 코드를 확장하고있다이 아닌 내 작업을 fulfulling에서 나에게 많은 도움이 될 것입니다 무엇을 JGraphT library.Is를 사용하여 3D로 무향 그래프를 그릴 수있다.

답변

0

나는 위의 작업을 성공적으로 구현했습니다. JGraphT 라이브러리를 사용하지는 않았지만 대신 Java 3D 만 사용했습니다. 아래는 자바 3D를 사용하여 만들어진 4 면체의 3D 무향 그래프를 보여주는 코드입니다.

enter image description here

:

import javax.media.j3d.Appearance; 
import javax.media.j3d.BoundingSphere; 
import javax.media.j3d.BranchGroup; 
import javax.media.j3d.GeometryArray; 
import javax.media.j3d.LineStripArray; 
import javax.media.j3d.Shape3D; 
import javax.media.j3d.TransformGroup; 
import javax.vecmath.Point3d; 
import com.sun.j3d.utils.behaviors.mouse.MouseRotate; 
import com.sun.j3d.utils.universe.SimpleUniverse; 


public class test { 

public test(){ 

SimpleUniverse u=new SimpleUniverse(); 

BranchGroup group=new BranchGroup(); 

Point3d coords[] = new Point3d[9]; 

Appearance app=new Appearance(); 

coords[0] = new Point3d(0.5d, 0.5d, 0.5d); 
coords[1] = new Point3d(1.0d, 0.5d, 0.5d); 
coords[2] = new Point3d(0.5d, 0.5d, 0.5d); 
coords[3] = new Point3d(0.5d, 1.0d, 0.5d); 
coords[4] = new Point3d(0.5d, 0.5d, 0.5d); 
coords[5] = new Point3d(0.5d, 0.5d, 1.0d); 
coords[6] = new Point3d(1.0d, 0.5d, 0.5d); 
coords[7] = new Point3d(0.5d, 1.0d, 0.5d); 
coords[8] = new Point3d(0.5d, 0.5d, 1.0d); 

int vertexCounts[] = {9}; 

LineStripArray lines = new LineStripArray(9, 
GeometryArray.COORDINATES, vertexCounts); 

lines.setCoordinates(0, coords); 

Shape3D shape=new Shape3D(lines , app); 

//group.addChild(shape); 

TransformGroup objRotate = new TransformGroup(); 
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
objRotate.addChild(shape); 

MouseRotate f1=new MouseRotate(); 
f1.setSchedulingBounds(new BoundingSphere()); 
f1.setTransformGroup(objRotate); 
group.addChild(f1); 

group.addChild(objRotate); 

u.addBranchGraph(group); 

u.getViewingPlatform().setNominalViewingTransform(); 

} 

} 

이것을 사용하여 구현 면체의 이미지는 아래 주어진

관련 문제