java - 在 JgraphX 中旋转边缘标签

标签 java jgraphx

我有一条垂直的边缘,我想相应地旋转边缘标签。

例如,将“helloworld.java”绘图部分替换为:

try
{
    Object v1 = graph.insertVertex(parent, "1", "TopLeft",
        20, 20, 80, 80);
    Object v3 = graph.insertVertex(parent, "2", "BottomLeft",
        20, 240, 80, 80);
    Object e1 = graph.insertEdge(parent, null, "edgelabel", 
        v1, v3, "dashed=true;endArrow=none;rotation=0");
}

设置rotation=90会旋转边缘,但不会旋转标签。 我找到了 mxCurveLabelShape,但我不明白如何使用它。我尝试了一些,但 edge-mxCell 不包含 mxCurve。我尝试过:

 List<mxPoint> pl = ((mxCell)e1).getGeometry().getPoints();
 mxCellState ecs1 = new mxCellState(graph.getView(),e1,null);
 mxCurveLabelShape cls = new mxCurveLabelShape(ecs1,(mxCurve)pl);

这显然无法工作,因为边缘 mxCell 也不包含非空 mxPoints。我可以使用边缘与其末端相连的位置吗? 有没有办法只处理标签并使用旋转来实现?

有什么想法吗?

最佳答案

好吧,我已经弄清楚了,它不太漂亮,而且(还)不适用于正交边缘,即由水平和垂直线段组成的边缘。

我可以让它工作的方法是覆盖一个并为 mxGraphComponent 引入一个函数。也就是说,drawLabel 函数现在检查标签是否来自边缘,如果是,则选择不同的 createTemporaryGraphics 函数。在后者中,边缘框以及标签是已知的,并且在给定边缘框的情况下计算旋转。有一个 boolean 值传递(象限),它告诉边缘是否在矩形的东北和西南之间(quadrant = true),或者在西北和东南之间( >象限=假)。目前,这在位置意义上并不是最佳的(即,垂直标签可能在边缘顶部结束)。但我把调整留给任何想使用它的人;)

开始吧:(将其放置在启动 graphComponent 的任何位置)

mxGraphComponent graphComponent = new mxGraphComponent(graph){
public mxInteractiveCanvas createCanvas()
    {
        return new mxInteractiveCanvas(){
            @Override
            public Object drawLabel(String text, mxCellState state, boolean html)
            {
                Map<String, Object> style = state.getStyle();
                mxIShape shapeL = getShape(style);
                mxITextShape shape = getTextShape(style, html);
                if (g != null && shape != null && drawLabels && text != null
                        && text.length() > 0)
                {
                    // Creates a temporary graphics instance for drawing this shape
                    float opacity = mxUtils.getFloat(style,
                            mxConstants.STYLE_TEXT_OPACITY, 100);
                    Graphics2D previousGraphics = g;
                    if(((mxCell) state.getCell()).isVertex()){
                        g = createTemporaryGraphics(style, opacity, null);                                  
                    }else{
                        //quadrant will be true if the edge is NE or SW
                        Object target = ((mxCell) state.getCell()).getTarget();
                        Object source = ((mxCell) state.getCell()).getSource();
                        boolean quadrant = false;
                        if(((mxCell)target).getGeometry().getCenterX()<((mxCell)source).getGeometry().getCenterX()){
                            if(((mxCell)target).getGeometry().getCenterY()>((mxCell)source).getGeometry().getCenterY()){
                                quadrant =  true;
                            }
                        }
                        if(((mxCell)target).getGeometry().getCenterX()>((mxCell)source).getGeometry().getCenterX()){
                            if(((mxCell)target).getGeometry().getCenterY()<((mxCell)source).getGeometry().getCenterY()){
                                quadrant =  true;
                            }
                        }
                        g = createTemporaryGraphics(style, opacity, state, state.getLabelBounds(),quadrant);
                    }

                    // Draws the label background and border
                    Color bg = mxUtils.getColor(style,
                            mxConstants.STYLE_LABEL_BACKGROUNDCOLOR);
                    Color border = mxUtils.getColor(style,
                            mxConstants.STYLE_LABEL_BORDERCOLOR);
                    paintRectangle(state.getLabelBounds().getRectangle(), bg, border);

                    // Paints the label and restores the graphics object
                    shape.paintShape(this, text, state, style);
                    g.dispose();
                    g = previousGraphics;
                }

                return shape;
            }
            public Graphics2D createTemporaryGraphics(Map<String, Object> style,
                    float opacity, mxRectangle bounds, mxRectangle labelbounds, boolean quad)
            {
                Graphics2D temporaryGraphics = (Graphics2D) g.create();

                // Applies the default translate
                temporaryGraphics.translate(translate.x, translate.y);

                // setup the rotation for the label
                double angle = java.lang.Math.atan(bounds.getHeight()/bounds.getWidth());
                double rotation = Math.toDegrees(angle);
                if(quad){
                    rotation = - rotation;
                }
                //get the translation needed
                double diff = labelbounds.getHeight()*(1-Math.cos(angle));
                double plusy = diff * Math.sin(angle);
                double plusx = diff * Math.cos(angle);
                // Applies the rotation and translation on the graphics object
                if (bounds != null)
                {
                    if (rotation != 0)
                    {
                        temporaryGraphics.rotate(Math.toRadians(rotation),
                                labelbounds.getCenterX(), labelbounds.getCenterY());
                        temporaryGraphics.translate(
                                - plusx, plusy);
                    }
                }

                // Applies the opacity to the graphics object
                if (opacity != 100)
                {
                    temporaryGraphics.setComposite(AlphaComposite.getInstance(
                            AlphaComposite.SRC_OVER, opacity / 100));
                }

                return temporaryGraphics;
            }
        };
    }

};

关于java - 在 JgraphX 中旋转边缘标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35917090/

相关文章:

java - Android ListView : changing dataset and calling notifyDataSetChanged inside of getView

java - Jgraphx样式应用于circleLayout

java - 搜索字符串数组以查找元素的部分

java - ImageIcon引用麻烦

java - box2d - 更稳定的堆栈

java - 组打破了 JGraphX 中的层次布局

java - 使用 JGraphX 删除图中的重复顶点

java - jgraphx 上划线顶点标签

java - 在 jGraphX 图形编辑器中拖放一组单元格

c# - 为什么在 switch case 语句中只允许使用有限的类型