import browser.*; import awt.*; // This is a simple applet that displays a number of icons on a background // and lets you drag them around. // // Calling attributes // bg=name_of_gif_file - background image // sml=directory_name - directory to find the icon images // T#.gif (normal) and S#.gif (selected) // starting T1.gif T2.gif T3.gif etc etc // If no S#.gif then the image is copied from the // T#.gif. Icon loading stops when we can't find // the next T#.gif in sequence // // // Internal details: // // Each icon has four attribues: // smallImage - the normal displayed image // selImage - the image to display when it is selected // xpos,ypos - the top-left co-ordinate of the icon // These attributes are stored as arrays (yeah, yeah, OK not an object) // with the array index being used // eg Icon 0 has smallImage[0] selImage[0] xpos[0] ypos[0] // Icon 1 has smallImage[1] selImage[1] xpos[1] ypos[1] // etc // // We use double buffering to try and avoid flickering (no backward hacks // for Alpha2 browsers - double buffer or die :-)) // // When the mouse button is pressed, we determine if we are over an icon, // and if so select it. When dragging, we make the selected icon follow the // mouse, and when released we deselect the icon. 'select' is a simple // int acting as the array index to determine the icon. class game extends Applet { Image baseImage; // The background image Image offscrImage; // Used for offscreen drawing to stop flickering Graphics background; // using double-buffering Image smallImage[]; // The small 'icon' images Image selImage[]; // How the image looks when selected/dragged int xpos[]; // X co-ordinate of each icon - topleft corner int ypos[]; // Y co-ordinate of each icon - topleft corner int fudgex; // Offset between corner of icon and cursor when int fudgey; // mouse is clicked. int numimgs = 0; // Number of icons loaded int selected = -1; // Current icon selected // Startup routine public void init() { String s; // Attribute "bg" is the background image. If none specified then silly duke s=getAttribute("bg"); if (s==null) s="doc:/demo/images/duke/T01.gif"; baseImage=getImage(s); // The game area is the size of this image resize(baseImage.width,baseImage.height); // Create the double-buffered area of the same size offscrImage=createImage(baseImage.width,baseImage.height); background = new Graphics(offscrImage); // We allow up to 40 (arbitrary) icons smallImage = new Image[40]; selImage= new Image[smallImage.length]; xpos = new int[smallImage.length]; ypos = new int[smallImage.length]; // Get the directory to find the icon images in. If none, then // just have one icon being the duke s=getAttribute("sml"); if (s==null) { numimgs=1; smallImage[0]=getImage("doc:/demo/images/duke/T01.gif"); selImage[0]=getImage("doc:/demo/images/duke/T04.gif"); xpos[0] = ypos[0] = 40; } else { for (int i = 1; i < smallImage.length; i++) { // T images are the normal icons. // S images are the selected icons. Image im1 = getImage(s + "/T" + i + ".gif"); Image im2 = getImage(s + "/S" + i + ".gif"); // If no icon with that name, then we must have finished. if (im1 == null) break; // If no "selected" image, then just use the normal image if (im2 == null) im2=im1; // For lack of anything better, put this icon at (40*i,30) xpos[numimgs]=40*numimgs; ypos[numimgs]=30; smallImage[numimgs] = im1; selImage[numimgs] = im2; numimgs++; } } } // If we press a button, and it's within the area of an icon, then select // the icon and refresh the screen. If multiple icons could match, we // simply select the one with the highest index (eg icon 10 is selected // in preference to icon 2) public void mouseDown(int x,int y) { for (int i=0;i=xpos[i]) && (x=ypos[i]) && (y310) play("doc:///demo/audio/joy.au"); selected=-1; repaint(); } } // This routine gets called when the mouse is dragged. If we are dragging // an icon, then update it's position public void mouseDrag(int x,int y) { if (selected != -1) { xpos[selected]=x-fudgex; ypos[selected]=y-fudgey; repaint(); } } // This draws the image. We get called from 'paint' to do the double // buffering. public void mypaint(Graphics g) { // Draw the background image g.drawImage(baseImage,0,0); // For each icon, draw it (the selected one last so it's ontop) for (int i=0; i