gpt4 book ai didi

java - Jung 可视化中的顶点标签颜色

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:05 26 4
gpt4 key购买 nike

我已经与 Jung 创建了一个图表,并且正在研究可视化选项。我正在尝试更改顶点标签的颜色,不关心它们是否被拾取/取消拾取,只是从一开始就如此。

即我想将顶点标签涂成白色,将边缘标签保留为黑色(默认)。

似乎找不到有效的示例,我猜 DefaultVertexLabelRenderer 类应该有帮助,但我似乎无法让它工作。有任何想法吗?目前的代码如下所示:

public static void main(String[] args) {
Graph<Node, Double> g = MakeGraph.makeManual(); //graph created somewhere

Layout<Node, Double> layout = new CircleLayout<Node, Double>(g);
layout.setSize(new Dimension(300,300));
VisualizationViewer<Node, Double> vv = new VisualizationViewer<Node, Double>(layout);
vv.setPreferredSize(new Dimension(500,500));


// Paints the vertex (as a shape)
Transformer<Node,Paint> vertexPaint = new Transformer<Node,Paint>() {
public Paint transform(Node node) {
if(node.getNodeType().equals(NodeType.SLA)) return new Color(228,108,10);//Color.ORANGE;
else return new Color(79,129,189);//Color.BLUE;
}
};

//Wanna do this: paint the label text
Transformer<Node,Paint> vertexLabelPaint = new Transformer<Node,Paint>() {
public Paint transform(Node node) {
return Color.WHITE;
}
};

//Sets the vertex label font
Transformer<Node,Font> vertexFont = new Transformer<Node,Font>() {
public Font transform(Node node) {
Font font = new Font("Calibri", Font.BOLD, 14);
return font;
}
};

//Sets the vertex shape's size, w.r.t. to the size of the text in the label
Transformer<Node,Shape> vertexSize = new Transformer<Node,Shape>(){
public Shape transform(Node node){
Ellipse2D circle = new Ellipse2D.Double(-15, -15, 30, 30);
AffineTransform affinetransform = new AffineTransform();
FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
Font font = new Font("Calibri", Font.BOLD, 14);
int textwidth = (int)(font.getStringBounds(node.toString(), frc).getWidth());

return AffineTransform.getScaleInstance(textwidth/14, 1).createTransformedShape(circle);
}
};


vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.WHITE));
vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint); //fill
vv.getRenderContext().setVertexDrawPaintTransformer(vertexPaint); //outline
vv.getRenderContext().setVertexFontTransformer(vertexFont);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Node>());
//COLOR ME WHITE! ^^
vv.getRenderContext().setVertexShapeTransformer(vertexSize);
vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);

vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Double>());
//LEAVE ME BLACK ^^

DefaultModalGraphMouse<Node, Double> gm = new DefaultModalGraphMouse<Node, Double>();
// gm.setMode(ModalGraphMouse.Mode.TRANSFORMING);
vv.setGraphMouse(gm);

JFrame frame = new JFrame("Test Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);

}}

我试过了

DefaultVertexLabelRenderer dvlr = new DefaultVertexLabelRenderer(Color.WHITE);
dvlr.setForeground(Color.WHITE);
vv.getRenderContext().setVertexLabelRenderer(dvlr);

没有效果。

最佳答案

一种选择是重写返回标签渲染器组件的方法,并相应地设置前景色。在这里,这被实现为 MCVE (当问题中已经提供了它时,可以更轻松地快速尝试并找到解决方案......)

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JComponent;
import javax.swing.JFrame;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
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.renderers.DefaultVertexLabelRenderer;

public class JungVertexLabelColors
{
public static void main(String[] args)
{
JFrame jf = new JFrame();
final Graph<String, String> g = getGraph();
final VisualizationViewer<String, String> vv =
new VisualizationViewer<String, String>(
new FRLayout<String, String>(g));
DefaultModalGraphMouse<Node, Double> graphMouse =
new DefaultModalGraphMouse<Node, Double>();
vv.setGraphMouse(graphMouse);


final Color vertexLabelColor = Color.RED;
DefaultVertexLabelRenderer vertexLabelRenderer =
new DefaultVertexLabelRenderer(vertexLabelColor)
{
@Override
public <V> Component getVertexLabelRendererComponent(
JComponent vv, Object value, Font font,
boolean isSelected, V vertex)
{
super.getVertexLabelRendererComponent(
vv, value, font, isSelected, vertex);
setForeground(vertexLabelColor);
return this;
}
};
vv.getRenderContext().setVertexLabelRenderer(vertexLabelRenderer);
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());



jf.getContentPane().add(vv);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}

public static Graph<String, String> getGraph()
{
Graph<String, String> g = new DirectedSparseGraph<String, String>();
g.addVertex("v0");
g.addVertex("v1");
g.addVertex("v2");
g.addVertex("v3");
g.addVertex("v4");
g.addEdge("e0", "v0", "v1");
g.addEdge("e1", "v1", "v2");
g.addEdge("e2", "v2", "v3");
g.addEdge("e3", "v3", "v4");
g.addEdge("e4", "v4", "v0");
g.addEdge("e5", "v1", "v3");
g.addEdge("e6", "v2", "v4");
return g;
}
}

(确实有点奇怪,似乎没有更简单的方法,但根据 DefaultVertexLabelRenderer 的实现,渲染器本身无法影响默认标签颜色:它只是从 VisualizationViewer 获取前景色。(因此 visualizationViewer.setForeground(Color.RED) 会改变颜色,但这会影响所有标签...))

关于java - Jung 可视化中的顶点标签颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35300510/

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