简体   繁体   English

在Java onPaint()上调用Repaint()

[英]Calling Repaint() on Java onPaint()

I'm currently implementing a mousehover script in Java for a button with full graphics (no JButton). 我目前正在用Java为具有完整图形的按钮(没有JButton)实现鼠标悬停脚本。 here's my piece of code: 这是我的代码:

@Override
public void mouseMoved(MouseEvent e){
    if (btnExit.getBound().contains(e.getX(), e.getY())){
        btnExit.setStatus(BUTTON_STATE.HOVER);
    } else {
        btnExit.setStatus(BUTTON_STATE.IDLE);
    }
    System.getInstance().repaint();
}

the repaint method is always called when my mouse moves. 当鼠标移动时,总是会调用repaint方法。

The question is > is it a good implementation for the hover action? 问题是>悬停操作是否很好的实现? or are there better implementations? 还是有更好的实现? because I thought that calling repaint() everytime my mouse move is quite heavy in computation. 因为我认为每次鼠标移动都会调用repaint(),因此计算量很大。

THX b4 THX b4

Calling repaint() does not mean that the component will be repainted immediately. 调用repaint()并不意味着将立即重新绘制组件。 This call simply places an entry into repaint request queue that may be merged with others entries in many situations. 该调用只是将条目放置在重画请求队列中,在许多情况下,这些条目可以与其他条目合并。

If you suspect repaint() may be called way too often, use the version that accepts the maximal time after that the object should be repainted. 如果您怀疑repaint()可能被调用得太频繁了,请使用可接受最长时间的版本来重新绘制对象。 For instance, if you call button.repaint(1000) 100 times in the same second, it will be only repainted once. 例如,如果您在同一秒内调用button.repaint(1000) 100次,它将仅被重新绘制一次。 You may also specify the area that should be repainted (rather than whole screen), but this only works well if your implementation really does less job with such partial repaint. 您还可以指定应该重新粉刷的区域(而不是整个屏幕),但这仅在您的实现在这种部分重新粉刷中确实做得更少的情况下才有效。

Also, you may call repaint on btnExit rather than on the whole application frame. 另外,您可以在btnExit而不是整个应用程序框架上调用重绘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM