通过绘制一个秋千选择框 [英] Drawing a selection box using Swing

查看:137
本文介绍了通过绘制一个秋千选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了面板和三个按钮的应用程序。我想用鼠标选择添加按钮这个。我的意思是像我们在Windows桌面上。我preSS鼠标左键并用鼠标区域选择越来越大的运动。

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:

所以我有一个问题,当我使用事件鼠标拖动矩形漆,按钮被重新绘制,以便用户看到闪烁的按钮。我想这个按钮时,我画的矩形不disapear。我想,我需要使用的glassPane。这是我的观念。我有一个框架。在I帧添加具有按键面板,我需要另一个小组在那里我会画画透明的矩形。我想我的话按钮将不会被重新粉刷依然。你怎么看待这个概念是什么。或者,也许有人有另一个想法。这是code:

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


            }
        });
    }

}

我知道,code是没有完美的,但几乎工作。我有一个小问题。当我preSS的mousebutton,拖按钮disapear。

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.

我不需要建议,如你的code是错误的。我知道,我想有人帮助我什么,我必须纠正。我知道我不应该在mouseEvents使用的paintComponent(),但只有这样我可以画透明矩形。或者,也许你可以othet想法我怎么能画透明矩形。我试了又试,我认为我必须改变的mouseDragged方法。因为当我从这个方法删除code和只画矩形按钮上方一切正常。但问题是,当我需要通过拖动鼠标绘制矩形。我应该改变油漆,但我不知道如何。任何人都可以帮助我还是尽量帮帮我吗?

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?

推荐答案

我认为code确实在这个形式不是作品(主),也许有人会调试你,请与Joey的建议如下

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

嗯很反感的话,反正如下(甲骨文的Java教程)的http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html

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

这篇关于通过绘制一个秋千选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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