Returns the rectangle containing the specified tile in the supplied larger rectangle. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Returns the rectangle containing the specified tile in the supplied larger rectangle.

Demo Code

// Copyright (C) 2002-2012 Three Rings Design, Inc., All Rights Reserved
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import static com.threerings.geom.Log.log;

public class Main{
    /**/*from   ww  w.j a  v  a2s .  co m*/
     * Returns the rectangle containing the specified tile in the supplied larger rectangle. Tiles
     * go from left to right, top to bottom.
     */
    public static Rectangle getTile(int width, int height, int tileWidth,
            int tileHeight, int tileIndex) {
        Rectangle bounds = new Rectangle();
        getTile(width, height, tileWidth, tileHeight, tileIndex, bounds);
        return bounds;
    }
    /**
     * Fills in the bounds of the specified tile in the supplied larger rectangle. Tiles go from
     * left to right, top to bottom.
     */
    public static void getTile(int width, int height, int tileWidth,
            int tileHeight, int tileIndex, Rectangle bounds) {
        // figure out from whence to crop the tile
        int tilesPerRow = width / tileWidth;

        // if we got a bogus region, return bogus tile bounds
        if (tilesPerRow == 0) {
            bounds.setBounds(0, 0, width, height);

        } else {
            int row = tileIndex / tilesPerRow;
            int col = tileIndex % tilesPerRow;
            // crop the tile-sized image chunk from the full image
            bounds.setBounds(tileWidth * col, tileHeight * row, tileWidth,
                    tileHeight);
        }
    }
}

Related Tutorials