Java Swing GlassPane 拖动性能

发布于 2024-11-16 17:15:47 字数 1450 浏览 4 评论 0原文

我正在尝试使用 GlassPane 在 Swing 应用程序中进行可视化拖放,但遇到了拖动图像滞后于鼠标指针的问题,有时甚至相当严重。我该如何解决这个问题?我的 GlassPane 代码如下。谢谢!

package dragui;

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter;

public class GlassPane extends JComponent {
    private int x=0, y=0, k=25,z=1;
    private boolean showDot;
    private MouseInputAdapter mia = new MouseInputAdapter(){

        @Override
        public void mouseDragged(MouseEvent me) {
            setPos(me.getX(), me.getY());
        }

        @Override
        public void mousePressed(MouseEvent me) {
            setShow(true);
            setPos(me.getX(), me.getY());   
        }

        @Override
        public void mouseReleased(MouseEvent me){
            setShow(false);
            setVisible(false);
        }
    };

    public void setShow(boolean b){ this.showDot = b; }

    public void paint(Graphics g) {
        if (showDot) {
            g.setColor(new Color(0.0f, 0.25f, 1.0f));
            g.fillOval(x - k, y - k, 2*k, 2*k);
        }
    }

    public void setPos(int x, int y) {
        int tmpX = this.x, tmpY = this.y; 
        this.x = x; this.y = y;
        repaint(tmpX - k , tmpY-k, 2*k+5, 2*k+5);
        repaint(this.x-k, this.y-k, 2*k+5, 2*k+5);        
    }

    public GlassPane() {
        addMouseListener(mia); 
        addMouseMotionListener(mia);
        setOpaque(false);
    }
}

I'm trying to do a visual drag and drop in my Swing application using a GlassPane, but am running into the problem that the drag image lags behind the mouse pointer, sometimes considerably. How do I solve this? My GlassPane code is below. Thanks!

package dragui;

import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter;

public class GlassPane extends JComponent {
    private int x=0, y=0, k=25,z=1;
    private boolean showDot;
    private MouseInputAdapter mia = new MouseInputAdapter(){

        @Override
        public void mouseDragged(MouseEvent me) {
            setPos(me.getX(), me.getY());
        }

        @Override
        public void mousePressed(MouseEvent me) {
            setShow(true);
            setPos(me.getX(), me.getY());   
        }

        @Override
        public void mouseReleased(MouseEvent me){
            setShow(false);
            setVisible(false);
        }
    };

    public void setShow(boolean b){ this.showDot = b; }

    public void paint(Graphics g) {
        if (showDot) {
            g.setColor(new Color(0.0f, 0.25f, 1.0f));
            g.fillOval(x - k, y - k, 2*k, 2*k);
        }
    }

    public void setPos(int x, int y) {
        int tmpX = this.x, tmpY = this.y; 
        this.x = x; this.y = y;
        repaint(tmpX - k , tmpY-k, 2*k+5, 2*k+5);
        repaint(this.x-k, this.y-k, 2*k+5, 2*k+5);        
    }

