簡體   English   中英

在Java中用多個面板繪制線條

[英]Draw line over multiple panels in Java

我試圖在多個面板上繪制一條線(圖像中的紅線),但我似乎無法使它工作。 我怎樣才能做到這一點? 有什么建議?

繪制所需的功能

畫在玻璃窗上

JDK 7添加了JLayer以支持任意組件之上的視覺裝飾 對於早期版本, java.net上的項目JXLayer實際上是它的前身,具有非常相似的api

這是一個基本的例子,使用自定義的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM