// file: Lesson3.java // type: Java code // note: Doing a Layout and using images // author: Gregory C. Walsh // date: 11/23/97 import java.awt.*; import java.awt.event.*; import java.awt.*; import java.applet.Applet; public class Lesson3 extends Applet implements ActionListener{ Image walsh[]; Button front, top, left, right; WalshView view; // WalshView is an inner class (see details below)... Font f1; public void init () { walsh = new Image[4]; // Download the four images ..... for(int i = 0; i < 4; i++) walsh[i] = getImage(getDocumentBase(), "walsh" + i + ".jpg"); // Generate the GUI objects with initial states front = new Button( "+"); top = new Button( "Mug Shot Project" ); left = new Button("<"); right = new Button(">"); view = new WalshView(0); // Set the font on the GUI f1 = new Font("TimesRoman",Font.PLAIN,16); front.setFont(f1); top.setFont(f1); left.setFont(f1); right.setFont(f1); // Configure the appearance of the GUI setLayout(new BorderLayout() ); add("North",top); add("Center",view); add("West",left); add("East",right); add("South",front); // Connect buttons to action listeners .... front.addActionListener(this); top.addActionListener(this); right.addActionListener(this); left.addActionListener(this); } // Action listener for the buttons .... public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if(command == "+") { view.image = 0; view.repaint(); } if(command == "<") { view.image = 1; view.repaint(); } if(command == ">") { view.image = 2; view.repaint(); } if(command == "Mug Shot Project") { view.image = 3; view.repaint(); } } // WalshView : This inner class displays an image on a canvas.... public class WalshView extends Canvas { int image; public WalshView(int i) { image = i; } public void paint(Graphics g) { g.drawImage(walsh[image], 0, 0, this); } } }