//
// $Id: TrimmedTile.java,v 1.1 2002/05/06 18:08:32 mdb Exp $

package com.threerings.media.tile;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;

import com.samskivert.util.StringUtil;

import com.threerings.media.util.ImageUtil;

/**
 * Behaves just like a regular tile, but contains a "trimmed" image which
 * is one where the source image has been trimmed to the smallest
 * rectangle that contains all the non-transparent pixels of the original
 * image.
 */
public class TrimmedTile extends Tile
{
    /**
     * Creates a trimmed tile using the supplied tileset image with the
     * specified bounds and coordinates.
     *
     * @param tilesetSource the tileset image that contains our trimmed
     * tile image.
     * @param bounds contains the width and height of the
     * <em>untrimmed</em> tile, but the x and y offset of the
     * <em>trimmed</em> tile image in the supplied tileset source image.
     * @param tbounds the bounds of the trimmed image in the coordinate
     * system defined by the untrimmed image.
     */
    public TrimmedTile (Image image, Rectangle bounds, Rectangle tbounds)
    {
        super(image, bounds);
        _tbounds = tbounds;
    }

    // documentation inherited
    public void paint (Graphics2D gfx, int x, int y)
    {
        int vx = x + _tbounds.x, vy = y + _tbounds.y;
        Shape oclip = gfx.getClip();
        gfx.clipRect(vx, vy, _tbounds.width, _tbounds.height);
	gfx.drawImage(_image, vx - _bounds.x, vy - _bounds.y, null);
        gfx.setClip(oclip);
    }

    // documentation inherited
    public Image getImage ()
    {
        String errmsg = "Can't convert trimmed tile to image " +
            "[tile=" + this + "].";
        throw new RuntimeException(errmsg);
    }

    // documentation inherited
    public boolean hitTest (int x, int y)
    {
        return ImageUtil.hitTest(_image, _bounds.x + x, _bounds.y + y);
    }

    // documentation inherited
    protected void toString (StringBuffer buf)
    {
	buf.append(", tbounds=").append(StringUtil.toString(_tbounds));
    }

    /** The dimensions of the trimmed image in the coordinate space
     * defined by the untrimmed image. */
    protected Rectangle _tbounds;
}

@@ -29,29 +29,29 @@
 v 1.
-1
+2
  2002/0
-5/06 18:0
+6/19 08:2
 8:
-3
 2
+5
  mdb
@@ -118,18 +118,16 @@
 Graphics
-2D
 ;%0Aimport
@@ -190,24 +190,62 @@
 awt.Shape;%0A%0A
+import java.awt.image.BufferedImage;%0A%0A
 import com.s
@@ -1338,10 +1338,8 @@
 hics
-2D
  gfx
@@ -1373,154 +1373,80 @@
    i
-nt vx = x + _tbounds.x, vy = y + _tbounds.y;%0A        Shape oclip = gfx.getClip();%0A        gfx.clipRect(vx, vy, _tbounds.width, _tbounds.height);%0A%09
+f (_subimage == null) %7B%0A            createSubImage();%0A        %7D%0A        
 gfx.
@@ -1460,21 +1460,24 @@
 ge(_
+sub
 image, 
-v
 x 
--
++
  _
+t
 boun
@@ -1486,14 +1486,14 @@
 .x, 
-v
 y 
--
++
  _
+t
 boun
@@ -1509,36 +1509,8 @@
 l);%0A
-        gfx.setClip(oclip);%0A
     
@@ -1880,32 +1880,467 @@
 .y + y);%0A    %7D%0A%0A
+    // documentation inherited%0A    protected void createSubImage ()%0A    %7B%0A        if (_image instanceof BufferedImage) %7B%0A            _subimage = ImageUtil.getSubimage(_image, _bounds.x, _bounds.y,%0A                                              _tbounds.width, _tbounds.height);%0A        %7D else %7B%0A            String errmsg = %22Can't obtain tile image %5Btile=%22 + this + %22%5D.%22;%0A            throw new RuntimeException(errmsg);%0A        %7D%0A    %7D%0A%0A
     // documenta

Switch to using a subimage instead of clipping because clipping creates too much garbage.