荣格可视化中的顶点标签颜色
Vertex label color in Jung visualization
我已经用 Jung 创建了一个图表,我正在研究可视化选项。我正在尝试更改顶点标签的颜色,不关心它们是否是 picked/unpicked,只是从一开始就这样。
即我想将顶点标签涂成白色,将边标签涂成黑色(默认)。
似乎找不到有效的示例,我猜 DefaultVertexLabelRenderer class 应该有帮助,但我似乎无法让它工作。有任何想法吗?目前的代码如下所示:
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);
无果。
一个选项是覆盖 returns 标签渲染器组件的方法,并相应地设置前景色。这里这是作为 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)
会改变颜色,但这会影响 all 标签...))
我已经用 Jung 创建了一个图表,我正在研究可视化选项。我正在尝试更改顶点标签的颜色,不关心它们是否是 picked/unpicked,只是从一开始就这样。
即我想将顶点标签涂成白色,将边标签涂成黑色(默认)。
似乎找不到有效的示例,我猜 DefaultVertexLabelRenderer class 应该有帮助,但我似乎无法让它工作。有任何想法吗?目前的代码如下所示:
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);
无果。
一个选项是覆盖 returns 标签渲染器组件的方法,并相应地设置前景色。这里这是作为 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)
会改变颜色,但这会影响 all 标签...))