本文介绍了在具有子面板的 jPanel 上绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 jPanel,其中包含许多可以拖动的子面板.我想要做的是绘制将其中一些子面板连接在一起的线条.

I have a jPanel, which contains a number of sub-panels that can be dragged around. What I want to do, is to draw lines connecting some of those sub-panels together.

然而,虽然这看起来应该很简单,但事实证明它非常令人沮丧.我得到的最好的方法是像这样覆盖原始 jPanel 中的 paintComponent 函数:

However, while it seems like this should be simple, it's proven very frustrating. The best I've gotten, is to override the paintComponent function in the original jPanel as such:

panCharDisplay = new javax.swing.JPanel() {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        paintLines(g);
    }
};

然后这样画线:

public void paintLines(Graphics g) {
    g.setColor(Color.BLUE);
    for (Character c : characters) {
        if (c.female && c.spouse != null) {
            g.drawLine(c.display.getX(), c.display.getY(), c.spouse.display.getX(), c.spouse.display.getY());
        }
    }
}

这在某种意义上是有效的,因为它确实在技术上绘制了线条,在正确的位置,正确的颜色等等,但前提是我从线条应该出现的位置滚动,然后向后滚动.每当我在它周围拖动一个组件时,都会导致奇怪的图形错误,因为它只绘制线条的一部分并且不会擦除之前的线条.这些线条也显示在子面板下方而不是上方,使它们在很多时候都隐藏起来.

This works in a sense, in that it does technically draw the lines, in the right place, the right color, and so on, but only if I scroll away from where the line should be, and then scroll back. Whenever I drag a component around it causes weird graphics errors, as it draws only parts of the line and doesn't erase the ones before. The lines also show up below the sub-panels instead of over them, making them hidden a lot of the time.

我假设这是因为我在错误的时间绘制线条,需要在绘制子面板后绘制它们,并且还要确保每次拖动面板时重新绘制它们

I assume the reason for this is that I'm drawing the lines at the wrong time, and need to draw them after drawing the sub-panels, and also make sure that they are re-drawn every time the panels are dragged around.

是否还有其他地方可以进行覆盖以使线条显示得更一致?我尝试的另一种方法是创建一个扩展 JPanel 的类,并尝试使用它来处理绘图,但我根本无法让它工作.

Is there another place I can put in an override to make the lines show up more consistently? Another method I tried, was to make a class that extends JPanel, and try to use that to handle the drawing, but I couldn't get it to work at all.

推荐答案

当您拖动面板时,您可能需要在面板的父级上调用 repaint().

You probably need to invoke repaint() on the panel's parent as you drag the panel around.

线条也显示在子面板下方而不是上方,使它们在很多时候都隐藏起来.

您应该能够覆盖paint()paintChildren() 方法而不是paintComponent() 方法.无论您覆盖哪种方法,请确保首先调用 super.XXX() 以便在您尝试绘制线条之前完成默认绘制.

You should be able to override either the paint() or paintChildren() method instead of the paintComponent() method. Whichever method you override make sure to invoke super.XXX() first so that the default painting is done before you attempt to draw your lines.

我个人喜欢在trashgod的GraphPanel中展示的在组件下方绘制的线条例子.该示例对形状进行了自定义绘制,但我猜想组件的逻辑是相似的.

Personally I like the lines painted below the component as is demonstrated in trashgod's GraphPanel example. The example does custom painting for the shapes, but I would guess the logic would be similar for the components.

这篇关于在具有子面板的 jPanel 上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:20
查看更多