Load several images from a single GIFTag(s): AWT
It's a good idea to combine small GIFs into a single big one to speed up the loading process. In the following snippet, I assume that all images are the same height and width. You may want to get this GIF (

import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class strip extends Applet{ int iconHeight = 16; int iconWidth = 16; int iconCount = 2; Image icon[] = new Image[iconCount]; Image allIcons; public void init(){ loadImages("item.gif"); } public void paint(Graphics g) { g.drawImage(allIcons, 0, 0, this); g.drawImage(icon[0], 0, 20, this); g.drawImage(icon[1], 0, 40, this); } public void loadImages(String s) { MediaTracker t=new MediaTracker(this); allIcons=createImage(1,1); try { URL u=new URL(getCodeBase(), s); allIcons=Toolkit.getDefaultToolkit().getImage(u); t.addImage(allIcons,0); } catch (MalformedURLException me) { System.out.println("MalformedURLException: " + me); } try { t.waitForAll(15000); } catch (InterruptedException e) { System.out.println("interrupted"); } for (int i=0; i < iconCount; i++) { Image z=createImage(iconWidth,iconHeight); Graphics g=z.getGraphics(); g.clipRect(0,0,iconWidth,iconHeight); g.drawImage(allIcons,-i*iconWidth,0,this); icon[i]=z; } } }
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com