0

Probably missing something basic, but I have a for-loop in my program that's supposed to iterate over an array (lines[]) and draw them onscreen (using LWJGL/Slick).
Instead of the lines being shown on separate lines (at (i*16 + 7, 5)), they all appear at (7, 5).

for(int i = 0; i < lines.length; i++) {
  if(lines[i] != null) {
    f.drawString((i * 16) + 7, 5, lines[i]);
  }
}

I have seen answers on other questions suggesting to define a final variable in the loop to avoid the value being changed before the called function uses it. So I have changed the code to:

for(int i = 0; i < lines.length; i++) {
  if(lines[i] != null) {
    final int i2 = i;
    f.drawString((i2 * 16) + 7, 5, lines[i]);
  }
}

But it doesn't fix it (didn't really expect it to, as I'm not getting stuck on the last value of i and Slick shouldn't be storing the value of i, but tried it to make sure).


The header of drawString is as follows:

public void drawString(float x, float y, String whatchars)
CosmicGiant
  • 6,275
  • 5
  • 43
  • 58
  • (keep in mind) If i == 0, i * 16 == 0 too. – sschrass Nov 09 '12 at 18:57
  • I think you may have your arguments in the wrong order. What kind of object is `f` (Fixed wrong object name) – Chad Campbell Nov 09 '12 at 18:57
  • For the first iteration, yes, but when i increases to 1 in the next iteration, 1*16 seems to be evaluated to 0 too. – GinjaNinja32 Nov 09 '12 at 18:58
  • Don't know the arguments, but could it be you're drawing each new object with a horizontal shift instead of a vertical shift? – Kenogu Labz Nov 09 '12 at 18:58
  • r? I don't have an r, do you mean f? TrueTypeFont, the method is public void drawString(float x, float y, String whatchars) – GinjaNinja32 Nov 09 '12 at 18:59
  • Are you using an IDE to code? Can you debug the code and set a breakpoint to watch the variables there? EDIT: Also keep in mind, if you're looking to offset the *LINES* you need to have the (i * 16) constant in the `y` position. – Grambot Nov 09 '12 at 19:00
  • Also, check your lines[] array. If the only non-null value in the array is at lines[0] that's all you're going to get, is (7,5) for lines drawn. – JWiley Nov 09 '12 at 19:01
  • 1
    @TheCapn, that's it. Said it'd be basic. EDIT: wrong person. – GinjaNinja32 Nov 09 '12 at 19:03
  • You do have the horizontal shift instead of the vertical shifting that you want. Try using debug tools to look at the i-value each iteration or adding a println for the value of i. – Chad Campbell Nov 09 '12 at 19:04
  • Have you done any research, such as printing the values or using an IDE debugger? – Jim Garrison Nov 09 '12 at 19:10

2 Answers2

2

I guess my comment supplied the answer so I'll post for others to help.

The correct call is to put the offset in the y parameter of the function:

f.drawString(7, (i * 16)+5, lines[i]);

As the function definition is:

public void drawString(float x, float y, String whatchars)
Grambot
  • 4,370
  • 5
  • 28
  • 43
0

I think you need to change the y co-ordinate not x as below:

for(int i = 0; i < lines.length; i++) {
  if(lines[i] != null) {
   f.drawString(7, (i * 16) +5, lines[i]);
  }
 }

This will print the lines from x = 7 and y varying from 5 to 20.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73