简体   繁体   中英

Calling Repaint() on Java onPaint()

I'm currently implementing a mousehover script in Java for a button with full graphics (no 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.

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.

THX b4

Calling repaint() does not mean that the component will be repainted immediately. 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. For instance, if you call button.repaint(1000) 100 times in the same second, it will be only repainted once. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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