hey there,

I have two pieces of code that I expected would behave the same. However, what I noticed is that one of them works fine, the other does not at all. I need help from someone experienced to explain what's going on under the hood. the code that does not work at all is the following:

public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);

while(size>0){
Vizatuesi(size, g);
//size = (int)(size*0.8);
}

}


public void Vizatuesi(int size, Graphics g){
//while(size>0){
g.drawRect(0, 0, size, size);
size = (int)(size*0.8);
//}

and the code that does work, is the following:

public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);

while(size>0){
Vizatuesi(size, g);
size = (int)(size*0.8);
}

}


public void Vizatuesi(int size, Graphics g){
g.drawRect(0, 0, size, size);
}

to facilitate your analysis, note that my variable size, in the code that works is being reduced in the paintComponent method, whereas it is being reduced inside the helper method Vizatuesi() in the code that does not work.

any insight is much appreciated

Obviously, size of paintComponent is a global variable (instance or class). Where the scope of Vizatuesi's size is local within this method even it is a formal parameter. Any changes of the local variable does not affect the globally defined size, that is just as living in two different worlds.

Therefore the second code is correct.

You should read something about local, instance and class/ static variables, maybe at sun site.

-- tesu

Edited 6 Years Ago by tesuji: n/a

Changing the variable "size" in the Vizatuesi method has no affect on the value of the variable size that was originally declared in the paintComponent method. In fact, the 'size' variable in the paintComponent method and the 'size' variable in the Vizatuesi method are two completely different variables. When the Vizatuesi method is called, a variable called 'size' is created on the stack. This variable is completely separate from the size variable back in paintComponent.

Comments
it's not the first time that BestJew answers my question right.

Try debugging the code by using println() to print out the value of the size variable when its changed and when its referenced.

well, size is actually not declard in paintCompononet nor in Vizatuesi. variable size is a field (private int size) variable declared outside any method that I wanted to update using the Vizatuesi method, inside a while loop inside paintComponent. this is my complete class (the one that does not work):

import javax.swing.*;
import java.awt.*;

public class Some extends JPanel{

private int size = 500;

public static void main(String[] args){

new Some();
}

public Some(){
//RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
JFrame x = new JFrame();
x.getContentPane().add(this);
x.setSize(500, 500);
x.setTitle("Katroret");
x.setVisible(true);
}

public void paintComponent(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.black);

while(size>0){
Vizatuesi(size, g);
}

}


public void Vizatuesi(int size, Graphics g){

g.drawRect(0, 0, size, size);
size = (int)(size*0.8);

}

}

please note that the code above compiles well, but it does not show the squares drawn on a white background.

@NormR1:

I put a println() inside both methods, paintComponent, and inside Vizatuesi. surprizingly enough, something is resting the value of size to 500 again... and now that I looked back to my code, I see that BestJewSinceOJ was right. I have two different variables, named the same...

I just altered my Vizatuesi method as follows:

public void Vizatuesi(int size1, Graphics g){ g.drawRect(0, 0, size1, size1);
size = (int)(size1*0.8); }

and everything went fine. Thanks a whole lot to both of you.

This question has already been answered. Start a new discussion instead.