Drawing a selection box using Swing

只愿长相守 提交于 2019-12-08 03:54:49

问题


I have written an application with a panel and three buttons. I want to add selection this buttons using the mouse. I mean like we have in Windows on the Desktop. I press the left mouse button and with the movement of the mouse the area selection is growing.

Is there a specific interface in this or do I have it manually call the appropriate methods for event listeners and there draw transparent rectangle? Here is a picture:

So I have a problem when I paint rectangle using event mouse-dragged, button is repainting so user see blinking button. I want to this button don't disapear when I paint rectangle. I think that I need to use glassPane. This is my conception. I have a frame. In frame I add panel with button and I need another panel where I will paint transparent rectangle. I am thinking then my button will not be still repainting. What do you think about this conception. Or maybe someone have another idea. This is code:

        @Override
        public void mousePressed(MouseEvent e) {
            startPoint=e.getPoint();
            setOpaque(true);

            Graphics2D g2 = (Graphics2D)getGraphics();

            Rectangle2D prostokat = new Rectangle2D.Double();
            prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.draw(prostokat);
            g2.setColor(Color.BLUE);
            g2.fill(prostokat);


        }



        @Override
        public void mouseDragged(MouseEvent e) {
            setOpaque(true);

            Graphics2D g2 = (Graphics2D)getGraphics();
            Rectangle2D prostokat = new Rectangle2D.Double();

            prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
            g2.setComposite(AlphaComposite.getInstance(rule, alpha));
            g2.draw(prostokat);
            g2.setColor(Color.BLUE);
            g2.fill(prostokat);
            paintComponent(g2);


        }

        int rule = AlphaComposite.SRC_OVER;
        float alpha = 0.85F;




    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable()
        {
public void run()
            {
                zaznacz rys = new zaznacz();
                JFrame frame = new JFrame();
                JButton Button = new JButton("1");
                JPanel panel = new JPanel();



                panel.add(Button);
                rys.add(panel);
                frame.setSize(400,300);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                panel.setOpaque(false);

                frame.add(rys);


            }
        });
    }

}

I know that code is no perfect but almost work. I have a little problem. When I press the mousebutton and dragging my button disapear.

I don't need advise like "your code is wrong". I know that and I want to somebody help me what I must correct. I know that I shouldn't use paintComponent() in mouseEvents but only that way I can paint transparent rectangle. Or maybe you can othet idea how I can draw transparent rectangle. I try and try and I think that i must change mouseDragged method. because when I delete code from this method and only draw rectangle over a button all is ok. But problem is when I need draw rectangle by dragging mouse. I should change paint but I don't have idea how. Anyone can help me or try help me?


回答1:


I think that that code doesn't works in this forms(main), maybe someone will debug that for you, please follows with Joey's advices

hmmm very offensive words, anyway follows (Oracle Java tutorial) http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html



来源:https://stackoverflow.com/questions/5727578/drawing-a-selection-box-using-swing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!