/* File: GIFCanvasApplet.java * Copyright (C) 1998 by Bill Giel/KCMDG. All rights reserved. * * This little applet demonstrates use of the GIFCanvas package * to display animated GIF's in a Java applet (or application.) */ import java.applet.*; import java.awt.*; import java.net.*; import com.kcmultimedia.gifcanvas.GIFCanvas; public class GIFCanvasApplet extends Applet { public static final String MSG = "Select an image, press Show"; Choice picker; Button show; GIFCanvas canvas; boolean hasImage; TextField lbl; public void init() { setBackground(new Color(204,204,204)); Panel p = new Panel(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(new BorderLayout()); p.setLayout(gridbag); picker = new Choice(); show = new Button("Show"); canvas = new GIFCanvas(); lbl = new TextField(MSG); lbl.setEditable(false); picker.addItem("gears.gif"); picker.addItem("compass.gif"); picker.addItem("world.gif"); picker.addItem("eye.gif"); picker.addItem("book.gif"); c.weightx=1; c.weighty=0; c.gridwidth=1; c.fill=GridBagConstraints.HORIZONTAL; c.anchor=GridBagConstraints.EAST; c.insets = new Insets(5,5,5,2); gridbag.setConstraints(picker,c); p.add(picker); c.weightx=0; c.weighty=0; c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.NONE; c.anchor=GridBagConstraints.WEST; c.insets = new Insets(5,2,5,5); gridbag.setConstraints(show,c); p.add(show); c.weightx=1; c.weighty=1; c.gridwidth=GridBagConstraints.REMAINDER; c.fill=GridBagConstraints.BOTH; c.anchor=GridBagConstraints.CENTER; c.insets = new Insets(5,5,5,5); gridbag.setConstraints(canvas,c); p.add(canvas); add("Center",p); add("South",lbl); } //When the applet stops, stop the animation thread. public void stop() { if(hasImage){ canvas.stop(); } super.stop(); } //If we restart from a backlink, and we had an image, we can simply //restart the animation. Netscape Navigator does this correctly. IE //apparently does not. public void start() { if(hasImage){ canvas.start(); } } public boolean action(Event e, Object arg) { URL imageurl = null; if(e.target.equals(show)){ lbl.setText("Loading image..."); canvas.hide(); try{ imageurl = new URL(getCodeBase(),"/" + picker.getSelectedItem()); canvas.setImage(imageurl); canvas.show(); lbl.setText(MSG); hasImage=true; canvas.start(); }catch(Exception ee){ lbl.setText("An error occured!"); if(imageurl != null) System.out.println("Error loading image: " + imageurl.toString()); hasImage=false; } return true; } else return super.action(e,arg); } }