在Java中用多个面板绘制线条

时间:2023-01-28 20:05:16

I'm trying to draw a line (Red line in the image) over multiple panels, but I can't seem to make it work. How can I make this possible? Any suggestions?

我试图在多个面板上绘制一条线(图像中的红线),但我似乎无法使其工作。我怎样才能做到这一点?有什么建议么?

在Java中用多个面板绘制线条

2 个解决方案

#1


10  

Draw onto the glass pane.

画在玻璃窗上。

#2


5  

JDK 7 added JLayer to support visual decorations on top of arbitrary components. For earlier versions, there's the project JXLayer at java.net which actually is its predecessor with very similar api

JDK 7添加了JLayer以支持任意组件之上的视觉装饰。对于早期版本,java.net上的项目JXLayer实际上是它的前身,具有非常相似的api

Here's a rudimentary example, using a custom LayerUI which draws a straight line from one component in a container to another component in a different container. The common parent of the two containers is decorated with a JLayer using that ui:

这是一个基本的例子,使用自定义的LayerUI,它从容器中的一个组件到另一个容器中的另一个组件绘制一条直线。两个容器的公共父级使用该UI来装饰JLayer:

    JComponent comp = Box.createVerticalBox();
    final JComponent upper = new JPanel();
    final JButton upperChild = new JButton("happy in upper");
    upper.add(upperChild);
    final JComponent lower = new JPanel();
    final JButton lowerChild = new JButton("unhappy in lower");
    lower.add(lowerChild);
    comp.add(upper);
    comp.add(lower);
    LayerUI<JComponent> ui = new LayerUI<JComponent>() {

        @Override
        public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            Rectangle u = SwingUtilities.convertRectangle(upper, upperChild.getBounds(), c);
            Rectangle l = SwingUtilities.convertRectangle(lower, lowerChild.getBounds(), c);

            g.setColor(Color.RED);
            g.drawLine(u.x, u.y + u.height, l.x, l.y);
        }

    };
    JLayer<JComponent> layer = new JLayer<JComponent>(comp, ui);

#1


10  

Draw onto the glass pane.

画在玻璃窗上。

#2


5  

JDK 7 added JLayer to support visual decorations on top of arbitrary components. For earlier versions, there's the project JXLayer at java.net which actually is its predecessor with very similar api

JDK 7添加了JLayer以支持任意组件之上的视觉装饰。对于早期版本,java.net上的项目JXLayer实际上是它的前身,具有非常相似的api

Here's a rudimentary example, using a custom LayerUI which draws a straight line from one component in a container to another component in a different container. The common parent of the two containers is decorated with a JLayer using that ui:

这是一个基本的例子,使用自定义的LayerUI,它从容器中的一个组件到另一个容器中的另一个组件绘制一条直线。两个容器的公共父级使用该UI来装饰JLayer:

    JComponent comp = Box.createVerticalBox();
    final JComponent upper = new JPanel();
    final JButton upperChild = new JButton("happy in upper");
    upper.add(upperChild);
    final JComponent lower = new JPanel();
    final JButton lowerChild = new JButton("unhappy in lower");
    lower.add(lowerChild);
    comp.add(upper);
    comp.add(lower);
    LayerUI<JComponent> ui = new LayerUI<JComponent>() {

        @Override
        public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            Rectangle u = SwingUtilities.convertRectangle(upper, upperChild.getBounds(), c);
            Rectangle l = SwingUtilities.convertRectangle(lower, lowerChild.getBounds(), c);

            g.setColor(Color.RED);
            g.drawLine(u.x, u.y + u.height, l.x, l.y);
        }

    };
    JLayer<JComponent> layer = new JLayer<JComponent>(comp, ui);