CSC-116   Computer Programming I

 

Chapter 6     Events

 

 

Introduction

 

            Suppose you came to class and knew everything that was going to happen, that the professor would ask you a question, that there would be a quiz, etc. Or suppose you were playing a football game and you knew everything that the players on the opposing team were going to do. In these situations, the activity would become boring very quickly. Human beings are good at reacting to changes in their daily activities. In fact, most of what we do is a result of some kind of change happening in our lives.

 

            When computer programs we first written, the usual way in which they worked was this:

1.      Read data from some device

2.      Process that data

3.      Print out the results

Once the computer program started running, there was no way to make it change what it was doing. There was no way to react with the program from outside the computer.

 

            Today things are different. Consider playing a computer game. The game program must react to whatever keys or buttons you press to make the game character do different things. These types of programs are called event-driven programs. They wait for the user to do something before they perform any actions.

 

            Even the Windows operating system is an event-driven program. It basically does nothing until you click on an icon or press a button in some window. Many times event-driven programs provide the user with this sort of environment (buttons, icons, etc.) This is called a Graphical User Interface or GUI (pronounced “gooey”).

 

---

 

What is an event?

 

            An event is nothing more than an action taken by a user.

 

            What are some computer related events you may have seen or used?

1.      pressing a key on the keyboard

2.      clicking the mouse ( either right or left  - two different events )

3.      using a scrollbar

4.      clicking a button

5.      selecting a menu option

6.      even just moving the mouse from one place to another

 

The old form of programming, where the program starts running and keeps on running until it prints its results, is still around and being used today, but in the more modern applications, programmers are turning to a more event-driven approach. In this approach the program is viewed as a number of sub-programs, each one designed to handle a particular event. In fact this whole approach is sometimes termed event-handling. You should note that our practice with building methods in the last chapter was very important. We cannot talk about event-handling and sub-programs until we understand the idea of methods because methods provide us a way of making sub-programs that handle events.

 

---

 

The event loop

 

            So how does a program know when an event has taken place? When event-driven programs were first developed, the user had to actually place a loop inside of a program. The “loop”, whatever that is, would suspend the program for a fraction of a second, then restart the program and look to see if an event had happened. If an event had not occurred, the program was suspended for another fraction of a second and would then check again, etc. If an event did occur, then the appropriate actions were taken and then the program suspended again, waiting for a new event.

 

            Fortunately with Java, a lot of the looping and suspending is done automatically for us. We do not have to worry about it. All we have to be concerned with is what do to when an event occurs.  Here is a way of looking at this process: similar to the diagram in your textbook.

 

 

 

 

 


   The Event Loop                                            

   Handled by JVM

 

 

 

 

 

 


                                                                                                                  Flow

              Flow

 

 

 

 

 

 

 

 

 

 

 

 

            Before we can use Java to detect our events and before we code our methods to handle those events, we must understand that in Java, different kinds of events are treated differently. For example, if you click your mouse on a button somewhere in a Java Applet, that is different to Java than when you try to adjust the value on a scrollbar. There will be certain methods that we MUST include in our Applets to handle these:

 

                        Event                            Required Java Method              Class needed

                        Clicking button             actionPerformed                       ActionListener

                        Changing scrollbar        adjustmentValueChanged          AdjustmentListener

 

We will see the “adjustmentValueChanged” method soon. But there is another piece to the puzzle. We said that Java does the work for us be checking or “listening” for events, but it does not do it automatically. We must tell Java to listen for events. This is easy to do however; it only requires us to mention another class in our main program statement. These are the classes listed above. Let’s do a program to see how this works.

 

---

 

