ObjectDraw and Objects Early Chris Nevison Barbara Wells.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Outline 3.1 Introduction 3.2 Sample Applets from the Java 2.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Java Applets. Road Map Introduction to Java Applets Review applets that ship with JDK Make our own simple applets –Introduce inheritance –Introduce the.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Programming What is a program? –A set of instructions –Understood by a computer.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
A Simple Applet.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
Chapter 5 - Making Music: An On-Screen Piano
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Overview of our Approach Program Structure –Data, variables, and parameters –Basic control structures (conditionals, loops and Threads) –Class definitions.
JAPPLET.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
POS 406 Java Technology And Beginning Java Code
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Intro to Applets August 19, 2008 Mrs. C. Furman. Java Applets vs. Java Applications Java Applet: a program that is intended for use on the web. Java Applet:
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Applets Yong Choi School of Business CSU, Bakersfield.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
A Class of Our Own We’ve used many classes: –Location –FilledRect –Color –Text –And More! –But what if we want to make our own class?
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Java Object Oriented Programming Encapsulation, Inheritance, Polymorphism (Java: An Eventful Approach - Ch. 17, 21.7), Slides Credit: Bruce, Danyluk and.
Chapter 5 Introduction to Defining Classes
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Graphics Tools and Parameters Chris Nevison Barbara Wells.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
Creating a Java Application and Applet
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Java Objects (Java: An Eventful Approach, Ch 2), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture October 2012.
Teaching Control Structures, using objectdraw Chris Nevison Barbara Wells.
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
A structured walkthrough
Lecture 09 Applets.
Object Oriented Programming
Java Classes (Java: An Eventful Approach, Ch. 6),
Outline Character Strings Variables and Assignment
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Presentation transcript:

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 –provides tools for OO early with graphics –teaches use of API –leads to use of standard tools

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); }

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)

Reading the Code: "class" Declaration of a class begins with –public class > –name here is ElevatorSystem1 –keyword extends means this class is an extension of another, in this case WindowController –this means ElevatorSystem1 is a WindowController 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

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

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

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

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

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)

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

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 variable elevator. The FilledRect is drawn.

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)

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 in the y (vertical) direction. In the vertical, a negative move is up.

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

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

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

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

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