1 / 120

Objects At their Best

Objects At their Best Introduction to Applet Programming and Java’s Abstract Window Toolkit http://www.sci.brooklyn.cuny.edu/~arnow/ Gerald Weiss and David Arnow Department of Computer and Information Science Brooklyn College, City University of New York The Abstract Window Toolkit (AWT)

adamdaniel
Download Presentation

Objects At their Best

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects At their Best Introduction to Applet Programming and Java’s Abstract Window Toolkit http://www.sci.brooklyn.cuny.edu/~arnow/ Gerald Weiss and David Arnow Department of Computer and Information Science Brooklyn College, City University of New York Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  2. The Abstract Window Toolkit (AWT) • Supports GUI • Offers a set of classes • packages java.awt, java.applet, ... • no new Java • Forms a framework for writing • stand-alone graphic applications • applets Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  3. interrupt Event notification button object textfieldobject Event handling object val++; update textfield Native OS Environment Java AWT Environment GUI In Action The native OS displays our GUI controls; but there must be some connection to our Java code The native OS receives an interrupt from mouse click; but there must be a Java object, modeling the button, to receive the interrupt from the OS. The button object should not be responsible for handling the addition; that is the job of a separate, event handling object How does the button object know the identity of the event handling object (how does it know whom to notify of events)? How does the event object know what event occured? Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  4. What GUI Support Entails • Windows, Controls, Graphics (“Doodles”) • Articulation with • application code • external environment • Layout Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  5. GUI Tasks and Issues • Control creation • Control positioning • Responding to events • Maintaining the display • Components vs “Doodles” • Component objects: buttons, text fields: Java objects • Graphic “objects”: ovals, lines— drawn, passive doodles • Initialization vs. Ongoing Execution Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  6. Applets • Code referenced in web page • Downloaded by browser • Executes within a browser’s GUI context • Portion of pre-existing window • Executes as threads within browser process • No main! Must provide hooks for browser Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  7. Life Cycle of An Applet • Initialization • Control creation and placement • Initialize event handling • Method: init() • Ongoing Execution • Event handling • Maintenance of display— “doodles” • Methods: paint() + various event handlers Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  8. Applet Class (Default) Behavior • Control creation: none • Control placement: none • Event handling: none • Display maintenance: dull gray Programmer Task: Extend Applet Class Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  9. A Sample Applet: BasicElements Not your default Applet behavior Must extend Applet class Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  10. Extending The Applet Class (1) import java.awt.*; import java.applet.*; public class BasicElements extends Applet { public void init() { … } public void paint(Graphics g) { … } // event-handling code // instance variables } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  11. Overriding init:Control Creation and Placement public void init() { Button red = new Button("red"); Button green = new Button("green"); Label textlabel = new Label("(oval/rect)"); TextField shapeSpec = new TextField("oval"); add(red); add(green); add(textlabel); add(shapeSpec); ... } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  12. Event Handling • The applet must be notified of events: • example: a button is clicked • Three objects of interest: • The event source – the object causing the event (e.g., a button) • The event - represented by an object • The listener - our applet — must react in some way • Source notifies the listener • sends message with event object as argument • Particular message is specified in an interface Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  13. Button Event BasicElements event source listener Is Anyone Listening? • Our applet must implement the listener interface, particular to the event. • Event source must know who is listening: • registration Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  14. Overriding init:Listener Registration public void init() { red = new Button("red"); green = new Button("green"); Label textlabel = new Label("(oval/rect)"); shapeSpec = new TextField("oval"); add(red); add(green); add(textlabel); add(shapeSpec); red.addActionListener(this); green.addActionListener(this); ... } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  15. Extending The Applet Class (2) import java.awt.*; import java.awt.event.*; import java.applet.*; public class BasicElements extends Applet implements ActionListener { public void init() { … } public void paint(Graphics g) { … } public void actionPerformed(ActionEvent e) { … } // instance variables } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  16. Maintaining the Display • Applet's paint method: paint(Graphics g) • Draws an empty gray box • Invoked as needed • By environment • Programmatically • To get USEFUL paint behavior: • Override Applet’s paint method • Applet is responsible for “Doodles” only! Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  17. Components vs “Doodles” • Components • Objects— independent of applet • Possess behavior • RESPONSIBLE FOR THEIR OWN DISPLAY • Require placement on applet • “Doodles” • Stuff WE draw directly on surface • No behavior! • OUR APPLET RESPONSIBLE FOR THEIR DISPLAY • Text is a “doodle” too Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  18. Graphic Behavior:The Graphics Class • Provides drawing and other graphic behavior • drawRect/fillRect • drawOval/fillOval • setColor • Shields programmer from device dependencies Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  19. Overriding paint:Drawing the doodles public void paint(Graphics g) { setBackground(Color.black); g.setColor(Color.black); g.fillOval(5,5,80,80); g.fillRect(5,5,80,80); g.setColor(currentColor); if (shapeSpec.getText().equals("oval")) g.fillOval(5,5,80,80); else g.fillRect(5,5,80,80); } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  20. Back to events:actionPerformed method • Promised by actionListener interface public void actionPerformed(ActionEvent e) { if (e.getSource()==red) currentColor = new Color(255,0,0); else currentColor = new Color(0,255,0); repaint(); } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  21. Needed: Instance Variables public class BasicElements extends Applet implements ActionListener { public void init() { … } public void paint(Graphics g) { … } public void actionPerformed(ActionEvent e) { … } Button red, green; TextField shapeSpec; Color currentColor; } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  22. Overriding init:Initializing currentColor public void init() { red = new Button("red"); green = new Button("green"); Label textlabel = new Label("(oval/rect)"); shapeSpec = new TextField("oval"); add(red); add(green); add(textlabel); add(shapeSpec); red.addActionListener(this); green.addActionListener(this); currentColor = new Color(255,0,0); } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  23. The BasicElements Applet public void paint(Graphics g) { setBackground(Color.black); g.setColor(Color.black); g.fillOval(5,5,80,80); g.fillRect(5,5,80,80); g.setColor(currentColor); if ( shapeSpec.getText(). equals("oval")) g.fillOval(5,5,80,80); else g.fillRect(5,5,80,80); } public void actionPerformed (ActionEvent e) { if (e.getSource()==red) currentColor = new Color(255,0,0); else currentColor = new Color(0,255,0); repaint(); } Button red, green; TextField shapeSpec; Color currentColor; } import java.awt.*; import java.awt.event.*; import java.applet.*; public class BasicElements extends Applet implements ActionListener { public void init() { red = new Button("red"); green = new Button("green"); Label textlabel = new Label("(oval/rect)"); shapeSpec = new TextField("oval"); add(red); add(green); add(textlabel); add(shapeSpec); red.addActionListener(this); green.addActionListener(this); currentColor = new Color(255,0,0); setBackground(Color.black); } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  24. Running an Applet • Compile • Move class files as needed • Create HTML file with class file reference <APPLET CODE="BasicElements.class" WIDTH=250 HEIGHT=100> </APPLET> • Access HTML file using • browser • Appletviewer Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  25. Using BasicElements Applet:What's The Problem? Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  26. Wanted: Component for Drawing • Our doodles • Should not occlude buttons, text areas etc • Should not be occluded by buttons, text areas etc • Need Component: • Provides drawing surface • Can be placed along with buttons, text areas, etc. • Solves occlusion problem Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  27. Improving BasicElements: Canvas • The add method: • Places components • NEVER on top of each other • Canvas: a component class for doodling! Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  28. General Strategy For Using Canvas • Extend Canvas class: MyCanvas • Move drawing to MyCanvas • Move appropriate event-handling to MyCanvas • Applet's init creates and adds Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  29. MyCanvas: Overview class MyCanvas extends Canvas implements ActionListener { // constructor ??? public void paint(Graphics g) { // just like earlier paint method of applet } public void actionPerformed(ActionEvent e) { // just like earlier actionPerformed method of applet } // instance variables ??? } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  30. MyCanvas: paint public void paint(Graphics g) { setBackground(Color.black); g.setColor(Color.black); g.fillOval(5,5,80,80); g.fillRect(5,5,80,80); g.setColor(currentColor); if (shapeSpec.getText().equals("oval")) g.fillOval(5,5,80,80); else g.fillRect(5,5,80,80); } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  31. MyCanvas: actionPerformed public void actionPerformed(ActionEvent e) { if (e.getSource()==red) currentColor = new Color(255,0,0); else currentColor = new Color(0,255,0); repaint(); } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  32. MyCanvas: Instance Variables class MyCanvas extends Canvas implements ActionListener { // constructor ??? public void paint(Graphics g) { // requires shapeSpec, currentColor } public void actionPerformed(ActionEvent e) { // requires red, green, currentColor } // instance variables Button red, green; TextField shapeSpec; Color currentColor; } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  33. MyCanvas: Constructor public MyCanvas(Button red, Button green, TextField shapeSpec) { super(); this.shapeSpec = shapeSpec; this.red = red; this.green = green; red.addActionListener(this); green.addActionListener(this); currentColor = new Color(255,0,0); setBackground(Color.black); setSize(100,100); } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  34. Improved Applet: BetterBasicElements public class BetterBasicElements extends Applet { public void init() { Button red = new Button("red"); Button green = new Button("green"); Label textlabel = new Label("(oval/rect)"); TextField shapeSpec = new TextField("oval"); MyCanvas mycanvas = new MyCanvas(red,green,shapeSpec); add(red); add(green); add(textlabel); add(shapeSpec); add(mycanvas); setBackground(Color.black); } // NOTHING ELSE! (in this case) } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  35. Placing Controls • Control not visible or usable until PLACED in the applet • Carried out in init, after creation • Accomplished using the add method Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  36. Components and Containers • Components • controls, canvas, applet • NOT doodles • Container • a component on which other components are placed • purpose: support positioning of components • example: an Applet! • Components are not visible or usable until PLACED in a container Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  37. The add Method • Inherited from Container class • Therefore a method of Applet • Places a component onto the container • A container often sends the add message to itself: Button b = new Button("OK"); this.add(b); Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  38. LayoutManager Objects • Used by containers to position their components. • LayoutManager: an interface • specifies methods invoked by containers’ add method • Implementations of LayoutManager differ in • ways of laying out components • degree of programmer control Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  39. North West Center East South Two Layout Managers: • FlowLayout • Components laid out left to right • BorderLayout • Defines 5 regions: • North South East West Center • Component can be placed in any region • 5 Components at most! Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  40. Using Layout Managers: setLayout method • Method of Container class • Inherited by Applet • Inherited by your subclass • Specifies layout manager: prototype:setLayout(LayoutManager) example:setLayout(new FlowLayout()) Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  41. add method • Method of Container class • Inherited by Applet • Inherited by your subclass • Adds component to the receiving container: add(Component, other arguments) • Delegates actual placement to container's layout manager Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  42. Panel Class • Container class • Sole purpose: • group components • apply LayoutManager to its components independently of rest of applet Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  43. Using Panels • Strategy: • divide applet's real estate into different Panels • add components to the Panel objects • send setLayout message to each Panel object • Panels within Panels Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  44. Using Panels: BestBasicElements public class BestBasicElements extends Applet { public void init() { // create controls as before Panel buttonGroup = new Panel(); // add buttons to buttonGroup Panel textGroup = new Panel(); // add textfield and label to textGroup // add buttonGroup, canvas, textGroup to applet // set everyone's background color } } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  45. Using Panels: BestBasicElements public void init() { // create controls as before Panel buttonGroup = new Panel(); buttonGroup.setLayout(new BorderLayout(2,2)); buttonGroup.add("North", red); buttonGroup.add("South", green); Panel textGroup = new Panel(); textGroup.setLayout(new BorderLayout(0,0)); textGroup.add("North", textlabel); textGroup.add("South", shapeSpec); add(buttonGroup); add(textGroup); add(mycanvas); // set everyone's background color } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  46. Using Panels: BestBasicElements public class BestBasicElements extends Applet { public void init() { ... buttonGroup.setBackground(Color.black); textGroup.setBackground(Color.black); red.setBackground(Color.red); green.setBackground(Color.green); textlabel.setBackground(Color.white); shapeSpec.setBackground(Color.white); setBackground(Color.black); } } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  47. Using Panels: BestBasicElements Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  48. Calculator Layout: Same Strategy Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  49. GUI Ain’t Just Buttons • Other components: • Text fields • Windows • Associated with different events • Changing text • Closing a window • Different events, different Event classes Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  50. Event Summary Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

More Related