1.3 OBJECT ORIENTED PROGRAMMING
• Inheretance allows us to write one generic class (function) that depends on other functions to deal with special cases (this can be done in other languages with function calls, but this is more consistent)
• an applet (note a special type of Java program) needs to have four functions,
init() - called when the applet is first loaded
start() - called when an applet becomes visible in the browser
stop() - called when the applet is no longer visible
destroy() - called when leaving a browser page
* init() is always called first, and then start() and stop() will be called while the document using the applet is alive. If the document is exited, then stop() will be called if not already, and destroy() will be called last before exiting.
• We can also call out to the parent HTML document. Generally, these calls deal with URL’s and parameters.
URL getDocumentBase() - returns the URL for the HTML document
URL getCodeBase() - returns the URL of the applet
String getParameter(String name) - for the parameter with the ‘name’, the string value is returned
URL A = new URL(“http://www.computer.edu”); - define a URL
URL B = new URL(A, “directory/file.ext”); - define a new URL by extending the existing URL A
• Various types of media can also be included using simple functions.
Image getImage(URL) - will set up a link to an image at a remote site to be used when the image is drawn.
Image getImage(URL, filename) - an overloaded version that allows the filename string to be separated from the URL.
AudioClip getAudioClip(URL) - will retrieve (but not play yet) an audio file. Functions available include,
play() - play the clip
loop() - play the clip continuously
stop() - stop playing the current clip.
AudioClip getAudioClip(URL, file_name)
void play(URL) - plays an audio file directly
void play(URL, file_name)
• Applets can interact with the browser that has called it,
AppletContext getAppletContext() - gets a structure that is required by other functions.
Applet getApplet(String filename) - retrieves an applet using an HTML parameter.
Enumeration getApplets() - returns applets on the current page.
void show Document(URL) - allows a new document to be loadedin the browser.
void showStatus(String) - the string is printed to the screen.
String get AppletInfo() - Can be used to return information about the applet.
String getParameterInfo() - used to inform the browser what parameters are allowed.
• GUIs can be constructed using the ‘.awt’ toolkit. This allows the user to work with a variety of interface tools. The hierarchy is shown below.
Component - the basic class to represent the position & size of objects
Container - a class that can hold other classes
Panel - container in a container for organizing objects
Window - a rectangular area on the GUI
Frame - a window with a border added
Dialog - will receive input from the user
FileDialog - a file selection box
Button - when clicked on with a mouse will cause an action
Canvas - an area generally for free form I/O like lines and mouse events
Checkbox - will allow toggled or checked inputs
Label - a printed string
List - a scrolling list of strings
Scrollbar - can be attached to canvases to scroll
TextArea - a text editing window
TextField - a single text inport line
• Components, and all objects in the hierarchy, can use the following functions.
Dimension Size() - returns width and height
Rectangle bounds() - return x, y, width, and height values
void enable() - sets a component to accept user input
void disable() - turns off user input to a component
void show() - makes a component visible
void paint(Graphics g) - a function called to redraw a component
void repaint() - requests that a component be redrawn
void update(Graphics g) - a level above a call to paint, might allow other function to be added
boolean mouseEnter(Event e, int x, int y) - called when the mouse has entered a component
boolean mouseExit(Event e, intx, int y) - called when the mouse leaves a component
boolean mouseMove(Event e, int x, int y) - reports a move inside a component with no buttons pressed
boolean mouseDrag(Event e, int x, int y) - called when a mouse button is held down, and the pointer dragged inside a component
boolean keyDown(Event e, int key) - reports a keyboard event inside the component
boolean handleEvent(Event e) - The normal event handler for component events.
Dimension preferredSize() - returns the idea component size
Dimension minimumSize() - returns the smallest size that a component can be.
• The container class also has a variety of associated functions (as well as its children)
void add(Component) - add a component
void add(String name, Component) - a component is added, with a name as well.
void setLayout(Layout Manager) - this automatically positions components