/* * $Id$ * * $Log$ */ /* Lots of graphical/image libraries are needed here. */ import java.awt.Canvas; import java.awt.Component; import java.awt.Container; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.FilteredImageSource; import java.awt.image.RGBImageFilter; import javax.swing.ImageIcon; import javax.swing.JFrame; /** * Represents an image that can be broken into pieces. The pieces are basically tiles for * a game map and must all be the same height/width. * * @author Andy Fanton (arf3827@cs.rit.edu) * @author Jessica D. Bayliss (jdb@cs.rit.edu) * */ public class TiledImage { /** The default tile height in pixels */ public static final int DEFAULT_TILE_HEIGHT = 32; /** The default tile width in pixels */ public static final int DEFAULT_TILE_WIDTH = 32; /* The big image containing sub-images (representing tiles on a game map) */ protected BufferedImage image; /* The big image's height/width and rows/cols */ protected int height, width, rows, cols; /* Where the big image comes from */ protected String sourceFile; /** * Creates a TiledImage from a default file name. Problems occur if the default * is not in the right place in the file system. */ public TiledImage() { // The PondMazeEditor has a hard coded reference to a default load // file in it. The variable is called TILESET. Call the constructor // below with the TILESET variable in PondMazeEditor. this(PondMazeEditor.TILESET); } /** * Creates a TiledImage from a given filename. * * @param fileName The file to create the tiled image from. */ public TiledImage( String fileName ) { // use the this operator and call the method below with the variables: // filename, DEFAULT_TILE_HEIGHT, and DEFAULT_TILE_WIDTH this(fileName,DEFAULT_TILE_HEIGHT,DEFAULT_TILE_WIDTH); } /** * Full Constructor * * @param fileName the name of the file to create the image from * @param tileHeight the height of the individual tiles * @param tileWidth the width of the individual tiles */ public TiledImage( String fileName, int tileHeight, int tileWidth ) { // ah-ha! we must do the actual work of creating the image here // setup the instance variables with the appropriate value: // sourceFile should have value fileName // height should have value tileHeight // width should have value tileWidth sourceFile=fileName; height=tileHeight; width=tileWidth; // Get an icon representing the file // Icons are of type ImageIcon, so we must create a new // ImageIcon object with the fileName as an argument. // Then set rows/cols to be the icon height/width // divided by the tile height/width // so that we're only dealing with the appropriate icon size. ImageIcon icon=new ImageIcon(fileName); rows=icon.getIconHeight()/tileHeight; cols=icon.getIconWidth()/tileWidth; // Give the image variable a value: // image should be of type BufferedImage which is created with the values // cols*width, rows*height, and BufferedImage.TYPE_INT_RGB image=new BufferedImage(cols*width,rows*height,BufferedImage.TYPE_INT_RGB); // Get the source image: // sourceImage is of type Image and is gotten from the above // BufferedImage with its method // called getImage() (no args) Image sourceImage=icon.getImage(); // Draw image into buffer - if we don't do this, // we get black where we should draw // Uncomment the following code for this part: Graphics g = image.getGraphics(); g.drawImage( sourceImage, 0, 0, null ); } /** * Filters the image to create transparency. Assume that this * method works. * * @param sourceImage * @return */ public void addTransparency( Component context, final int backgroundColor ) { // Create anonymous RBG Filter // Anonymous classes are covered in CS2 and give you a way to create very small // classes inside of methods (with their own methods/var's for cases where there isn't enough stuff there // to bother creating a separate class file for it. You will notice that the parent class // (TiledImage) creates a couple of extra class files (TiledImage$1.class, etc.) and these // anonymous classes are the reason for that. RGBImageFilter filter = new RGBImageFilter() { { canFilterIndexColorModel = true; } public int filterRGB( int x, int y, int rgb ) { if ( rgb == backgroundColor ) { //System.out.println( "(" + x + "," + y + "):clear " ); return 0x000000ff; } else { //System.out.println( "(" + x + "," + y + "):" + rgb ); return rgb; } } }; // Create a filtered image source FilteredImageSource newSource = new FilteredImageSource( image.getSource(), filter ); // Create new image using a given component Image newImage = context.createImage( newSource ); image = new BufferedImage( cols * width, rows * height, BufferedImage.TYPE_INT_RGB ); // make sure we draw into the buffer Graphics g = image.getGraphics(); g.drawImage( newImage, 0, 0, null ); } /** * Gets the whole image represented by this object * * @return the image held by this object */ public Image getFullImage(){ // return the real image variable instead of null return image; } /** * Gets the specified tile as an image * * @param x the x index of the desired tile * @param y the y index of the desired tile * @return */ public Image getImage( int x, int y ) { // Image has a method called getSubimage that should be called with // x*width, y*height, width-1, and height-1 // That subimage should then be returned return image.getSubimage(x*width,y*height,width-1,height-1); } /** * Gets the specified tile as an image icon * * @param row the row index of the desired tile * @param col the col index of the desired tile * @return */ public ImageIcon getIcon( int row, int col ) { // return a new ImageIcon object that is created with the Image returned // by getImage above with the appropriate row/col return new ImageIcon(getImage(col,row)); } /** * Gets all of the tiles as an array of images * * @return an image array containing all of the tiles */ public Image[][] getImages() { // oooooh, double array Image[][] returnArray = new Image[rows][cols]; // now write the double for loop that calls getImage across // all columns and then incrementing rows. Note that columns are in // the x direction (horizontal) and rows are in the y direction (vertical). // Then return the array. for(int i=0;i