1 / 19

ObjectDraw and Objects Early

ObjectDraw and Objects Early. Chris Nevison Barbara Wells. Objects Early. Introduce Programming in Modern Context Use Graphics for Motivation Work with Objects from Day One ObjectDraw Library (Bruce, Danyluk, Murtaugh) text: Java: An Eventful Approach , under development

jaden
Download Presentation

ObjectDraw and Objects Early

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. ObjectDraw and Objects Early Chris Nevison Barbara Wells

  2. Objects Early • Introduce Programming in Modern Context • Use Graphics for Motivation • Work with Objects from Day One • ObjectDraw Library (Bruce, Danyluk, Murtaugh) • text: Java: An Eventful Approach, under development • provides tools for OO early with graphics • teaches use of API • leads to use of standard tools

  3. Elevator import objectdraw.WindowController; import objectdraw.FilledRect; import objectdraw.Location; public class ElevatorSystem1 extends WindowController{ private FilledRect elevator; public void begin() { elevator = new FilledRect(100, 200, 20, 30, canvas); } public void onMouseClick(Location point) { elevator.move(0, -60); } }

  4. Reading the Code: "import" • import • provides access to classes from named package (library) • objectdraw is package • library classes used are • WindowController • FilledRect • Location • Information available from Application Programming Interface (API)

  5. Reading the Code: "class" • Declaration of a class begins with • public class <<name of class>> • name here is ElevatorSystem1 • keyword extends means this class is an extension of another, in this case WindowController • this means ElevatorSystem1is aWindowController • it has all the attributes and methods of a WindowController • it may add its own attributes and methods • it may override WindowController methods to change their behavior

  6. Reading the Code: "private" • Every component of a class has an indication of visibility: we will use two of these throughout: "private" and "public" (later we will add a third) • private means this component can only be accessed within this class • public means this component can be accessed from any other class, by the appropriate naming conventions

  7. Reading the Code: "private" • private FilledRect elevator; • private means this data field can only be used within this class specification • We will always make data fields private • FilledRect declares that the following identifier will be a reference to a FilledRect (as defined in the objectdraw package, by the import statement) • elevator is an identifier that we choose to represent an instance variable -- a variable that is supplied for every object (instance) of this class • No value has yet been supplied for this variable

  8. Reading the Code: "public void begin" • begin is a method supplied by WindowController that we must override • whatever actions we specify in the body of begin will be carried when the ElevatorSystem1 code first starts • the body of the method is everything between the { and matching } • public makes this method accessible from other classes • void indicates that this method does not return any result • the ( ) are a place where information (parameters) can be given to the method -- none are given for begin

  9. Reading the Code: "elevator =" • This statement is inside the { } so it is the body of the begin method and will be executed when the ElevatorSystem1 first starts • elevator refers to the declared instance variable • = is the assignment operator • the expression on the right is evaluated and assigned to the variable on the left (backwards, perhaps) • the left side must be a variable that the right side can be assigned to

  10. Reading the Code: "elevator =" • new on the right is the operator that creates a new object. • FilledRect is the new object being created • (100.0, 200.0, 20.0, 30.0, canvas) is the list of actual parameters used to create the FilledRect • we can find out what actual parameters are needed by consulting constructors in the API. This constructor needs four numbers. The first two specify the x and y coordinates of the upper left corner of the FilledRect, the next two the width and height (all in pixels)

  11. Reading the Code: "elevator =" • what is canvas? • a FilledRect needs a drawing canvas where is "lives" - where it is drawn and can move • canvas is a drawing canvas the is an attribute of the WindowController class that our ElevatorSystem1 inherits -- we do not need to worry about the details • So FilledRect is at the 100, 200 location of the supplied canvas, with width 20 and height 30 pixels. • Note: x (horizontal) coordinates increase left to right, y (vertical) increase top to bottom and the (0, 0) location is in the upper left of the window

  12. Reading the Code, summary so far • We are using tools specified from the objectdraw package • Since our ElevatorSystem1 class extends the WindowController class, we get things like the canvas (for drawing objects) automatically • We override the begin method to indicate what happens when the ElevatorSystem1 is first started • Our begin method creates a FilledRect and assigns a reference to the instance variableelevator. The FilledRect is drawn.

  13. Reading the Code: onMouseClick • onMouseClick is one of several methods for the WindowController that respond to user mouse actions, that we can override • public so it is accessible from other classes • void because it does not return any result • (Location point) indicates that this method must be supplied with a Location when it is called (somewhere in the system)

  14. Reading the Code: elevator.move • We are in the body of the onMouseClick method, specifying what happens when the system calls this method in response to a user action. • elevator refers to the instance variable • the "." means access a component of that object • move is a method of the elevator object -- it is specified in the FilledRect class specification (we can get information on this from the API) • (0.0, -60.0) are the actual parameters supplied to the move method, the 0.0 is the distance moved in the x (horizontal) and -60.0 in the y (vertical) direction. In the vertical, a negative move is up.

  15. Reading the Code, summary • When the code starts, a filled rectangle (FilledRect) is draw at the given position • Whenever the user clicks the mouse, the rectangle moves up, modeling an elevator • The system calls the onMouseClick method in response to outside events (the user clicking the physical mouse). This is typical of modern programs and is called event-driven programming

  16. Exercise 1 • Draw a Scene • Consult the objectdraw API to find different shapes available: FilledRect, FilledOval, FramedRect, FramedOval, FramedArc, Line • Use these shapes to draw a house or another scene • All your code can be in begin method

  17. Exercise 2 • Modify the ElevatorSystem1 example. • follow instructions to open the Elevator1.java file • follow instructions to open the objectdraw API • look at the API for FilledRect and Location • read the methods available, including inherited methods • Make a change in the behavior by changing the body of either the begin method or the onMouseClick method • change color, move differently, use moveTo instead of move, use the Location formal parameter • Note: for colors try Color.yellow, etc. See the API for Color in the java.awt documentation

  18. Aspects of Java in this example • Use of libraries (import package code) • Declaration of a class • extend another class • inherit attributes and methods • Instance variable (private) • Override inherited methods • begin, onMouseClick • Create an object (new FilledRect) • Invoke object method (elevator.move) • supply actual parameters

  19. objectdraw Context (for teachers) • WindowController extends a class called applet • An applet • can run in a browser window (or with appletviewer) • always starts with init method • WindowController calls the begin method in its init method, after doing some preliminary setup of the system • Freestanding Java programs are applications • start with a class with a main method • can also invoke graphics and user events, using the awt or swing libraries

More Related