"Offset tile placement by one pixel" confusion

My game uses a tilemap configured by a text file that is in my java project, the engine reads the text file, gets the information for the bricks, organises them by column for easy searching and then displays them.

To work out where the tiles need to be placed on the x-axis it starts with the first visible tile on the left and then places tiles until the last visible tiles on the right spacing them using this code:

BufferedImage im = (BufferedImage) brickImages.get(0);
    imWidth = im.getWidth() -1;
    imHeight = im.getHeight();

You’ll notice that I’ve had to offset the im.getWidth() variable by -1, without this my tiles were placed 1 pixel apart from each other on the x-axis and I cannot understand where this extra pixel is coming from?

Any takers?

An Images getWidth() / getHeight() methods return the images width and height, so my guess is that your image is wider than what is being draw ( transparent borders? ). Why don’t you upload the image tile here or look it over yourself.

here we are:

also surprisingly, when I first initialised the tileset, I forgot to edit it to remove the 1 pixel width space between them and thought this was the issue, I removed this and rebuilt the project but to no avail…

Well they’re all 32x32. Since they’re in a sheet, maybe there’s a bug in how you split them up into their own images?

Or in the draw method?

I’d like to see more of your code. You might just be making a silly mistake somewhere that you haven’t spotted.

Ok well here’s the code that reads the bricks file (i.e. tile) and stores the information line by line in an array:

private void storeBricks(String line, int lineNo, int numImages)
  /* Read a single bricks line, and create Brick objects.
     A line contains digits and spaces (which are ignored). Each
     digit becomes a Brick object.
     The collection of Brick objects are stored in the bricksList
     ArrayList.
  */
  {
    int imageID;
    for(int x=0; x < line.length(); x++) {
      char ch = line.charAt(x);
      if (ch == ' ')   // ignore a space
        continue;
      if (Character.isDigit(ch)) {
        imageID = ch - '0';    // we assume a digit is 0-9
        if (imageID >= numImages)
          System.out.println("Image ID " + imageID + " out of range");
        else   // make a Brick object
          bricksList.add( new Brick(imageID, x, lineNo) );
      }
      else
        System.out.println("Brick char " + ch + " is not a digit");
    }
  }  // end of storeBricks()

And this is the code that reads the brick arrays (they get transfered into columns rather than rows earlier on in the code) and displays them column by column:

 private void drawBricks(Graphics g, int xStart, int xEnd, int xBrick)
  /* Draw bricks into the JPanel starting at xStart, ending at xEnd.
     The bricks are drawn a column at a time, separated by imWidth pixels.

     The first column of bricks drawn is the one at the xBrick location 
     in the bricks map.
  */
  { int xMap = xBrick/imWidth;   // get the column position of the brick
                                 // in the bricks map
    // System.out.println("xStart: " + xStart + "; xEnd: " + xEnd);
    // System.out.println("xBrick: " + xBrick + "; xMap: " + xMap);
    ArrayList column;
    Brick b;
    for (int x = xStart; x < xEnd; x += imWidth) {
      column = columnBricks[ xMap ];   // get the current column
      for (int i=0; i < column.size(); i++) {   // draw all bricks
         b = (Brick) column.get(i);
         b.display(g, x);   // draw brick b at JPanel posn x
      }
      xMap++;  // examine the next column of bricks
    }
  }  // end of drawBricks()

If you can see any obvious mistakes in those lines of code, please tell me, I can’t see wood for trees at the moment!

The tiles are all 32x32, but have you checked if the tiles drawn are 32x32?

I had a problem exactly like this one some time ago, since my load function was cutting off the tile’s last column, making them 31x32

without taking a long look into the code I can’t be exactly sure right now, but I do know that the width of a tile is found using im.width and is used in the draw process.