2

I have a question. Can I create glasspane in body MousePressed ? If yes anyone can write me how? I mean that I press mouse button and glass pane is visible and I can painting on him.

EDIT

Ok I have now what I want. My glass pane is creating when I click mouse button and disapear when I release this button. Now I have another question. Where I should create my painting method. I want draw rectangle on this glasss pane using mouse dragged. Where I must implement paint method? In other class or in this events? I implement one my try paint function but I don't know if this is good way. This is my code:

public class Selection extends JPanel
{
    static Point startPoint;
    public static void GUI()
    {

        final JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton("Select");
        final JPanel glassPane = new JPanel();


        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.add(button);
        glassPane.setOpaque(false);
        frame.add(panel);


        frame.setGlassPane(glassPane);
        glassPane.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                System.out.println("f.getGlassPane() mousePressed");
                if(e.getButton() == MouseEvent.BUTTON1)


                frame.getGlassPane().setVisible(true);

                startPoint=e.getPoint();

                Graphics2D g = null;
                Graphics2D g2 = (Graphics2D) g;
                Rectangle2D rect = new Rectangle2D.Double();
                rect.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
                g2.setColor(Color.BLUE);
                g2.fill(rect);
                g2.draw(rect);

            }

        });
        glassPane.addMouseMotionListener(new MouseMotionListener() {


            @Override
            public void mouseDragged(MouseEvent e) 
            {


            }

            @Override
            public void mouseMoved(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

        frame.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                if(e.getButton() == MouseEvent.BUTTON1)

                frame.getGlassPane().setVisible(true);

            }
            public void mouseReleased(MouseEvent e)
            {                     
                    frame.getGlassPane().setVisible(false);
            }
        });



        frame.setVisible(true);
    }


        int x1, x2, y1,y2;
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D)g;
             g2.drawRect(x1,y1, x2, y2);
        }


    public static void main(String[] args) 
    {

        GUI();
    }

}
edi233
  • 3,511
  • 13
  • 56
  • 97
  • Have you tried before asking? – jfpoilpret May 13 '11 at 10:10
  • yes, I think that I must create method with methods where I will implements painting methods and in frame I should create glass pane and add mouse listeners. That is good way? – edi233 May 13 '11 at 10:14
  • see my answer below. NJormally you would: create your frame, add content (normal content: labels, text fields...) to it, set a glass pane and attache a MouseListener. – jfpoilpret May 13 '11 at 10:17

2 Answers2

2

Hi please check out my answer to some other question where I present a way in which a glass pane can be used to simulated dialog behaviour. There you have shown how to show it and hide it on mouse click in my case right mouse click. This example should get you started nicely.

Community
  • 1
  • 1
Boro
  • 7,913
  • 4
  • 43
  • 85
1

I see no problem creating a glasspane and attaching it to a RootPaneContainer from inside moussePressed() method.

However, I may wonder why create a new glass pane every time the user clicks the mouse; that wouldn't be very performant; it is probably wiser to create and attach a glass pane up front and then change its content during mouse click).

Now, regarding "painting on the glass pane", it depends on what you mean by "painting", if this means using a "Graphics" instance to directly draw on the glass pane, the answer is NO (well, actually you could but, your painting would disappear at first UI refresh...)

Such painting must occur in paintComponent() method of your glass pane (that you must override).

jfpoilpret
  • 10,449
  • 2
  • 28
  • 32
  • I want using a Graphics2D to draw rectangle on glass pane using mouseDragged. My idea is: 1) I click mouse button and my glass pane is visible. 2) I use mouse dragged to draw rectangle. 3) When I release mouse button my glass pane disapear. I want drawing rectangle in real time and change his size using mousedragged – edi233 May 13 '11 at 10:32
  • Then you have to record mouse coordinates during mouseDragged and force a repaint() of the pane. You still need to override paintComponent(). – jfpoilpret May 13 '11 at 11:21