A first event-driven program

 

            This program is very similar to the one in your textbook because we have to start with simple concepts and I can’t get much simpler than your authors do. This program places a scrollbar on the screen. Once it is on the screen, you can click on either end of the scrollbar to change it, or place your mouse on the middle adjustment “lever” and drag the mouse to the left or the right.

 

            The only changes I will make is that I will make the scrollbar appear vertically and I will change the output somewhat and add some comments.

 

            import java.awt.*;

            import java.applet.Applet;

            import java.awt.event.*;

 

            // notice that we have added a new set of library routines (to handle events)

 

            public class AnEventProgram extends Applet implements AdjustmentListener {

                        // notice that we have now told Java to listen for adjustment events

 

                        private Scrollbar slider;

                        private int sliderValue = 0;

                                    // here we have created a scrollbar and have set an initial value to zero

 

                        public void init ( ) {

                                    // this is a new method. It is similar to the paint method.

                       

                                    slider = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 100);

                                    add(slider);

                                    slider.addAdjustmentListener(this);

                                                // this places the scrollbar on the screen

                        }

 

                        public void paint (Graphics g) {

                                    // this is a method you should already understand

 

                                    g.drawString(“The adjustment height is “ + sliderValue, 100, 100);

                        }

                                   

                        public void adjustmentValueChanged( AdjustmentEvent e) {

                                    // we mentioned the fact that we would need this method

 

                                    sliderValue = slider.getValue( );

                                    repaint ( );

                        }

            // end of program

            }

 

            WOW! There is a LOT of new stuff in this program. Let’s take a look at the actual working Applet and then we will dissect and discuss this program step-by step.

 

            Click here to see the Applet.

 

            OK, now let’s discuss the program, line-by-line!

 

Line1    import java.awt.*;

Line2    import java.applet.Applet;

Line3    import java.awt.event.*;

 

            // notice that we have added a new set of library routines (to handle events)

 

Line4    public class AnEventProgram extends Applet implements AdjustmentListener {

                        // notice that we have now told Java to listen for adjustment events

 

Line5                private Scrollbar slider;

Line6                private int sliderValue = 0;

                                    // here we have created a scrollbar and have set an initial value to zero

 

Line7                public void init ( ) {

                                    // this is a new method. It is similar to the paint method.

                       

Line8                            slider = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 100);

Line9                            add(slider);

Line10                          slider.addAdjustmentListener(this);

                                                // this places the scrollbar on the screen

                        }

 

Line11              public void paint (Graphics g) {

                                    // this is a method you should already understand

 

Line12                          g.drawString(“The adjustment height is “ + sliderValue, 100, 100);

                        }

                                   

Line13              public void adjustmentValueChanged( AdjustmentEvent e) {

                                    // we mentioned the fact that we would need this method

 

Line14                          sliderValue = slider.getValue( );

Line15                          repaint ( );

                        }

            // end of program

            }

 

 

Line 1   - You have seen this before. It only brings in routines to allow us to use the Abstract Windows Toolkit in Java.

 

Line 2   - You have seen this before as well. It tells Java that we will need the routines that allow us to make an Applet.

 

Line 3   - This is new. But it is not difficult to understand. It basically tells Java to bring in all of the routines necessary to define and handle events.

 

Line 4   - The first part of the program statement is the same as you have seen before. The only difference is that we have added the words “implements AdjustmentListener”. What this does is that it tells Java that we are going to be using an adjustment type of event and that we will define what has to be done for that kind of event. In other words we will implement the necessary methods to handle that type of event.

 

Line 5   - This is nothing more than a variable declaration statement. However, we are not defining a variable of type int or float this time. We are defining a variable called “slider” which is of type “Scrollbar”. In other words we are defining a data area in our program that will eventually contain all the information necessary to put a scrollbar on the screen and use it.

 

Line 6   - This is simply a declaration of an integer to hold the value of the scrollbar. Try to understand that the scrollbar – the physical GUI element that appears in the Applet, is NOT the same as the value that it may represent at any given time. It’s like having a bottle of water – the bottle is one thing, the amount of water inside the bottle is something else. The bottle does not change but the amount of water inside the bottle can change.

 

Line 7   - This is a new method, one you have not seen before. It is similar to the “paint” method because it is one of those methods that Java knows about, but where you have to give all details inside. The “init” method is used in event-driven programs to set up the screen, to place all of the GUI elements (in this case only the scrollbar) on the screen and to initialize (hence the term “init”) certain values.

 

Line 8   -  slider = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 100); Now here is something VERY different. Remember that “slider” was our data location to hold information about a scrollbar (see Line 5). What this statement does is that it tells Java to create a new scrollbar for us and place the information into our data location called “slider”. If we look at the parameters, we can note a few things. Obviously the new scrollbar will be vertical, going up and down on the screen. The other numbers are for initial values, maximum and minimum values, etc. We will discuss those later.

 

Line 9   - This line of code calls a method called “add”. The “add” method is a method that Java knows about and all it does is place a GUI element into the Applet. In other words, now that we have our “slider” scrollbar defined inside our program, we are telling Java to “add” that element to the Applet area.

 

