当setLabelsClipped为true时,顶点标签在顶点外部,滚动条和其他组件上绘画 [英] Vertex labels painting outside of vertex, over scrollbars and onto other components when setLabelsClipped is true

查看:158
本文介绍了当setLabelsClipped为true时,顶点标签在顶点外部,滚动条和其他组件上绘画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JGraph已有一段时间,当您将标签裁剪设置为true时,似乎出现绘画问题:

I've been working with JGraph for a while and it appears there is a painting issue when you set label clipping to true:

以下精简示例显示了您可能会遇到的正在运行的应用程序中的问题:

The following boiled-down example shows the problem in a living application that you can mess with:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

/** it's an app! */
public class GraphApp extends JFrame {
    private mxGraph graph;
    private mxGraphComponent graphComponent;
    private boolean labelsClipped = false;

    /** @return the splitpane */
    public JSplitPane getSplitpane() {
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        graph = new mxGraph();
        graph.getModel().beginUpdate();
        graph.removeCells(graph.getChildCells(graph.getDefaultParent(), true, true));
        for (int i = 0; i < 10; i++)
            graph.insertVertex(null, null, "very_very_long_vertex_" + i, 10 * i, 10 * i, 100, 50);
        graph.getModel().endUpdate();
        graph.setLabelsClipped(labelsClipped);
        graphComponent = new mxGraphComponent(graph);
        JTextArea area = new JTextArea("There's overpaint below this text."); //$NON-NLS-1$
        splitPane.add(graphComponent, JSplitPane.LEFT);
        splitPane.add(area, JSplitPane.RIGHT);
        splitPane.setDividerLocation(70);
        return splitPane;
    }

    private JButton getButton() {
        JButton button = new JButton(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                labelsClipped = !labelsClipped;
                graph.setLabelsClipped(labelsClipped);
                GraphApp.this.repaint();
            }
        });
        button.setText("Swap setLabelsClipped");
        return button;
    }

    private JPanel getPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(getSplitpane(), BorderLayout.CENTER);
        panel.add(getButton(), BorderLayout.SOUTH);
        return panel;
    }

    public static void main(String[] args) {
        GraphApp app = new GraphApp();
        app.add(app.getPanel());
        app.setPreferredSize(new Dimension(300, 100));
        app.setVisible(true);
        app.pack();
    }
}

值得注意的是,过度绘制仅在顶点范围内发生.以下是剪下的名称:

It's also interesting to note, that overpaint only happens within the confines of the vertex. Here are the names with clipping off:

并加上以下内容:

我现在正在调查JGraphx源,以查看问题所在.以前有人解决过吗?显然,设置graph.setLabelsClipped(false)可以解决该问题,但是我不希望我的顶点标签溢出到我的顶点范围内.

I'm looking into the JGraphx source now to see where the problem lies. Has anyone worked around this before? Obviously setting graph.setLabelsClipped(false) works around it, but I'd rather not have my vertex labels spill out over the bounds of my vertices.

推荐答案

我找到了问题所在.

从com.mxgraph.view.mxGraph中,如果添加了一点点绘画代码,则可以看到剪辑被错误地设置为一个矩形,如我的示例所示,该矩形可以位于实际图形之外组件.

From com.mxgraph.view.mxGraph, if you add this little bit of painting code, you can see that the clip is incorrectly getting set to a rectangle that can be, as evidenced in my example, outside of the actual graph component.

        if (clippedCanvas instanceof mxGraphics2DCanvas)
        {
            System.out.println("setting new clip");
            Graphics g = ((mxGraphics2DCanvas) clippedCanvas).getGraphics();
            clip = g.getClip();
            g.setClip(newClip);
            ((mxGraphics2DCanvas) clippedCanvas).paintRectangle(((mxGraphics2DCanvas) clippedCanvas).getGraphics().getClipBounds(), Color.GREEN, Color.WHITE);
        }

如果我们绘制标签所使用的剪切区域,就可以看到问题所在.

If we paint the clipping area that the label is working with, we can see where the problem lies.

实际上,我们应该只绘制原始画布和新的裁剪矩形的交集.此图显示了剪切矩形在被新矩形践踏之前的状态:

Realistically, we should only be painting the intersection of the original canvas, and the new clipping rectangle. This image shows what the clipping rectangle was before being trampled by the new one:

此问题的解决方法很简单:

The fix for this is a simple one:

        if (clippedCanvas instanceof mxGraphics2DCanvas)
        {
            Graphics g = ((mxGraphics2DCanvas) clippedCanvas).getGraphics();
            clip = g.getClip();
            if (clip instanceof Rectangle)
            {
                g.setClip(newClip.intersection((Rectangle) clip));
            }
            else
            {
                g.setClip(newClip);
            }
        }

我想知道原始代码是否像故意那样进行了剪辑设置.我对此表示怀疑.

I'd be interested to hear if the original code did the clip setting as it did intentionally. I kind of doubt it.

在某人对其剪辑形状有些古怪的情况下,我的修复程序也默认使用其实现,就像一种CYA.可能不需要,因为不能保证裁剪不能用于除矩形以外的任何其他东西:

My fix also defaults to their implementation in the case that someone does something wacky with their clip shapes, just as a sort of CYA. Probably not needed as clipping isn't guaranteed to work with anything other than rectangles:

代码似乎位于github上,因此希望我可以将修复程序推送到这里:

It appears that the code lives here on github, so hopefully I can get the fix pushed there:

https://github.com/jgraph/jgraphx

这篇关于当setLabelsClipped为true时,顶点标签在顶点外部,滚动条和其他组件上绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