import java.applet.*; import java.net.*; import java.awt.*; /** Java applet for animal (or other things) animation with sound effects. When mouse enters applet and when clicked, two special images animate the picture and two different sounds are played. By Martin Lovell, 1997 Feel free to copy, modify, and reuse this source. **/ public class Animals1 extends Applet { AudioClip snd; AudioClip sndClick; int imageWidth, imageHeight; int replaceImageWidth, replaceImageHeight, replaceImageX, replaceImageY; Image mainImage, replaceImage1, replaceImage2; String otherAnimalName; Color color; int triState = MOUSE_NOT_OVER; final static int MOUSE_NOT_OVER = 1; final static int MOUSE_OVER = 2; final static int MOUSE_DOWN = 3; //convertion routine from a java book public Color colorFromString(String s, Color defaultColor){ if (s == null) return defaultColor; try { int i = Integer.parseInt(s,16); return new Color(i); } catch (Exception e) { return defaultColor; } } //convertion routine to catch errors public int stringToInt(String s, int defaultInt) { if (s == null) return defaultInt; try { int i = Integer.parseInt(s,10); return i; } catch (Exception e) { return defaultInt; } } public void init() { //read HTML parameters String mainImageName = getParameter("mainImage"); String image1Name = getParameter("replaceImage1"); String image2Name = getParameter("replaceImage2"); otherAnimalName = getParameter("otherAnimalName"); imageWidth = stringToInt(getParameter("imageWidth"), 200); imageHeight = stringToInt(getParameter("imageHeight"), 300); replaceImageWidth = stringToInt(getParameter("replaceImageWidth"), 200); replaceImageHeight = stringToInt(getParameter("replaceImageHeight"), 200); replaceImageX = stringToInt(getParameter("replaceImageX"), 100); replaceImageY=stringToInt(getParameter("replaceImageY"), 100); color = colorFromString(getParameter("color"),Color.white); //set the background color setBackground(color); String sound1 = getParameter("sound"); String sound2 = getParameter("soundClick"); if (image1Name != null) replaceImage1=getImage(getCodeBase(), image1Name); if (image2Name != null) replaceImage2=getImage(getCodeBase(), image2Name); if (mainImageName != null) mainImage=getImage(getCodeBase(), mainImageName); if (sound1 != null) snd = getAudioClip(getDocumentBase(), sound1); if (sound2 != null) sndClick = getAudioClip(getDocumentBase(), sound2); } public boolean mouseEnter(Event e, int x, int y){ if (snd != null) { snd.play(); } triState = MOUSE_OVER; repaint(); return true; } public boolean mouseDown(Event evt, int x, int y) { triState = MOUSE_DOWN; if (otherAnimalName != null) { Animals1 otherAnimal = (Animals1)getAppletContext().getApplet(otherAnimalName); if (otherAnimal != null) { otherAnimal.triState = MOUSE_DOWN; otherAnimal.repaint(); } } repaint(); if (sndClick != null) { sndClick.play();} return true; } public boolean mouseUp(Event evt, int x, int y) { triState = MOUSE_OVER; repaint(); if (sndClick != null) { sndClick.play(); } return true; } public boolean mouseExit(Event e, int x, int y){ triState = MOUSE_NOT_OVER; repaint(); return true; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (mainImage!=null) g.drawImage(mainImage,0,0,imageWidth,imageHeight,this); switch (triState) { case MOUSE_OVER: if (replaceImage1!=null) g.drawImage(replaceImage1,replaceImageX,replaceImageY,replaceImageWidth, replaceImageHeight,this); break; case MOUSE_DOWN: if (replaceImage2!=null) g.drawImage(replaceImage2,replaceImageX,replaceImageY,replaceImageWidth, replaceImageHeight,this); break; } } }