    public GlassPane() {
        addMouseListener(mia); 
        addMouseMotionListener(mia);
        setOpaque(false);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别低头,皇冠会掉 2024-11-23 17:15:47

根据 Javadoc,

一个只想做的子类
专门化 UI(外观和感觉)
委托的绘制方法应该只是
覆盖paintComponent。

这是因为 paint(...) 实际上将绘画工作委托给了三个受保护的方法:paintComponentpaintBorder绘制儿童。话虽这么说,请尝试用此替换 paint(...)

@Override
protected void paintComponent(Graphics g) {
    if (showDot) {
        Graphics gCopy = g.create();

        gCopy.setColor(new Color(0.0f, 0.25f, 1.0f));
        gCopy.fillOval(x - k, y - k, 2*k, 2*k);

        gCopy.dispose();
    }
}

According to the Javadoc,

A subclass that just wants to
specialize the UI (look and feel)
delegate's paint method should just
override paintComponent.

And this is because paint(...) actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. That being said, try replacing paint(...) with this

@Override
protected void paintComponent(Graphics g) {
    if (showDot) {
        Graphics gCopy = g.create();

        gCopy.setColor(new Color(0.0f, 0.25f, 1.0f));
        gCopy.fillOval(x - k, y - k, 2*k, 2*k);

        gCopy.dispose();
    }
}
纸短情长 2024-11-23 17:15:47

也许您不应该在每次重绘时创建一个new Color(0.0f, 0.25f, 1.0f)

对我来说,表演很安静。

public class Example {
    static public void main( String[] s ) {
        EventQueue.invokeLater( new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setBounds( 50, 50, 600, 600 );
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setGlassPane( new JComponent() {
                    {
                        MouseInputAdapter mia = new MouseInputAdapter(){
                            public void mouseDragged(MouseEvent me) {
                                setPos(me.getX(), me.getY());
                            }
                            public void mousePressed(MouseEvent me) {
                                setShow(true);
                                setPos(me.getX(), me.getY());   
                            }
                            public void mouseReleased(MouseEvent me){
                                setShow(false);
                                setVisible(false);
                            }
                        };
                        addMouseListener(mia); 
                        addMouseMotionListener(mia);
                        setOpaque(false);
                    }
                    private int x=0, y=0, k=25,z=1;
                    private boolean showDot;
                    public void setShow(boolean b){ this.showDot = b; }
                    private Color color = new Color(0.0f, 0.25f, 1.0f);
                    public void paintComponent(Graphics g) {
                        if (showDot) {
                            g.setColor(color);
                            g.fillOval(x - k, y - k, 2*k, 2*k);
                        }
                    }
                    public void setPos(int x, int y) {
                        int tmpX = this.x, tmpY = this.y; 
                        this.x = x; this.y = y;
                        repaint(tmpX - k , tmpY-k, 2*k+5, 2*k+5);
                        repaint(this.x-k, this.y-k, 2*k+5, 2*k+5);        
                    }
                } );
                frame.getGlassPane().setVisible( true );
                frame.setVisible( true );
            }
        });
    }
}

perhaps you should not creating a new Color(0.0f, 0.25f, 1.0f) on every repaint.

For me the performance is quiet nice.

public class Example {
    static public void main( String[] s ) {
        EventQueue.invokeLater( new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.setBounds( 50, 50, 600, 600 );
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                frame.setGlassPane( new JComponent() {
                    {
                        MouseInputAdapter mia = new MouseInputAdapter(){
                            public void mouseDragged(MouseEvent me) {
                                setPos(me.getX(), me.getY());
                            }
                            public void mousePressed(MouseEvent me) {
                                setShow(true);
                                setPos(me.getX(), me.getY());   
                            }
                            public void mouseReleased(MouseEvent me){
                                setShow(false);
                                setVisible(false);
                            }
                        };
                        addMouseListener(mia); 
                        addMouseMotionListener(mia);
                        setOpaque(false);
                    }
                    private int x=0, y=0, k=25,z=1;
                    private boolean showDot;
                    public void setShow(boolean b){ this.showDot = b; }
                    private Color color = new Color(0.0f, 0.25f, 1.0f);
                    public void paintComponent(Graphics g) {
                        if (showDot) {
                            g.setColor(color);
                            g.fillOval(x - k, y - k, 2*k, 2*k);
                        }
                    }
                    public void setPos(int x, int y) {
                        int tmpX = this.x, tmpY = this.y; 
                        this.x = x; this.y = y;
                        repaint(tmpX - k , tmpY-k, 2*k+5, 2*k+5);
                        repaint(this.x-k, this.y-k, 2*k+5, 2*k+5);        
                    }
                } );
                frame.getGlassPane().setVisible( true );
                frame.setVisible( true );
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文