aspose file tools*
The moose likes Java in General and the fly likes for loop working 2 - 4 times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "for loop working 2 - 4 times" Watch "for loop working 2 - 4 times" New topic
Author

for loop working 2 - 4 times

Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
I'm really confused why the for loop is working multiple times. When the counter reaches the limit it goes back to zero. For this purpose I omitted the code that draws the actual line. I need to resolve this issue first before I include the algorithm for the line. Thanks so much in advance.



here's the result:

L is 20 i is 0
L is 20 i is 1
L is 40 i is 2
L is 40 i is 3
L is 60 i is 4
L is 60 i is 5
L is 80 i is 6
L is 80 i is 7
L is 100 i is 8
L is 100 i is 9
L is 120 i is 10
L is 120 i is 11
L is 140 i is 12
L is 140 i is 13
L is 160 i is 0
L is 160 i is 1
L is 180 i is 2
L is 180 i is 3
L is 200 i is 4
L is 200 i is 5
L is 220 i is 6
L is 220 i is 7
L is 240 i is 8
L is 240 i is 9
L is 260 i is 10
L is 260 i is 11
L is 280 i is 12
L is 280 i is 13
L is 300 i is 0
L is 300 i is 1
L is 320 i is 2
L is 320 i is 3
L is 340 i is 4
L is 340 i is 5
L is 360 i is 6
L is 360 i is 7
L is 380 i is 8
L is 380 i is 9
L is 400 i is 10
L is 400 i is 11
L is 420 i is 12
L is 420 i is 13
L is 440 i is 0
L is 440 i is 1
L is 460 i is 2
L is 460 i is 3
L is 480 i is 4
L is 480 i is 5
L is 500 i is 6
L is 500 i is 7
L is 520 i is 8
L is 520 i is 9
L is 540 i is 10
L is 540 i is 11
L is 560 i is 12
L is 560 i is 13

I'm a Hood Ornament
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
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.
vijin das
Ranch Hand

Joined: Jun 07, 2010
Posts: 129

the loop is getting executed 4 times as far you had pasted the code or is it getting executed more or less in another try of running it???

Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

What is the expected result ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

If the problem is the increasing value of lineLength you should consider making it a local variable. That way, each time your panel is repainted, it will start at 0 again.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
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.


Hmmm. that seemed to do the trick here's the result:

L is 20 i is 0
L is 20 i is 1
L is 40 i is 2
L is 40 i is 3
L is 60 i is 4
L is 60 i is 5
L is 80 i is 6
L is 80 i is 7
L is 100 i is 8
L is 100 i is 9
L is 120 i is 10
L is 120 i is 11
L is 140 i is 12
L is 140 i is 13
outside the for loop

Thanks so much for all your quick replies. Basically the lineLength should increase every other time it goes through the loop. I will be using that variable to alter the drawLine() method so the output will be a line spiralling outwards. 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.

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:

Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
Ok, now this is becoming really weird. I did a javac SpiralLine.java (without changing anyting) and java SpiralLIne and here's the result:


L is 20 i is 0
L is 20 i is 1
L is 40 i is 2
L is 40 i is 3
L is 60 i is 4
L is 60 i is 5
L is 80 i is 6
L is 80 i is 7
L is 100 i is 8
L is 100 i is 9
L is 120 i is 10
L is 120 i is 11
L is 140 i is 12
L is 140 i is 13
outside the for loop
L is 160 i is 0
L is 160 i is 1
L is 180 i is 2
L is 180 i is 3
L is 200 i is 4
L is 200 i is 5
L is 220 i is 6
L is 220 i is 7
L is 240 i is 8
L is 240 i is 9
L is 260 i is 10
L is 260 i is 11
L is 280 i is 12
L is 280 i is 13
outside the for loop

I will put the i % == 2 outside of the paintComponent() method and check the results again. Again many thanks for all your help. It made things a lot clearer. I was going crazy trying to find out where my logic is wrong.
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
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.
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
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.


OMG you're a genius. However, having the spiral recalculate will entail some heavy duty math (at least for me ) I was able to create what I wanted whilst not growing with the frame. I did what you earlier suggested (that is to put the lineLength calculation outside the paintComponent()). And here's the full code:


Yeah I know, it may seem very elementary for you . I'm just starting out. Thanks so much for your help.

Virendrasinh Gohil = javaGenius(int x);
public int javaGenius (int n){int intelligence = n * infinity; return intelligence};
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
Megs Maquito wrote:
Virendrasinh Gohil = javaGenius(int x);
public int javaGenius (int n){int intelligence = n * infinity; return intelligence};
Good code. I am delighted. I am happy it helped
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop working 2 - 4 times
 
Similar Threads
problem in threads(sun accesibility test)
Need help on animation using GUI Builder.
Can we expect this type of question in Beta 1.5, because AWT is not part of contents
HashSet Magic
drawing arrows