I'm a Hood Ornament
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Virendrasinh Gohil wrote:Seems like your paintComponent() getting called multiple times. (Try to put one more sysout outside of for loop to prove).
This is event based and whenever there is a need of painting the component, this method will be called. Now depending upon what do you want to do, you can add appropriate logic there in paintComponent method. If you want to recalculate (or something else) on every paint event, you can do it in paintComponent() method. (Not sure from your code what are you trying to do with lineLength in paintComponent method.) If it is a one time activity you may want to reconsider refactoring your code.
I'm a Hood Ornament
Megs Maquito wrote:Hmmm. that seemed to do the trick here's the result:
L is 20 i is 0
.......
L is 140 i is 13
outside the for loop
.................
Question is: why would i need to put something outside the for loop? is the i % == 2 somehow telling the class to repaint itself or is it the sequence after the JFrame declaration in main() method? I have other classes that have a for loop that does not have a statement outside the for loop and works perfectly. Here's an example:
....
...
...
I could put the for loop in the main method but I want the drawing to be repainted if i resize the JFrame, thus I put the for loop in the paintComponent() method.
Virendrasinh Gohil wrote:
Megs Maquito wrote:Hmmm. that seemed to do the trick here's the result:
L is 20 i is 0
.......
L is 140 i is 13
outside the for loop
.................
Question is: why would i need to put something outside the for loop? is the i % == 2 somehow telling the class to repaint itself or is it the sequence after the JFrame declaration in main() method? I have other classes that have a for loop that does not have a statement outside the for loop and works perfectly. Here's an example:
....
...
...
No, putting print statement outside of for loop will not prevent executing the paintComponent() method multiple times. I just mentioned that to show you that it's not the "for" loop which is creating problem but it is the event of request to repaint the JPanel. And that is what exactly happened when you re-executed the application. Whenever there is a repaint (either manual or system generated) event, your paintComponent() will get executed.
I could put the for loop in the main method but I want the drawing to be repainted if i resize the JFrame, thus I put the for loop in the paintComponent() method.
Now, if you want to recalculate the length of line on every paintComponent() method, you need to calculate using actual numbers (and not just keep increasing the line using for loop). That means, if the frame/panel is reduced in size, the line size should use new width/heigth values to decrease the line length. This way, let it be multiple repaint event to occur for same window, it will always calculate the lineLength correctly.
Good code. I am delighted. I am happy it helpedMegs Maquito wrote:
Virendrasinh Gohil = javaGenius(int x);
public int javaGenius (int n){int intelligence = n * infinity; return intelligence};