1 / 127

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).

higa
Télécharger la présentation

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, etc. • Articulation with • application code • external environment • Layout • Passive Graphics Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  5. GUI Tasks and Issues • Controls and Events • creation • positioning • Responding to events • Maintaining the Display • Components vs Passive Graphics (“Doodles”) • Components: buttons, text fields: Java objects • Doodles: ovals, lines, etc. • 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 (1):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:What We Want • The applet must be notified of events • Example: • a button is clicked • applet should do something Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  13. Event Handling:Three Objects of Interest • The event source • example: a Button— "causes" the event • The event • example: an ActionEvent— represents the event • The listener • example: an Applet — must react in some way Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  14. Button Event BasicElements event source listener Event Handling:How Can The Objects Give Us What We Want? • Source (button) notifies the listener (applet) • Source (button) sends message to listener (applet) • Event object (ActionEvent) is argument Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  15. Event Handling:What Must Be Arranged? • Source (button) must know listener (applet) • but listener (applet) is creator of Source (button)! • Source (button) must provide registration method • Source (button) must know what message to send • must know that listener (applet) can receive message Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  16. Event Handling:Listener Interfaces • Our listener (applet) must implement the listener interface, particular to the event. • interface: ActionListener • one method: actionPerformed(ActionEvent) class … extends Applet implements ActionListener { • • • public void actionPerformed(ActionEvent ae) { Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  17. Event Handling: Registration • Listener (applet) must first send message to button-listener to accomplish registration: red.addActionListener(this) • Source (button) must define registration method: public void addActionListener(ActionListener a) Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  18. Event Handling: Registration class … implements ActionListener { . . . red.addActionListener(this) . . . void actionPerformed(ActionEvent e) { … } class Button { public void addActionListener(ActionListener a) { . . . } . . . a.actionPerformed(new ActionEvent(…)); . . . } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  19. 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

  20. Overriding init (2):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

  21. Implementing ActionPerformed • Gotta make good on our "implements" claim public void actionPerformed(ActionEvent ae) { // change display } Arnow and Weiss: Objects At Their Best— Java's Abstract Window Toolkit

  22. 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

  23. 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

  24. 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

  25. 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

  26. Back to Events:actionPerformed method 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

  27. 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

  28. Overriding init (3):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

  29. 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

  30. 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

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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

More Related