Line 10 - Remember that we said that we must tell Java to actually listen for events. This line of code tells Java to listen for adjustment types of events that may occur on our “slider” which is a scrollbar. The word “this” as a parameter may be a little confusing. In my opinion, in simple programs like the one we are studying, the word “this” should not be needed. But unfortunately Java needs it. What is says is this: Add the ability to listen for an event to “this” object, i.e. the Scrollbar.

 

Line 11 - This is simply our normal paint method that will print something on the screen.

 

Line 12 - Obviously what we are going to print is the current value of that is represented by the scrollbar.

 

Line 13 – OK, we told Java that we would have “adjustment events”, we told Java to listen for those events but we have not as yet told Java what to do when one of those events occurs. This method is where we do that. Again, this method is one that Java knows bout but one where we must fill in the details, just like “init” and “paint”. Please note the name of the method “adjustmentValueChanged”. When you code programs that need this type of event-handling, you MUST have this method in your program and it MUST be spelled correctly – including the small and capital letters. Also notice that this event has a parameter called “Adjustment Event e”. This is a formal parameter. When Java actually sees and event of this type occur, it records the information about the event and then AUTOMATICALLY calls this particular method with that information. It then expects this method to “handle” the event.

 

Line 14 – Here we must remember a few things from the past. An object is a programming entity that has both data and actions associated with it. For example a dog may have a weight but it may also have an action called “bark” and in order to get the dog to bark (in a programming environment) we would write something like “Sparky.bark( );”. This is the same thing. We have a scrollbar object called “slider”; it has data and actions associated with it. We do not have direct access to the data associated with “slider”, but we do have access to methods that will get that data for us. Therefore all this line of code is doing is to say “Please go to the slider object and get the current value and then place that value in our data location called “sliderValue””. Hmmm, is that as clear as mud?

 

Line 15 -  The last real statement in the program. All this statement does is to call the “paint” method again. When the program started running, it called the “init” method to put stuff on the screen, and it called the “paint” method to write some data on the screen, then the program “slept” while Java listened for events. When an event occurred, Java called the last method in our program to handle it. That method retrieved the new value and then called the paint method again to print the new value. Then our program will “sleep” again, until another event occurs.

 

Whew! That was a lot of explanation. Now is a time for questions. Let’s make sure we understand all of this before going on.

 

---

 

The event framework

 

            This section of your textbook simply gives a review of what we just discussed in the area of what the three methods in our program do.

 

public void init ( )

This method is used to place GUI elements

on the screen. It is invoked only once when

the program starts to run.

public void paint(Graphics g)

This method is usually used to print data

that in some way relates to the GUI

elements. It is invoked once when the program

starts to run and also when a “repaint( )”

command is issued.

public void adjustmentValueChanged

    (AdjustmentEvent e)

This is the event-handling method for

adjustment events such as the scrollbar. Java

invokes this method when it detects some

action on the scrollbar. It is important to note

that other types of events require other types of

methods. For example, an applet that has a

button, not a scrollbar, would require a method

called “actionPerformed”.

 

 

---

 

Scopes – an introduction

 

            The scope of a variable is that portion of a program in which the variable can be used. Let’s consider a simple example:

 

            private void One ( ) {

                        int x;

                        x = 10;

            }

            private void Two ( ) {

                        x = 20;

            }

 

In the above example we have a definite problem! The variable “x” is defined inside of method “One”, but method “Two” tries to reference it. This is NO-NO! The way that we have defined “indicates that “x” can only be referenced inside method “One”. In other words “x” is a local variable for method “One”. In our examples from precious chapters, we needed only to use local variables. But now we have programs that have at least 3 methods in them ( and we will be writing programs that have more than 3 very soon ). So a problem arises. How can we define data that can be referenced in several methods? The answer is to place the data “outside” of the method but still inside the program. This is called creating instance variable and some programmers may call them global variables. Here is an example:

 

            public class MyProgram extends Applet {

                        private int x;

 

                        private void One ( ) {

                                    x = 10;

                        }

                        private void Two ( ) {

                                    x = 20;

                        }

            }

 

Now the variable “x” can be referenced by either of the two methods. This is why we placed the variables “slider” and “sliderValue” outside the methods in our program example but still inside the program. Please note, however, that it may be bad programming practice to place all variables in that position. If a variable is needed only within a certain method then it should be defined there!

 

---

 

