• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

for loop working 2 - 4 times

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 129
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???

 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the expected result ?
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic