//---------------------------------------------------------------- // Mr. Potatoe Head applet // as seen on the Internet at http://www.gumption.org/three/potato // // Written by Tony Sirna, sirna@cs.stanford.edu // // Copyright 1997 by BK Skinner, bk@gumption.org // (except, of course, for the ImageCanvas stuff at the bottom) // //---------------------------------------------------------------- import java.awt.*; import java.applet.*; public class PotatoHead extends Applet implements Runnable { //MediaTrackers are a pain in the butt. I think it actually works now. MediaTracker mt ; public Frame anchorFrame; //for setting cursor static boolean DEBUG = true; final int DONE = (MediaTracker.ABORTED | MediaTracker.ERRORED | MediaTracker.COMPLETE); int kImgHeight; //Determined by the actual size of the image int kImgWidth; String hatNames[] = {"","Mickey.gif","Partyhat.gif","Armyhat.gif","Bikehelm.gif","Panama.gif","Sackhat.gif","Beret.gif"}; String hairNames[] = {"","Longhair.gif","Redlong.gif", "Yellhair.gif","Grnhair.gif","Firehair.gif","Mohawk.gif","Redchin.gif","Shrthair.gif","Redshort.gif"}; String facialNames[] = {"","Fullbrd.gif","Goatee.gif","Redfu.gif","Amish.gif","Moustach.gif"}; String glassesNames[] = {"","Sunwrap.gif","Glaslenn.gif","Glasspop.gif","Redcool.gif"}; String shirtNames[] = {"","Leather.gif","Gturtle.gif","Suit.gif"}; String backgroundNames[] = {"","Sanfran.jpg","Kremlin.jpg"}; Image hatImgs[] = new Image[hatNames.length]; Image hairImgs[] = new Image[hairNames.length]; Image facialImgs[] = new Image[facialNames.length]; Image glassesImgs[] = new Image[glassesNames.length]; Image shirtImgs[] = new Image[shirtNames.length]; Image backgroundImgs[] = new Image[backgroundNames.length]; Image basic,cigImg,gasMaskImg; //-1 beacuse of the none option. These are used in PrePaint int kNumHat = hatNames.length - 1; int kNumHair = hairNames.length - 1 ; int kNumFacial = facialNames.length - 1; int kNumGlasses = glassesNames.length - 1; int kNumShirt = shirtNames.length - 1; int kNumBack =backgroundNames.length - 1; //This is the image for offscreen double buffering. public Image offscreenImage; Graphics offscreenGraphics; int numImages=0; //total images in media tracker //This is called by the regular init method but this separates out //our code from the generic layout crap. Basically this //sets up all the images but they are loaded asynchronously in a different thread. void myInit() { Object anchorpoint = getParent(); while (! (anchorpoint instanceof Frame)) anchorpoint = ((Component)anchorpoint).getParent(); anchorFrame = (Frame)anchorpoint; //Have to set up can here so it can be passed to mt can = new ImageCanvas(); mt= new MediaTracker(can); basic = getImage(getCodeBase(),"images/" + "Basic.gif"); mt.addImage(basic,numImages++); cigImg= getImage(getCodeBase(),"images/" + "Cigarett.gif"); mt.addImage(cigImg,numImages++); gasMaskImg= getImage(getCodeBase(),"images/" + "Gasmask.gif"); mt.addImage(gasMaskImg,numImages++); //Start at 1 because first entry is for None for(int i=1;i 0) { offscreenGraphics.drawImage(backgroundImgs[Background.getSelectedIndex()],0,0,this); mt.waitForID(2+Background.getSelectedIndex()); } offscreenGraphics.drawImage(basic,0,0,this); if(Clothes.getSelectedIndex() > 0) { offscreenGraphics.drawImage(shirtImgs[Clothes.getSelectedIndex()],0,0,this); mt.waitForID(2+kNumBack + Clothes.getSelectedIndex()); } if(Facial.getSelectedIndex() > 0) { offscreenGraphics.drawImage(facialImgs[Facial.getSelectedIndex()],0,0,this); mt.waitForID(2 + kNumBack + kNumShirt + Facial.getSelectedIndex()); } if(Hair.getSelectedIndex() > 0) { offscreenGraphics.drawImage(hairImgs[Hair.getSelectedIndex()],0,0,this); mt.waitForID(2 + kNumBack + kNumShirt + kNumFacial+ Hair.getSelectedIndex()); } if(gasMask.getState()) { offscreenGraphics.drawImage(gasMaskImg,0,0,this); mt.waitForID(2); } if(Hat.getSelectedIndex() > 0) { offscreenGraphics.drawImage(hatImgs[Hat.getSelectedIndex()],0,0,this); mt.waitForID(2 + kNumBack + kNumShirt + kNumFacial+ kNumHair + Hat.getSelectedIndex()); } if(Glasses.getSelectedIndex() > 0 && !gasMask.getState()) { offscreenGraphics.drawImage(glassesImgs[Glasses.getSelectedIndex()],0,0,this); mt.waitForID(2 + kNumBack + kNumShirt + kNumFacial+ kNumHair + kNumHat +Glasses.getSelectedIndex()); } if(Cigarette.getState() && !gasMask.getState()) { offscreenGraphics.drawImage(cigImg,0,0,this); mt.waitForID(1); } } catch (Exception e) { if (DEBUG) System.out.println("In Prepaint " + e); e.printStackTrace(); } if (DEBUG) System.out.println("Finish prepaint"); } //Does the offscreen drawing and the repaints the canvas public void paint(Graphics g) { if (DEBUG) System.out.println("In paint"); prePaint(); can.repaint(); if (DEBUG) System.out.println("Done with Paint"); } //This is where the images get loaded in a different thread public void run() { if (DEBUG) System.out.println("In run"); try { //This loads the basic image and then does a repaint. mt.waitForID(0); repaint(); for (int i=1; isize().width) w=size().width; if (h >size().height) w=size().width; if (w >= 0 && h >= 0) { g.drawImage(image, (size().width-w)/2, (size().height-h)/2, this); } } potato.statusBar.hide(); potato.anchorFrame.setCursor(Frame.DEFAULT_CURSOR); } }