1
我以JUNG開始,我在Jpanel中創建了一個新創建的JFrame圖形,但是我無法在我正在開發的程序的現有面板中繪製相同的圖形。JUNG:在現有的JPanel上繪製圖形
我使用的功能是這樣的:
private void demoGraph() {
// Graph<V, E> where V is the type of the vertices and E is the type of the edges
// Note showing the use of a SparseGraph rather than a SparseMultigraph
Graph<Integer, String> g = new SparseGraph<>();
// Add some vertices. From above we defined these to be type Integer.
g.addVertex((Integer) 1);
g.addVertex((Integer) 2);
g.addVertex((Integer) 3);
// g.addVertex((Integer)1); // note if you add the same object again nothing changes
// Add some edges. From above we defined these to be of type String
// Note that the default is for undirected edges.
g.addEdge("Edge-A", 1, 2); // Note that Java 1.5 auto-boxes primitives
g.addEdge("Edge-B", 2, 3);
// Layout<V, E>, VisualizationComponent<V,E>
Layout<Integer, String> layout = new CircleLayout(g);
layout.setSize(new Dimension(300, 300));
VisualizationViewer<Integer, String> vv = new VisualizationViewer<>(layout);
vv.setPreferredSize(new Dimension(350, 350));
// Setup up a new vertex to paint transformer...
Transformer<Integer, Paint> vertexPaint = new Transformer<Integer, Paint>() {
@Override
public Paint transform(Integer i) {
return Color.GREEN;
}
};
// Set up a new stroke Transformer for the edges
float dash[] = {10.0f};
final Stroke edgeStroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
Transformer<String, Stroke> edgeStrokeTransformer = new Transformer<String, Stroke>() {
@Override
public Stroke transform(String s) {
return edgeStroke;
}
};
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
// paint on a JPanel: NOT WORKING
GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
panel.setSize(this.graphPanel.getWidth(), this.graphPanel.getHeight());
BorderLayout panelMapLayout = new BorderLayout();
panel.setLayout(panelMapLayout);
panel.setVisible(true);
this.graphPanel.add(panel);
this.graphPanel.revalidate();
this.graphPanel.repaint();
// Paint on a new JFrame: perfectly working
JFrame frame = new JFrame("Simple Graph View 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
這裏的一些指導大加讚賞。
你沒有描述你的實際問題」重新擁有。你在看什麼,你在期待什麼? –
對不起,我需要在JPanel中顯示一個JPanel中的圖形,而不是在新的JFrame中,像所有示例顯示的那樣 – user721807
我通常理解你想要做什麼。什麼是不明確的是什麼是失敗。你所說的只是「無法繪畫」。無論如何,這些示例使用的JFrame沒有什麼特別之處,所以我預計可能會有些微妙的關於如何設置它。我建議儘可能使您的示例儘可能簡單 - 並儘可能與工作示例相似 - 然後查看是否可以找出差異。 –