The Abstract Windows Toolkit

 

            You already know that we have been using the Abstract Windows Toolkit to do things like drawing pictures and, now, placing GUI elements into our applets. When you look at the scrollbar we used in our program you may think that it looks kind of “scrawny”. One reason for this is that Java has to be a cross-platform programming language. In other words, it must work on PCs and Macs and other kinds of computers. Because of this, although any given computer may have nicer GUI elements, Java must use a “subset” of those elements that will work anywhere. Therefore the number and look of those elements is limited.

 

            But one nice thing about Java is that all of its GUI elements work basically the same in the sense of how you create them and use them.

 

            Suppose that Java had a GUI element called BullsEye. It does not have one, but let’s say for a moment that it does. The BullsEye element displays a number of concentric circles on the screen. Creating and using this element would be very similar to what we did for the Scollbar.

 

To create an element of this sort, we may code something like this:

 

            private BullsEye target;

            private int xHit, yHit;

 

To add it to our screen, inside the “init” method, we may code:

 

            target = new BullsEye ( 50, 50 , 6 );

            add( target );

            target.addShootListener(this);

 

where the 3 parameters would serve to indicate the position of the BullsEye and the number of circles.

 

We may also write a “shootDetected” method wherein we pick up the values of where the shot hit the target.

 

            public void shootDetected(ShootEvent e) {

                        xHit = target.getX ( ) ;

                        yHit = target.getY ( ) ;

            }

 

As we said before, a BullsEye element does not exist in Java. This little section is meant to show you that if one did, the use of it would be very similar to our Scrollbar element. So it is important to get to know what elements actually do exist, like Buttons, TextFields, etc. Once you know what you can put on the screen and once you know the general ways in which they are created and handled, then your programming should be a lot simpler.

 

---

 

The scrollbar component

 

            There is not much new to say in this section. But it is important to note that, in the first programs we wrote, we had to place fixed-value data into the program. Now we have at least one way of entering data WHILE the program is running and thus be able to see how it will react to that data!

 

---

 

 

An event driven example

 

            Your textbook uses an example that employs a scrollbar to draw a graphical object, in this case a window blind. As you change the scrollbar, the blind will go up or down. Let’s look at this program for a little while and then we will try a different one.

 

            import java.awt.*;

            import java.applet.Applet;

            import java.awt.event.*;

            public class WindowBlind extends Applet implements AdjustmentListener {

                        private Scrollbar slider;

                        private int sliderValue;

 

                        public void init ( ) {

                                    slider = new Scrollbar(Scrollbar.VERTICAL, 0,1, 0, 100);

                                    add(slider);

                                    slider.addAdjustmentListener(this);

                        }

                        public void paint(Graphics g) {

                                    showStatus(“Scrollbar value is “ + sliderValue);

                                    g.drawRect(40, 80, 60, 100);

                                    g.fillRect(40, 80, 60, sliderValue);

                        }

                        public void adjustmentValueChanged(AdjustmentEvent e) {

                                    sliderValue = slider.getValue( );

                                    repaint ( );

                        }

            }

 

            Take a look at the Applet.

 

            There is not much new in this Applet except 1) how the data from the scrollbar is used to draw something rather than just printing out the value, and 2) the use of the showStatus method. The showStatus method is very much like the drawString method except that the data is not printed in the Applet – it is printed on the bottom border of the browser window.

 

            Let’s finally give a thorough description of the data parameters used to create a scrollbar:

 

1.      orientation – either Scrollbar.HORIZONTAL or Scrollbar.VERTICAL

2.      initial value – where do you want to scrollbar to start? If your range is 0 to 100, you may want it to start at zero, or you may want it to start at 50.

3.      large change – when you click on the arrows at the ends of the scrollbar, the value will be increased or decreased by 1. However, when you click in the area in between the arrows and the slider, you have a choice as to how far you want the slider to move. For example you could ask for it to be moved by 10.

4.      the minimum value – you can start your scrollbar at 0 and go to 100, but there is no reason why it cannot start at 100 and go to 200

5.      the maximum value – similarly you may want the maximum value to be larger or smaller than 100.

 

