//AlphaImage.java //Illustrates images in alphabet game //Andy Harris, 05/00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class AlphaImage extends Applet implements ActionListener { Image imgA; Image imgB; Image imgC; Image imgD; Image imgE; Image imgF; Button btnA = new Button("A"); Button btnB = new Button("B"); Button btnC = new Button("C"); Button btnD = new Button("D"); Button btnE = new Button("E"); Button btnF = new Button("F"); Canvas drawSurface = new Canvas(); public void init(){ setLayout(new BorderLayout()); add(drawSurface, BorderLayout.CENTER); Panel pnlSouth = new Panel(); pnlSouth.add(btnA); pnlSouth.add(btnB); pnlSouth.add(btnC); pnlSouth.add(btnD); pnlSouth.add(btnE); pnlSouth.add(btnF); add(pnlSouth, BorderLayout.SOUTH); imgA = this.getImage(this.getDocumentBase(), "A.jpg"); imgB = this.getImage(this.getDocumentBase(), "B.jpg"); imgC = this.getImage(this.getDocumentBase(), "C.jpg"); imgD = this.getImage(this.getDocumentBase(), "D.jpg"); imgE = this.getImage(this.getDocumentBase(), "E.jpg"); imgF = this.getImage(this.getDocumentBase(), "F.jpg"); //set up mediatracker MediaTracker tracker = new MediaTracker(this); tracker.addImage(imgA, 0); tracker.addImage(imgB, 1); tracker.addImage(imgC, 2); tracker.addImage(imgD, 3); tracker.addImage(imgE, 4); tracker.addImage(imgF, 5); try{ tracker.waitForAll(); } catch (InterruptedException exc){ System.out.println(exc.getMessage()); } // end try btnA.addActionListener(this); btnB.addActionListener(this); btnC.addActionListener(this); btnD.addActionListener(this); btnE.addActionListener(this); btnF.addActionListener(this); } // end init public void actionPerformed(ActionEvent e){ Graphics g = drawSurface.getGraphics(); Dimension size = drawSurface.getSize(); int ht = size.height; int wd = size.width; Image currentImage; if (e.getSource() == btnA){ currentImage = imgA; } else if (e.getSource() == btnB){ currentImage = imgB; } else if (e.getSource() == btnC){ currentImage = imgC; } else if (e.getSource() == btnD){ currentImage = imgD; } else if (e.getSource() == btnE){ currentImage = imgE; } else if (e.getSource() == btnF){ currentImage = imgF; } else { currentImage = imgA; } // end if g.drawImage(currentImage, 0, 0, wd, ht, this); } // end actionPerformed } // end class def