Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  "Offset tile placement by one pixel" confusion  (Read 1902 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Member





« Posted 2012-04-21 22:09:24 »

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:

1  
2  
3  
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?
Offline jonjava
« Reply #1 - Posted 2012-04-22 01:20:19 »

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.

Offline westloar

Junior Member





« Reply #2 - Posted 2012-04-22 10:01:36 »

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...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2012-04-22 10:09:49 »

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?

Offline Dream Of Sleeping
« Reply #4 - Posted 2012-04-22 14:32:48 »

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

Offline westloar

Junior Member





« Reply #5 - Posted 2012-04-22 20:59:12 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
 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!
Offline Rorkien
« Reply #6 - Posted 2012-04-22 21:11:23 »

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
Offline westloar

Junior Member





« Reply #7 - Posted 2012-04-22 21:23:04 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!