0

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2089825
  • 13
  • 1
  • 4

1 Answers1

2

repaint(pane.getViewport().getViewRect()); is causing, in a long about way, paintComponent to be called, again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again and again....

I think you get the point...

A better idea would be to replace the scroll panes UI delegate instead.

Take a look at Modifying the Look and Feel

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yeah I had a feeling that was the problem but I tried looking through the source code for repaint and I couldn't find anything that made paintComponent be called again and again. Tbh I'd rather not edit the LookAndFeel is there any other work around? Thanks – user2089825 May 31 '13 at 00:09
  • `repaint` makes a request to `RepaintManager` which is responsible for scheduling repaints back to the EDT. Because these are pushed onto a queue to be processed later, this avoid stack overflow issues, but causes a nasty callback cycle...Check out [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) for more details... – MadProgrammer May 31 '13 at 00:12
  • repaint() *requests* a repaint, by posting a PAINT event to the queue. The 'workaround' is not to do the redundant repaint(). – user207421 May 31 '13 at 00:12
  • @MadProgrammer I'm doubt, JViewport required to override [setScrollMode and add own RepaintManager](http://stackoverflow.com/q/8614972/714968), then CPU consuption can going away, sure nothing clear from OPs question, leaving this one for whales – mKorbel May 31 '13 at 06:46