paintComponent maxing out CPU
I'm currently developing a program which outputs some basic graphics, only problem is is it maxing out my CPU in the process and was just wondering why.
I have a main JFrame which instantiates a few other classes that extend JPanel. Each JPanel then displays some very basic graphics or text.
If I comment out all the JPanel class calls the JFrame displays and CPU behaves normally, but of course no graphics are displayed. If I call one of the JPanels CPU activity increases dramatically.
Below is the code for one of the simpler classes. If I comment out the whole paintComponent() method the CPU runs fine, but even with the three lines left uncommented as it is shown now, the CPU runs at 100%
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
publicclass DrawXLabelextends JPanel{
int gap, pos;
public DrawXLabel(int num){
gap = 600/num;
pos = num/10;
if (pos == 0)
pos = 1;
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
this.setBorder(BorderFactory.createEmptyBorder(5,18,5,5));
g.setColor(Color.BLACK);
/*int j = 1;
int k = 15;
for (int i=0; i<10; i++){
g.drawString(""+j, k, 10);
j = j+pos;
k = k+gap;
}*/
}
}

