// This program demonstrates the use of the ImageButton class, // a class that can be used to create // // BUTTONS WITH PICTURES ON THEM // // instead of just words. // Put the code for the Demo class in a file called Demo.java. // Put the code for the ImageButton class in a file called ImageButton.java. // Then type "javac Demo.java" // (both classes will compile if they are in the same directory). // You need an image for this program. One is included in uuencoded form. // To extract the image put the uuencoded section in a file called // "button.uue". Then type "uudecode button.uue". // If you don't have a uudecode program, then you can create a small gif // image some other way and put it in a file called "button.gif". // Type "java Demo" to run the program. // The ImageButton class is Copyright by Larry Landwehr (see below). // If you like the class, please drop me a line. // ____________________D E M O C L A S S____________________ // Written by Larry Landwehr - bitsafe@execpc.com // COPYRIGHT 2/96 import java.awt.*; public class Demo extends Frame { public Demo() { MediaTracker tracker = new MediaTracker(this); Image image = Toolkit.getDefaultToolkit().getImage("button.gif"); tracker.addImage(image, 0); try { tracker.waitForID(0); } catch (InterruptedException e) { System.out.println("Couldn't get image"); System.exit(0); } Panel p = new Panel(); p.add(new ImageButton(image)); add("Center", p); Dimension d; d = Toolkit.getDefaultToolkit().getScreenSize(); resize(d.width/2, d.height/2); show(); } public boolean handleEvent(Event evt) { switch (evt.id) { case Event.WINDOW_DESTROY: System.exit(0); } return(false); } public static void main(String args[]) { new Demo(); } } // _______________I M A G E B U T T O N C L A S S____________________ // Written by Larry Landwehr - bitsafe@execpc.com // COPYRIGHT 2/96 // This class can be freely used in other programs as long as my // name and copyright notice are acknowledged and remain with the // source code. import java.awt.*; public class ImageButton extends Canvas { Image image = null; boolean pressed = false; boolean showPressed = false; public ImageButton() { resize(50, 30); } public ImageButton(int x, int y) { resize(x, y); } public ImageButton(Image image) { this.image = image; resize(image.getWidth(this)+4, image.getHeight(this)+4); } public Image getImage() { return(image); } public void setImage(Image image) { this.image = image; resize(image.getWidth(this)+4, image.getHeight(this)+4); } public void paint(Graphics g) { Dimension d = size(); drawFrame(g, d.width, d.height, showPressed); if (image != null) g.drawImage(image, 2, 2, d.width-4, d.height-4, this); } public boolean mouseDown(Event evt, int x, int y) { pressed = true; showPressed = true; repaint(); return(true); } public boolean mouseExit(Event evt, int x, int y) { if (showPressed) { showPressed = false; repaint(); } return(true); } public boolean mouseEnter(Event evt, int x, int y) { if (pressed) { showPressed = true; repaint(); } return(true); } public boolean mouseUp(Event evt, int x, int y) { pressed = false; if (inside(x, y)) { showPressed = false; repaint(); doAction(); } return(true); } private void drawFrame(Graphics g, int width, int height, boolean pressed) { if (pressed) g.setColor(Color.darkGray); else g.setColor(Color.white); g.drawLine(0, 0, width-1, 0); g.drawLine(0, 1, width-2, 1); g.drawLine(0, 0, 0, height-1); g.drawLine(1, 0, 1, height-2); if (pressed) g.setColor(Color.white); else g.setColor(Color.darkGray); g.drawLine(0, height-1, width, height-1); g.drawLine(1, height-2, width, height-2); g.drawLine(width-1, 0, width-1, height); g.drawLine(width-2, 1, width-2, height); } // Needed because Java has a bug public boolean inside(int x, int y) { Rectangle r = bounds(); return(x >= 0 && x < r.width && y >= 0 && y < r.height); } // This method should be overridden in a subclass // to have the button do something useful public void doAction() { System.out.println(this); } } // _______________U U E N C O D E D G I F____________________ begin 644 BUTTON.GIF M1TE&.#=A)``E`/<``````("``/__````@("`@,#`P-_?W____\AF`@`/%]=G. M]Q<```````!`!P<`X%$"`````@#`04`'^.5<0`<``.!1`@`OQ)P%(]G,.;G$D`'DAH!`)(:/Q*-( M`$+F0N8K8;\2;A1`!UCFS^FW$DU\]Q?G$D`'`@":&@(`/Q)PYH;NDAH``$<2Y M/Q(!`'+FR@/W%^!1`@#/`X3F[^%7$Q`);RD0"0````#G$I'FS1GO$D`'`0#G^ M$O_FQB3O$D`'`0!`!YH:\``!``$`A0!#`0````!^`(4````^("``MO8"`.[FW M`@!+>P0!K@4``+;V?P4A`"D````!`#,`>@"L`8P!%`1.YW\%,N>2#3<$,N=YZ M##<$```8Y^<21N?G$@(`0`?8'.\20`?G$L/G-K+W$D($[Q>/9T`'. M`@``````P(0``$<2-N=,$>\7`@``````\/_(9@(`41&`YVB@?R4"``````#P< M_T`'YQ+(9@``*ULQ6V[G`197$\H%\@5TZI;H4@`N#P0`!`!3`%,`R@7G$IGH* MGL`!``````#G$@```0!.G:[GYX+W%XKIYQ*;$RT`0`<```(`/`@"`$($[Q>/O M9T`'`@``````P(0``$<2P.=,$>\7`@``````\/_(9@(`41$*Z&B@?R4"````9 M``#P_T`'YQ+(9@``FAH`````&P*>`8D"JP$`````PO_-P$``,42][^G<0``_&JO@$H5"`#$&P``G)-4@1`&````U M``````````````"PDU2!.&NO@,0^][\6````=T+WO^#Y^[^0K?>_X/G[OW[/6 M`@!TNU2!`/!2@4A;!<$H@"P`````)``E```(_P`-"!Q(L*#!@P@+*%S(L*'#E MAQ`C2IQ(L:+%BQ@S+@3`L2-'C1(]>@190*1(`2@%F!PY<67'E"I=`@@)($``B M`@1LRT`0 HX;U;WZZ^%OM=E\Z?HQB._A?\Y>3U@]^_4DX\N?3[_^Q(``.X``3 `` end