Presentation is loading. Please wait.

Presentation is loading. Please wait.

Duke CPS 108 18. 1 Modules in Java l Classes that are related should be grouped together, may even share access to otherwise private methods/instance variables.

Similar presentations


Presentation on theme: "Duke CPS 108 18. 1 Modules in Java l Classes that are related should be grouped together, may even share access to otherwise private methods/instance variables."— Presentation transcript:

1 Duke CPS 108 18. 1 Modules in Java l Classes that are related should be grouped together, may even share access to otherwise private methods/instance variables ä Java uses package: one directory of related classes ä private, public, protected and the default: package access ä to compile, the CLASSPATH must be set properly setenv CLASSPATH `pwd`:. javac dirName/App.java java dirName.App l Can also create anonymous (package access) classes ä on-the-fly creation of unnamed classes ä useful for AWT event listeners, commands, etc., see Anagram and Pixmap code examples

2 Duke CPS 108 18. 2 Interface and Classes l Java is single inheritance, in GUI world this can be a problem ä must extend Frame, Component, Composite, etc. ä sometimes need additional functionality l a Java interface is similar to a C++ abstract base class ä classes can implement many interfaces ä all methods in each interface MUST be implemented ä see Comparable in Anaword, Iterator/Enumerator l sometimes awkward necessity of implementing all classes, even when not interested in them ä use an Adapter class (anonymous) that provides empty method implementations --- see WindowAdapter example l Anonymous classes can cause recompilation problems ä rm *$*.class

3 Duke CPS 108 18. 3 AWT concepts l Object < Component < Container < Window < Frame ä the component/container pair is an example of the Composite pattern: Container has Components, including other Containers (which are Components) ä containers have layout managers that control how widgets are added/appear ä Panel is the simplest, most useful container defaults to Flowlayout, add widgets (other panels!) ä Canvas is a drawable component ä Frames is a top-level window (which is a container) has a title, defaults to BorderLayout

4 Duke CPS 108 18. 4 General Gui/AWT guidelines l Keep the GUI and Application separate, use a Controller class to mediate between widgets in the GUI ä see AnaGui and PixGui for examples l use Command pattern (even if no explicit Command classes) ä isolate Action events on a class-per-action basis, avoid if- else chain to identify source of action l anonymous classes, or package/private classes associated with the GUI are useful ä see PixGui and AnaGui, note that default access is package l key member functions ä pack(), show(), validate() l understand the repaint(), paint(), update() protocol ä call repaint() directly


Download ppt "Duke CPS 108 18. 1 Modules in Java l Classes that are related should be grouped together, may even share access to otherwise private methods/instance variables."

Similar presentations


Ads by Google