如何将 JGraph 转换为玻璃窗格? [英] How to convert JGraph to a glass pane?

查看:32
本文介绍了如何将 JGraph 转换为玻璃窗格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类:public class GraphEditorPane extends JGraph

我使用这个GraphEditorPane"如下:

and I am using this "GraphEditorPane" as follows :

public JScrollPane getGraphPaneScrollPane() {
    if (graphPaneScrollPane == null) {
        graphPaneScrollPane = new JScrollPane();
        //graphPaneScrollPane.setViewportView(getGraphEditorPane());
        graphPaneScrollPane.setViewportView(getGraphEditorPane());
    }
    return graphPaneScrollPane;
}
public GraphEditorPane getGraphEditorPane() {
graphEditorPane = new GraphEditorPane(); }

我正在使用这个GraphEditorPane"在它上面绘制一个图表.现在我的问题是 - 有什么办法可以将这个 JGraph 转换成一个玻璃窗格,这样我的GraphEditorPane"就会是透明的,我仍然可以在它上面画画?

I am using this 'GraphEditorPane' to draw a Graph over it. Now my question is -- Is there any way that I can convert this JGraph into a Glass Pane so that my 'GraphEditorPane' would be transparent and I could still draw over it ?

推荐答案

我认为你把事情复杂化了.

I think you're over complicating things.

玻璃面板将是最顶部的组件(当可见时),在其他所有组件的顶部进行绘制.如果您只是想将一个组件覆盖"在另一个组件上,则有更简单的解决方案...

The glass pane will be the top most component (when visible), painting over the top of everything else. If you simply want to "overlay" one component over another you there are much simpler solutions...

我能想到的最简单的想法是使用 JLayeredPane,将其设置为使用 GridBagLayout 这样您就不必担心定位孩子成分.这将为您提供快速简便的方法来更改组件的顺序.

The simplest idea I can think off would be to use a JLayeredPane, setting it up to use a GridBagLayout so you don't need to worry about positioning the child components. This will give you quick and easy methods for changing the order of components.

另一种解决方案是简单地将叠加层组件直接添加到底层组件的顶部.

Another solution would be to simply add the overlay component directly on top of the underlay component.

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Overlay {

    public static void main(String[] args) {
        new Overlay();
    }

    public Overlay() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                ImagePane imagePane = new ImagePane();
                imagePane.setLayout(new BorderLayout());
                imagePane.add(new OverlayPane());

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(imagePane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class OverlayPane extends JPanel {

        public OverlayPane() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.setStroke(new BasicStroke(4));
            int radius = 40;
            g2d.drawOval(326 - radius / 2, 351 - radius / 2, radius, radius);
            g2d.drawOval(416 - radius / 2, 351 - radius / 2, radius, radius);

            int size = 20;

            g2d.drawLine(374, 400, 374 - size, 400 + size);
            g2d.drawLine(374, 400, 374 + size, 400 + size);

            g2d.dispose();
        }

    }

    public class ImagePane extends JPanel {

        private BufferedImage buffer;

        public ImagePane() {
            try {
                buffer = ImageIO.read(new File("/path/to/your/image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return buffer == null ? new Dimension(200, 200) : new Dimension(buffer.getWidth(), buffer.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(buffer, 0, 0, this);
            g2d.dispose();
        }
    }

}

这篇关于如何将 JGraph 转换为玻璃窗格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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