Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can just keep an Image and use an overload of the Graphics object's drawImage method to specify where to draw which part of the image:</p> <pre><code>g.drawImage(image, x1, y1, x2, y2, srcX1, srcY1, srcX2, srcY2); </code></pre> <p>See [javadoc](http://slick.cokeandcode.com/javadoc/org/newdawn/slick/Graphics.html#drawImage(org.newdawn.slick.Image, float, float, float, float, float, float, float, float))</p> <p>The first parameter is the instance of the image. The next two parameter define the point on screen, where the rendering begins. X2 and y2 define the end point of the rendering. Usually x2 is x1 + spriteWidth and y2 is y1 + spriteHeight, but you can change those values to draw the sprite in different sizes. The last four parameters work the same, but they define the area of the sprite sheet, that will be drawn on screen.</p> <p>If we take your example and we want to draw the second tile from the third row the call would look like this:</p> <pre><code>int tileWidth = 8; int tileHeight = 8; int sourceX = 40; int sourceY = 8; //as its the sec int drawX = 34; int drawY = 65; g.drawImage(image, drawX, drawY, drawX + tileWidth, drawY + tileHeight , sourceX, sourceY, sourceX + tileWidth, sourceY + tileHeight); </code></pre> <p>When I work with spritesheet, I have hardcoded values in some (very rare cases, mostly tests) and a sprite class, that has the source x1, x2, y1 and y2 values stored. I can pack a bunch of them in a list or a map and like that I have a sprite index. Usually I generate the definitions somehow and then serialize the list, so I can simply reload that list, if I need it. </p> <p>Here is a short example of my XML definition (I store the width and height rather then the x2 and y2 values in the xml, as I find it more human readable and more convenient for manual editing. After deserialization I calculate the x2 and y1 values):</p> <pre><code>&lt;spriteSheet imageName="buildings" name="buildings"&gt; &lt;sprite name="1x2 industry 01" spriteX="0" spriteY="0" width="50" height="112"/&gt; &lt;sprite name="1x2 quarters 01" spriteX="50" spriteY="0" width="50" height="112"/&gt; &lt;sprite name="1x1 spaceport 01" spriteX="243" spriteY="112" width="51" height="56"/&gt; ... &lt;/spriteSheet&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload