My java snippet for drawing an image turned out to be quite timely for my graduate alma mater. Onward to the Final Four!
Write a Java class ButtonDemoFrame to create this window:
Whenever the user presses a button, the program writes the name of the button to the standard output.
We encounter several design questions in writing this program::
Here is a quick solution: DemoButtonListener and ButtonDemoFrame.
Can we make the duplication go away? Creating and setting up the five buttons repeats a lot of code. What differs among the five button creation snippets?
Sure -- extract a method to make the buttons: ButtonDemoFrame.
Bonus:
- Can we make even more of the duplication go away? We still have to make five calls to makeButton() when creating the five buttons. Could we use a loop to make these calls?
Sure -- use an array to map button location strings to buttons: ButtonDemoFrame.
- Just for fun... In class, someone quietly asked whether the buttons might change locations each time a button is pressed. (Why is randomness so tempting to us?)
Sure -- our button listeners will need some way to tell the frame to change its button layout. Having an array of label strings turns out to be helpful in this regard: ButtonDemoFrame and ButtonDemoFrame.
Today we will consider a bit more about how to build graphical interface components using Java's Abstract Window Toolkit, or AWT. The AWT shows you lots of examples of polymorphism in the Java libraries.
Polymorphism:
Suppose that we would like to define a window that looks like this:
The user is able to type text into each box independently of the other.
Let's write a Java class called TextEntryFrame... With the AWT's support, this turns out to be almost too easy:
import java.awt.Frame; import java.awt.TextField; import java.awt.TextArea; public class TextCheckFrame extends Frame { public TextCheckFrame() { setTitle( "Text Component Examples" ); setSize ( 300, 250 ); add( "North", new TextField() ); add( "South", new TextArea () ); } }
This solution comes with a new exercise built right into it:
Suppose that we want any text typed into the text field to appear in the text area, too.What changes do we have to make?
"What do you mean by that?", you ask?
Now our solution needs a custom listener...
The AWT is a framework, a set of classes that provide objects and control for building graphical applications. It provides built-in control that works in your applications by taking advantage of polymorphism through inheritance and interfaces.
Consider a typical message sequence in one of our ...BallWorld programs:
The show() message example exemplifies the Hollywood Principle:
Don't call us; we'll call you.
We use inheritance and polymorphism to allow programmers to create complex applications with as little work as possible, overriding only those methods that need special behavior.
Methods in a framework that you must override are called hot spots and are the key to learning and using a framework. They should also be the focus of documentation for a framework.
Working with a framework introduces some new problems for us to watch for:
Dealing with the yo-yo problem is mostly a matter of practice. Once you've learned a framework or two, bouncing up and down the class hierarchy will seem natural.
Dealing with the tooth fairy problem is mostly a matter of good documentation and asking questions when you encounter unexpected behavior -- or, more likely non-behavior.