0

I have a class :public class GraphEditorPane extends JGraph

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(); }

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 ?

voidMainReturn
  • 3,339
  • 6
  • 38
  • 66
  • A Glass pane is simply a `Component`. So long as `JGraph` extends from `Component` (directly or indirectly) should be able to set it as the root panes glass pane. A glass pane will painted over the contents of the root pane, meaning that unless you are already painting ontop of the `JGraph` component directly (ie, overriding the `paintComponent` method or adding components directly to it), then nothing else paint over it. – MadProgrammer May 23 '13 at 00:23
  • and how to set it as a root panes glass pane? As far as I know root panes glass pane is an altogether different component right? – voidMainReturn May 23 '13 at 00:30
  • And I want to paint on top of the JGraph component – voidMainReturn May 23 '13 at 00:31
  • `JRootPane#setGlassPane` to set you own (remember, unless the component is transparent, it will cover everything else). If you use a glass pane in this manner, it will appear over the top of everything else, unless you painting directly to it or adding components on to it. You may consider using a [`JLayerdPane`](http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html) instead – MadProgrammer May 23 '13 at 00:40
  • JLayerdPane is used for a different purpose and I don't want to use it. I just want my scrollpane to be either transparent or as a glass pane so that I could place 2 scrollpanes over each other, make one of them transparent so as to see the image and text on the pane below, and draw graphs over the top pane as well. This seems a bit convoluted but this is the way it is supposed to work. – voidMainReturn May 23 '13 at 00:47
  • Why not simply add one component onto the other? – MadProgrammer May 23 '13 at 00:50
  • I did but there are transparency issues if you add two JScrollPanes onto each other. I have tried that. I even tried making viewPort opaque, din't work. – voidMainReturn May 23 '13 at 01:02
  • Don't add the scrollpanes together, add the two graph panes together... – MadProgrammer May 23 '13 at 01:07

1 Answers1

1

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...

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.

enter image description here

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();
        }
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366