Tutorial 9 Lecturer: Craig MacDonald Tutorial: Mon@10:10am 971110 ========== CSC 108, Fall 1997 Tutorial notes, T9 ========================================================================== Topics ====== - finish topics from last week - go over the event driven GUI program Summary ======= You can finish any material left over from the last tutorial. Invite questions about inheritance, class hierarchies, polymorphism and interfaces and their use. I have finished talking about chapter 10. They have been introduced to threads and asynchronous processes vaguely and only in the context of chapter 10, and event driven GUI programs. Look at my course web page and follow the link "Week ten" for a summary of text chapter 10. I am sure that they will have many questions on this topic. An understanding of Event driven programming is needed to do the project. Here is a simple but general TEMPLATE for an event driven GUI program using only two classes, an applet, "MyApplet" and a listener, "My_Component_Listener". Next is an example of refining this to create an applet with a picture and button. When the button is pressed the applet zooms in and out of the picture. First is PSEUDO CODE then CODE. It works. 1. This is a simple but general TEMPLATE for an event driven GUI program using only two classes, an applet, "MyApplet" and a listener, "My_Component_Listener." The following is pseudo code; it is not an actual Java program implementation. import applet, and awt packages public class MyApplet extends Applet { declare context and define current state (use other classes if you need to) declare components declare listeners public void init() { set design and context of applet add components to the applet container add listeners to components if you need to do anything with them } public void methodToChangeStateOfApplet (informationFromListeners){ change state of applet using informationFromListeners } } class My_Component_Listener implements Component_Listener(s) { declare an applet to call and any current state information My_Component_Listener (MyApplet applet) { this applet refers to the MyApplet object calling this constructor } public void specificListenerMethod (Event_Type) { possibly depending on Event_type change state of applet by calling methodToChangeStateOfApplet with any needed information } } 2. This is an example of an applet that contains a picture and a button. When the button is pressed, the picture zooms in and out. This is the pseudo code. import applet, and awt packages public class ButtonZoomer extends Applet { declare and initialize SIZE, button, listener, B_Listener, picture and size of picture. public void init() { set layout and color. add button to applet container get image from file add listener to button component } public void resetsize (int size) { set new size and repaint. } public void paint (Graphics page) { draw picture on page within "this" applet } } class B_Listener implements ActionListener { declare size array, index of next element and an applet to call B_Listener (ButtonZoomer applet) { this applet refers to the ButtonZoomer object calling this constructor } public void actionPerformed (ActionEvent event) { resetsize of image to next size in size array } } 3. This is an example of an applet that contains a picture and a button. When the button is pressed, the picture zooms in and out. // Pressing the Button results in changing picture size according // to the modulo of the "next" element in the list of sizes in // the listener class. import java.applet.*; import java.awt.*; import java.awt.event.*; public class ButtonZoomer extends Applet { private final int SIZE = 600; // maximum drawing area private Button button = new Button(); // button to press private B_Listener listener = new B_Listener(this); private Image picture; // picture to display private int size = 10; // size of picture displayed // Applet starts execution here. public void init() { // Sets layout, colour, adds button to applet container. setLayout (new BorderLayout()); // long narrow button setBackground (Color.white); // background colour white add(button, "West"); // add button left side of applet // Reads in 'picture' from the specified file. //picture = getImage (getDocumentBase(), "owl.gif"); picture = getImage (getDocumentBase(), "file:/A:/tut9/owl.gif"); // This associates the 'button' with the listener object. // The button waits for the events that are specified in // the listener's B_Listener class and then the // actionPerformed method is run. button.addActionListener (listener); show(true); } // This method is called to draw picture on screen within // 'this' applet. The picture is 'size x size' pixels. // Drawn in middle of screen that is 'SIZE x SIZE' pixels. public void paint (Graphics page) { page.drawImage (picture, SIZE/2-size/2, SIZE/2-size/2, size, size, this); } // Resets the size of the image and calls repaint() // to redraw the screen. public void resetsize (int size) { this.size = size; repaint(); } } // -------------------------------------------------------- // Class to handle events, and to call appropriate method // when event is detected. class B_Listener implements ActionListener { private final int[] size = {32, 64, 128, 256, 512, 256, 128, 64}; private int next = 0; private ButtonZoomer applet; // This applet refers to the ButtonZoomer object // calling this constructor. B_Listener (ButtonZoomer applet) { this.applet = applet; } // Reset size of image to next size in size array. public void actionPerformed (ActionEvent event) { next = (next + 1) % size.length; applet.resetsize( size[next] ); } }