Java 如何将JGraph转换为玻璃窗格?

Java 如何将JGraph转换为玻璃窗格?,java,swing,glasspane,jgraph,Java,Swing,Glasspane,Jgraph,我有一个类:公共类grapheditorphane扩展JGraph 我使用的“石墨二吗啉”如下所示: public JScrollPane getGraphPaneScrollPane() { if (graphPaneScrollPane == null) { graphPaneScrollPane = new JScrollPane(); //graphPaneScrollPane.setViewportView(getGraphEditorPane(

我有一个类:
公共类grapheditorphane扩展JGraph

我使用的“石墨二吗啉”如下所示:

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

我用这个“石墨烯”在上面画了一张图。现在我的问题是——有没有办法把这个JGraph转换成一个玻璃窗格,这样我的“石墨烯”就会是透明的,我仍然可以在上面画画?

我觉得你把事情复杂化了

玻璃窗格将是最上面的组件(当可见时),覆盖在所有其他组件的顶部。如果你只是想“覆盖”一个组件,你有更简单的解决方案

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

另一种解决方案是直接将覆盖组件添加到参考底图组件的顶部

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
扩展自
组件
(直接或间接),就应该能够将其设置为根窗格玻璃窗格。将在根窗格的内容上绘制一个玻璃窗格,这意味着除非您已经直接在
JGraph
组件的顶部绘制(即覆盖
paintComponent
方法或直接向其添加组件),否则不会在其上绘制任何其他内容。如何将其设置为根窗格玻璃窗格?据我所知,根窗格玻璃窗格是一个完全不同的组件,对吗?我想在JGraph组件
JRootPane#setGlassPane
的顶部绘制,以设置您自己的组件(请记住,除非组件是透明的,否则它将覆盖所有其他内容)。如果您以这种方式使用玻璃窗格,它将显示在所有其他窗格的顶部,除非您直接对其进行绘制或在其上添加组件。你可以考虑使用一个独立的JayaLeDPAE用于不同的目的,我不想使用它。我只希望我的滚动窗格是透明的,或者是玻璃窗格,这样我就可以将两个滚动窗格相互覆盖,使其中一个透明,以便看到下面窗格上的图像和文本,并在顶部窗格上绘制图形。这似乎有点复杂,但这是它应该工作的方式。