-3

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.

jiaweizhang
  • 809
  • 1
  • 11
  • 28
kingAm
  • 1,755
  • 1
  • 13
  • 23
  • The obvious values to check would be `x+w+5` compared to `x + i*(w+gap)` – chancea Jun 24 '15 at 16:48
  • 2
    Not sure if this is the prob. But you are comparing strings with "==" operator. Use equals() method. – Raman Shrivastava Jun 24 '15 at 16:51
  • @chancea Checked that value, no problem there. Raman thats not problem otherwise those 2 straight line also didn't give me result. I just wanted to know, shouldn't for loop should do exact same thing for those 2 lines? – kingAm Jun 24 '15 at 16:54
  • @Raman that's correct actually. That was the problem. Answer it properly I will accept. Characters.toString() is referring to different object which is not equivalent to "B" reference. Thanks for pointing it out. – kingAm Jun 24 '15 at 17:00
  • How can you compare my question with that? I couldn't spot that it was the problem in my code. I was thinking it might have something to do with the execution of for loop object.Thanks for discouragment. I will delete it, nvm. – kingAm Jun 25 '15 at 09:41

1 Answers1

1

Use equals() method to compare strings. "==" checks only reference not the content of the string.

Raman Shrivastava
  • 2,923
  • 15
  • 26
  • What a day :( wasted too much time for this silly mistake. Hope someone will benefit from this post in the future. – kingAm Jun 24 '15 at 17:04