What does the following scrollbar definition do?

 

            slider = new Scrollbar(Scrollbar.HORIZONTAL, 100, 10, 0, 200);

 

            OK, now let’s try our own example. We will do problem 6.4 at the end of your chapter. Here is the statement of the problem:

 

            6.4 Produce an applet that allows the user to manipulate a filled oval on the screen via two scrollbars – one for the width, and one for the height. Add labels close to the scrollbars to indicate horizontal and vertical change.

 

            Hmmm, we have a couple of problems here. First we need two scrollbars, not just one. Second we have to add labels and we don’t know how to do that yet. Well, first things first. Let’s do the program without the labels and then add them later.

 

            What we will do is take our last example, set up two variables called “slider1” and “slider2” and then change the rest of the methods accordingly.

 

  

            import java.awt.*;

            import java.applet.Applet;

            import java.awt.event.*;

 

            public class ChangeOval extends Applet implements AdjustmentListener {

                        private Scrollbar slider1, slider2;

                        private int sliderValue1, sliderValue2;

 

                        public void init ( ) {

                                    slider1 = new Scrollbar(Scrollbar.VERTICAL, 0,1, 0, 100);

                                    add(slider1);

                                    slider1.addAdjustmentListener(this);

                                    slider2 = new Scrollbar(Scrollbar.HORIZONTAL, 0,1, 0, 100);

                                    add(slider2);

                                    slider2.addAdjustmentListener(this);

                        }

                        public void paint(Graphics g) {

                                    showStatus(“Scrollbar values are “ + sliderValue1 + “, “ + sliderValue2);

                                    setBackground(Color.white);

                                    g.setColor(Color.red);

                                    g.fillOval(60, 60, sliderValue1, sliderValue2);                                

                        }

                        public void adjustmentValueChanged(AdjustmentEvent e) {

                                    sliderValue1 = slider1.getValue( );

                                    sliderValue2 = slider2.getValue( );

                                    repaint ( );

                        }

            }

 

            Take a look at the Applet and play with it. OOPS! There are some problems! It is working, but when we change the vertical scrollbar, it seems the width is changing. But we should be able to correct this simply reversing the orientation of the scrollbars.

 

---

 

Adding labels

 

            Continuing with our previous problem, 6.4 in the textbook, we need to reverse the orientation of our scrollbars, but the problem also says to add labels. Labels in Java are not active elements. In other words you cannot click on them or adjust them – they are simply words that are used to describe the actual GUI elements. A label is pretty easy to create. For example in our problem, our first scrollbar is for width. To add a label beside it, we simply need to add a couple of lines of code just before we define the scrollbar, namely:

 

                        label1 = new Label(“WIDTH”);

                        add(label1);

 

We do the same with another label for height. And call it “label2”. But label1 and label2 are variables and we need to declare them. So we need a line saying:

 

                        Label label1, label2;

 

Also it should be noted that since no other method in our program needs to reference these labels, there is no sense in making them global variables. They should be defined within the “init” method only.

 

Here is the final version of our program to solve problem 6.4.

 

            import java.awt.*;

            import java.applet.Applet;

            import java.awt.event.*;

 

            public class ChangeOval2 extends Applet implements AdjustmentListener {

                        private Scrollbar slider1, slider2;

                        private int sliderValue1, sliderValue2;

 

                        public void init ( ) {

                                    Label label1, label2;

 

                                    label1 = new Label(“WIDTH: “);

                                    add(label1);

 

                                    slider1 = new Scrollbar(Scrollbar.HORIZONTAL, 0,1, 0, 100);

                                    add(slider1);

                                    slider1.addAdjustmentListener(this);

                                   

                                    label2 = new Label(“HEIGHT: “);

                                    add(label2);

 

                                    slider2 = new Scrollbar(Scrollbar.VERTICAL, 0,1, 0, 100);

                                    add(slider2);

                                    slider2.addAdjustmentListener(this);

                        }

                        public void paint(Graphics g) {

                                    showStatus(“Scrollbar values are “ + sliderValue1 + “, “ + sliderValue2);

                                    setBackground(Color.white);

                                    g.setColor(Color.red);

                                    g.fillOval(60, 60, sliderValue1, sliderValue2);                                

                        }

                        public void adjustmentValueChanged(AdjustmentEvent e) {

                                    sliderValue1 = slider1.getValue( );

                                    sliderValue2 = slider2.getValue( );

                                    repaint ( );

                        }

            }

 

            Take a look at the final Applet in action. Now that wasn’t too bad, was it?

 

            One final word about labels and scrollbars and GUIs in general. Notice that we were careful to “add” the first label and then the first scrollbar, then the second label and then the second scrollbar. In general you should add these GUI items in the order in which you want them to appear on the screen. However, even then Java may put them in a position you do not like. For example, if your applet area is sized too small to allow all four elements to appear in a row, Java may put one of them below the others. Usually by playing around with the size of the applet area though, you will get some good results. If you are here for CSC-117 Computer Programming II, we will discuss other ways of controlling what the screen will actually look like.

 

            Any questions at this point?

 

