Hi so recently I wanted to make my own interface in Java and hated the Windows scroll bar(I was adding a JList). So I decided to mimic Facebook's scrollbars(not completed yet since I ran into this problem).
So the problem is basically the title. So pretty much I saw my CPU usage skyrocketed for a simple interface when I called repaint() in paintComponent(). But I found it was necessary to do so or else my scroll bar would not be redrawn.
My CPU is a i7 3770 and the program is using 15% which isn't a lot but for what it does it's quite a lot. But when I don't call repaint() it uses around 1% which is what I want it to be at.
So here's my paintComponent() code:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (pane.getViewport().getViewRect().getSize().equals(getSize())) return;
double percentageScrolled = (double) pane.getVerticalScrollBar().getValue() / (pane.getVerticalScrollBar().getMaximum() - pane.getVerticalScrollBar().getModel().getExtent());
int x = pane.getBounds().width - 11, y = pane.getViewport().getViewPosition().y + 3 + ((int) ((pane.getBounds().height - SCROLL_BAR.getHeight(null) - 9) * percentageScrolled));
repaint(pane.getViewport().getViewRect());
g.drawImage(SCROLL_BAR, x, y, null);
}
and below is a link to a video of what happens when I don't call repaint()
https://www.youtube.com/watch?v=OMXVB7REFHk&feature=youtu.be
So in that video when I scroll I have to click on the options/values to repaint the scroll bar.