Please don't get confused by my full complex code. Let me walk you through my code and problem.
I have built a class (CharDrawing
), in which I am calling one method (charDrawing
) inside another method (stringDrawing
), let's say two times, in full code below. And it is returning me desired object.
Now, I am trying to replace these 2 calls of charDrawing()
method, using for loop. See the commented part below.
sr=charDrawing(sr,x,y,w,h,pw,"G");
sr=charDrawing(sr,x+w+5,y,w,h,pw,"B");
/*for (int i=0;i<l;i++)
{
sr=charDrawing(sr,x+i*(w+gap),y,w,h,pw,Character.toString(s.charAt(i)));
}*/
But my for
loop is not behaving same as above two lines? What is the difference?
After applying for
loop I am not getting same result.
Debugging: These are my debugging result of objects in both cases. Refer full code below.
Code run without for
loop:
simple object line 1com.badlogic.gdx.graphics.glutils.ShapeRenderer@1ef3e2e
simple object line 2com.badlogic.gdx.graphics.glutils.ShapeRenderer@1ef3e2e
Code run using for
loop:
for loop objectcom.badlogic.gdx.graphics.glutils.ShapeRenderer@13841b8
for loop objectcom.badlogic.gdx.graphics.glutils.ShapeRenderer@13841b8
My Analysis: As you can see, in both cases, same objects are getting updated in each line. Then why am I not getting same results?
Full Code:
public class CharDrawing {
public ShapeRenderer stringDrawing(ShapeRenderer sr,float x, float y, float w, float h,float pw,float gap,String s) {
int l=s.length();
sr.begin(ShapeType.Filled);
/*for (int i=0;i<l;i++)
{
sr=charDrawing(sr,x+i*(w+gap),y,w,h,pw,Character.toString(s.charAt(i)));
System.out.println("for loop object"+sr);
}*/
sr=charDrawing(sr,x,y,w,h,pw,"G");
System.out.println("simple object line 1"+sr);
sr=charDrawing(sr,x+w+5,y,w,h,pw,"B");
System.out.println("simple object line 2"+sr);
sr.end();
return sr;
}
public ShapeRenderer charDrawing(ShapeRenderer sr,float x, float y, float w, float h,float pw,String c)
{
if(c=="G")
{
//sr.begin(ShapeType.Filled);
sr.setColor(200/255f, 245/255f,112/255f, 1); //color1
sr.rect(x,y,w,h);
sr.setColor(107/255f, 107/255f,107/255f, 1); //color2
sr.rect(x+pw,y+pw,w*0.35f,h-2*pw);
//sr.setColor(407/255f, 107/255f,107/255f, 1); //for debugging
sr.rect(x+pw+w*0.35f,y+pw+h*0.5f-pw,0.65f*w-pw,h*0.5f-pw);
sr.rect(x+2*pw+w*0.35f,y,0.65f*w-3*pw,h*0.5f-pw);
//sr.end();
}
if(c=="B")
{
//sr.begin(ShapeType.Filled);
sr.setColor(200/255f, 245/255f,112/255f, 1); //color1
sr.rect(x,y,w,h);
sr.setColor(107/255f, 107/255f,107/255f, 1); //color2
//sr.setColor(407/255f, 107/255f,107/255f, 1); //for debugging
sr.rect(x+pw,y+pw,w-2*pw,0.5f*h-1.5f*pw);
sr.rect(x+pw,y+0.5f*h+0.5f*pw,w-2*pw,0.5f*h-1.5f*pw);
//sr.end();
}
return sr;
}
}
FYI.. I am using this libGDX ShapeRenderer api to draw few shapes. http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html
Apologies if it's very basic concept and I am unable to figure it out
Thanks.