Java Swing GlassPane 拖动性能
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Javadoc,
这是因为
paint(...)
实际上将绘画工作委托给了三个受保护的方法:paintComponent
、paintBorder
和绘制儿童
。话虽这么说,请尝试用此替换paint(...)
According to the Javadoc,
And this is because
paint(...)
actually delegates the work of painting to three protected methods:paintComponent
,paintBorder
, andpaintChildren
. That being said, try replacingpaint(...)
with this也许您不应该在每次重绘时创建一个
new Color(0.0f, 0.25f, 1.0f)
。对我来说,表演很安静。
perhaps you should not creating a
new Color(0.0f, 0.25f, 1.0f)
on every repaint.For me the performance is quiet nice.