Comp 170 Week 14





enum example:

Just so you can read the examples in the book; we won't use them in class.
See book, p 233, and zuul-with-enums-v1



    Some Events and Their Associated Event Listeners

Act that causes the event
Listener type
User clicks a button, presses Enter while typing in a text field, or chooses a menu item ActionListener
User closes a frame (main window) WindowListener
User presses a mouse button while the cursor is over a component MouseListener
User moves the mouse over a component MouseMotionListener
Component becomes visible ComponentListener
Component gets the keyboard focus FocusListener
Table or list selection changes ListSelectionListener
Any property in a component changes such as the text on a label PropertyChangeListener    
Adjust a "slider" control (or similar)
ChangeListener




Demo of my version of ImageViewer
Note that all the image operations are to OFImage objects, and are one-to-one pixel changes. Each output pixel depends on one and only one input pixel.

B&K comment on anonymous inner classes (p 350, 4th ed)

This code fragment looks quite myserious when you encounter it for the first time, and you will probably have trouble interpreting it, even if you understood everything we have discussed in this book so far. This construct is probably syntactically the most confusing example that you will ever see in the Java language. But don't worry -- we shall investigate this slowly.

Remember that it looks like a constructor followed by a {...} body. Try to think of it as a constructor followed by a definition, which is kind of close to the actual intent:

    new ActionListener() {
       public void actionPerformed(ActionEvent e) { openFile(); }
   }

The other clue is that ActionListener is an interface, not a class. You can not construct interface instances, only class instances.

version 1.0
    adds menu of darker/lighter/threshold. These are implemented in OFImage, by darker(), ligher(), threshold(). Look at the code here; they are our first example of actually getting at the pixels! Note also that they are entirely in-place operations.

Review this; then look at filters and where code for buttons would go

version 2.0
    adds abstract Filter class. Filters have a name (so that from a list of filters we can extract a real menu). However, the primary (and abstract) method is apply(OFImage image). Now look at the DarkerFilter, LighterFilter, and ThresholdFilter filter classes.
   

version 3.0
    uses layout manager and buttons
Adds buttons "smaller", "larger"
        layoutManager NORTH/CENTER/SOUTH

Demo: chapter 11, layouts project
    BoxLayout
    GridLayout
    FlowLayout
    BorderLayout
    NoLayout

Moral: things are not what they seem. You have to experiment, and you have to expect to provide a significant amount of "placement" information.

GridLayout  (java.awt.GridLayout)
public GridLayout(int rows,
int cols)
Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size. One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column.


FlowLayout is similar.

BoxLayout is the only one in javax.swing; that is, it is newer.


Notice that:
Note this is three levels of nesting. What if we got rid of flow?

For that matter, what if we got rid of toolbar, and just added the buttons to contentPane.add(, WEST)?
   

What do YOU have to do to get buttons to work?   
Interface version
What has to change? How do we resolve the problem of no data fields?

Lab 10: add:
    rotate button (can't be a filter because it changes the dimensions!!)
    undo button
    redden filter
   

Wednesday:

enums: above

try/catch

java outside bluej
    my portscan.java file contains the following:
        try {
            s.connect(sockaddr, connecttimeout);
        }
        catch (SocketTimeoutException ste) {
            if (DEBUG) System.err.println("connect timeout on port " + port);
            timeoutflag = true;
        }
        catch (IOException ioe) {
            if (DEBUG) System.err.println("connection refused on port " + port);
            refuseconnectflag = true;
        }

Later, there is:

        try {
            len = istr.read(buf, 0, readchunk);
        }
        catch (SocketTimeoutException ste) {
            if (DEBUG) System.err.println("read timeout on port " + port);
            len = -1;
        }
        catch (IOException ioe) {
            System.err.println("bad read");
            len=-1;    // probably a socket ABORT; treat as a close
        }

Note that usually one only catches one kind of exception; the dual-catch is only when we want to handle the two exceptions differently.



in file main.java:
public class main{
    public static void main(String[] args) {
        Game g = new Game();
        g.play();
    }
}

When you try to run a java program with the java command, java looks for a method named main, with the type signature above. If it finds it in the class you indicate (which does not have to also be called "main"), then it runs it. Otherwise it's an error.

Anyway, I do the following:
    javac main.java
    java main
and the game runs in my terminal window.

soundPlayer: if time (it doesn't work on my machine right now)

greenfoot:
This is sort of the ultimate extension of the fox-rabbit simulation. Note how we just define how each actor acts.

review
    for-each loops
    while loops
    conditional statements