gpt4 book ai didi

java - 荣格用值给顶点着色

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:52:00 25 4
gpt4 key购买 nike

我现在被 Java 库 Jung 困住了。

我显示了顶点和边,只是我找不到任何我需要的顶点着色函数,而不是鼠标。

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.functors.ConstantTransformer;

public class Design {
public Graph<String, Integer> g;
public Design() {

this.g = ProjectTools.readNet("SmallTown.net");
}

public static <Paint> void main(String[] args) {
Design sgv = new Design();
Layout<Integer, String> layout = new FRLayout(sgv.g);
layout.setSize(new Dimension(800,800));
BasicVisualizationServer<Integer, String> vv =
new BasicVisualizationServer<Integer, String>(layout);

Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() {
public Paint transform(Integer i) {
return (Paint) Color.GREEN;
}
};

vv.setPreferredSize(new Dimension(850,850));
vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.green));
vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.white));
vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2.5f)));

vv.getRenderContext().setVertexFillPaintTransformer((Transformer<Integer, java.awt.Paint>) vertexPaint);
vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer<Integer>(vv.getPickedVertexState(), Color.green, Color.yellow));

vv.setBackground(Color.gray);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Integer>());
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);


JFrame frame = new JFrame("Projet Algo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
}

我的渲染: Vertices and edges

最佳答案

我可以看到两个可能的问题;您正在调用 setVertexFillPaintTransformer 两次并且 BasicVisualizationServer 似乎不喜欢 DefaultModalGraphMouse

试试这个版本:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.functors.ConstantTransformer;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.picking.PickedInfo;
import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class Design {
public Graph<Integer, String> g;
public Design() {

this.g = ProjectTools.readNet("SmallTown.net");
}

private static class VertexPaintTransformer implements Transformer<Integer,Paint> {

private final PickedInfo<Integer> pi;

VertexPaintTransformer ( PickedInfo<Integer> pi ) {
super();
if (pi == null)
throw new IllegalArgumentException("PickedInfo instance must be non-null");
this.pi = pi;
}

@Override
public Paint transform(Integer i) {
Color p = null;
//Edit here to set the colours as reqired by your solution
if ( i % 2 == 0)
p = Color.GREEN;
else
p = Color.RED;
//Remove if a selected colour is not required
if ( pi.isPicked(i)){
p = Color.yellow;
}
return p;
}
}

public static <Paint> void main(String[] args) {
Design sgv = new Design();
Layout<Integer, String> layout = new FRLayout(sgv.g);
layout.setSize(new Dimension(800,800));
VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(layout);

Transformer<Integer,Paint> vertexPaint = new Transformer<Integer,Paint>() {

@Override
public Paint transform(Integer i) {
if ( i % 2 == 0)
return (Paint) Color.GREEN;
else
return (Paint) Color.RED;
}
};

vv.setPreferredSize(new Dimension(850,850));
vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.green));
vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.white));
vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2.5f)));

vv.getRenderContext().setVertexFillPaintTransformer(new VertexPaintTransformer(vv.getPickedVertexState()));

DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();
graphMouse.setMode(edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode.PICKING);
vv.setGraphMouse(graphMouse);

vv.setBackground(Color.gray);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Integer>());
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);


JFrame frame = new JFrame("Projet Algo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
}
}

我已经用支持选择的新版本 VertexPaintTransformer 替换了你的 Transformer,删除了 setVertexFillPaintTransformer 的第二次使用,并让你从BasicVisualizationServerVisualizationViewer 这样我就可以使用拾取 GraphMouse

enter image description here

我不得不模拟你的 ProjectTools#readNet() 方法,但我希望你能看到奇数、偶数和选定的颜色。

您现在可以根据需要修改VertexPaintTransformer#transform()

关于java - 荣格用值给顶点着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12897525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com