---

 

Scaling the scrollbar

 

            Suppose you were driving in Canada where distances are measured in kilometers, not in miles. However you know that sometimes when you ask directions, people will tell you that a certain place is ˝ a kilometer away, or its 3.7 kilometers away. Hmmm. You know that 1 mile = 1.6 kilometers, approximately, but you don’t want to do all of the math yourself, so you bring a computer along with an applet that will do the conversion for you.

 

            Now, you know how to do scrollbars that increment by 1 unit each time you click it, but you decide you need to input data in tenths of a unit, so that you can convert things like 3.7 kilometers. How do you do this? Well, you can use a practice called scaling the scrollbar which is really nothing more than faking it out with some additional calculations inside the program.

 

            Suppose you had a scrollbar that allowed you to input numbers from 0 to 500. By taking that number and dividing it by 10, you would get numbers in the range 0.0 to 50.0. That is what we will do.

 

            Here is a very simple program that does just that!

 

            import java.awt.*;

            import java.applet.Applet;

            import java.awt.event.*;

 

            public class KilosToMiles extends Applet implements AdjustmentListener {

 

                        private Scrollbar bar;

                        private float barValue;

 

                        public void init ( ) {

                                    Label name;

                                    name = new Label(“Kilometers: “);

                                    add(name);

 

                                    bar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 500);

                                    add(bar);

                                    bar.addAdjustmentListener(this);

                        }

                        public void paint (Graphics g) {

                                    float miles;

                                    miles = barValue / 1.6f;

                                    g.drawString(“ Kilometers = “ + barValue + “   Miles = “ + miles, 20, 80);

                        }

                        public void adjustmentValueChanged(AdjustmentEvent e) {

                                    barValue = ( (float)  bar.getValue( ) )/ 10.0f ;

                                    repaint( );

                        }

            }

                       

            Here is the Applet. One thing to note is the expression that picks up the barValue in the last method.

 

                                                barValue = ( (float)  bar.getValue( ) )/ 10.0f ;

 

            Why did we put the (float) cast in this expression?

            What would happen if we simply put a 10 in the divisor instead of a 10.0f?

            Compare this with the following:

                                                barValue = (float) ( bar.getValue( ) / 10 )?

            Explain, in your own words, what these two lines of code really do.

 

---

 

Building up graphical input

 

            One final word before leaving the chapter. There is a small problem with the “paint” method that we have been using in that it always erases what had been painted previously and redraws the screen. Suppose that you wanted to keep a “history” of the values that events may generate. This would be difficult to do if you last result was always erased when the new result was displayed. Fortunately there is a way out of this dilemma. But to use it, we have to get rid of the “paint” method. Instead we use another method called “getGraphics” so that we can obtain that “g” that we use with our drawstring calls. Once we have the “g”, we can put those calls into another method, say the “adjustmentValueChanged” method. Here is the example that is in your textbook.

 

            import java.awt.*;

            import java.applet.Applet;

            import java.awt.event.*;

 

            public class History extends Applet implements AdjustmentListener {

 

                        private Scrollbar slider;

                        private int currentX = 1;

                        private int currentY = 5;

 

                        public void init ( ) {

                                    slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 100);

                                    add(slider);

                                    slider.addAdjustmentListener(this);

                        }

                        public void adjustmentValueChanged(AdjustmentEvent e) {

                                    Graphics g = getGraphics( );

                                    currentX = slider.getValue( );

                                    g.drawLine(0, currentY, currentX, currentY);

                                    currentY = currentY + 5;

                        }

            }          

 

            This is a neat feature, but please remember that it is to be used whenever you do not want the paint method to erase what you had previously. Therefore, the use of this technique very much depends upon the problem you are trying to solve.

 

---

 

Your next assignment

 

            You can get your next assignment by clicking on the link on the previous page. Again you will be asked a few questions and will have to do a program. The program will either be the swimming pool program (problem 6.2, page 79) or the SuperWoman program (problem 6.6, page 80). See the actual assignment for details.