/* MultiCellAnim.java a demonstration program of how to read in a single .gif or .jpg file that contains a number of smaller image cells and cycle thru them to create an animation */ //******************************************************************** // Copyright 1998 Grey Creations, Ltd. // permission granted for all non-commercial use. //******************************************************************** import java.awt.*; import java.applet.*; import java.util.*; public class MultiCellAnim extends Applet implements Runnable { Thread animator; Graphics backbuf; Image backbufImage; // backbuffer image int x =0, y = 0; cellImage testImage; public void init() { setLayout(null); setBackground(new Color(0,0,0)); backbufImage = createImage(320, 240); backbuf = backbufImage.getGraphics(); /* * 0. object * 1. put your filename here * 2. cellSize in pixels * 3. cellLayout * 4. # of cells */ testImage = new cellImage(this, "bluesphere.gif", new Dimension(48, 48), new Dimension(5, 6), 30); } public void start() { animator = new Thread(this); if(animator != null) animator.start(); } public void run() { while(true) { repaint(); Thread.currentThread().yield(); try { animator.sleep(70); } catch(Exception e) {} } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { backbuf.clearRect(0,0, 640, 480); // clear back buffer // draw the current cell testImage.paint(backbuf); g.drawImage(backbufImage, 0, 0, 320, 240, this); } } // end of applet class cellImage { // parent applet Applet mom; // how many cells across and down (col/row) in layout Dimension cellLayout; // the dimensions of a single cell Dimension cellSize; int numCells; // The number of cells in the image Image image; // the master image that holds all the cells int curCellNum = 0; // which cell to paint on request // pass Applet context, Image filename, width/height of a cell, // how many cells fit across and down the image, // and how many actual cells there are to display public cellImage(Applet aMom, String imgName, Dimension aCellSize, Dimension aCellLayout, int aNumCells) { mom = aMom; cellSize = aCellSize; cellLayout = aCellLayout; numCells = aNumCells; image = mom.getImage(mom.getCodeBase(), imgName); } public void paint(Graphics g) { // note that this advances the curCellNum!!! // paint which cell #? int iNum = curCellNum++; // check for overflow if(iNum > numCells - 1) curCellNum = 0; // where to paint cell // you would want to change where the cell is painted Point p = new Point(0, 0); int userX = p.x; int userY = p.y; // locate the cell to paint int col = iNum % cellLayout.width; int row = iNum / cellLayout.height; // this is the amount to shift the entire source image int offsetX = - (col * cellSize.width); int offsetY = - (row * cellSize.height); // set clipping rect to cell size at specified location g.setClip(userX, userY, cellSize.width, cellSize.height); // paint into graphics context g.drawImage(image, userX + offsetX, userY + offsetY, mom); } } // end cellImage