Chapter 2 Objects and Mutator Methods. A Sample Program A rudimentary solar simulation: We want a click of the mouse in the window to make the ‘sun’ rise.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

In put Devices and Media In order for a computer to do anything it must be told what to do.
1What is the Stage. 2How do you open a panel in Flash
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Programming What is a program? –A set of instructions –Understood by a computer.
Recall: Accessing Location Info private Location firstPoint; public void onMousePress( Location pressPt ) { new Text("Pressed", pressPt, canvas ); firstPoint.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
A Sample Program A rudimentary solar simulation: We want a click of the mouse in the window to make the ‘sun’ rise.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 5 Graphics. Topics Applets Classes used for graphics –Graphics –Point –Dimension –Color.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Slide 1 Chapter 2 Visual Basic Interface. Slide 2 Chapter 2 Windows GUI  A GUI is a graphical user interface.  The interface is what appears on the.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
A First Program Using C#
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
CS413: Java Programming language Applications Applets
Microsoft Word 2010 Chapter 1 Creating, Formatting, and Editing a Word Document with Pictures.
Agenda For Feb Finish up Unit 3 Exercises on page Unit 4 Exercises on page 33. Question #2 3. Create a Happy Face Java Applet (due next class).
Overview of our Approach Program Structure –Data, variables, and parameters –Basic control structures (conditionals, loops and Threads) –Class definitions.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
© 2011 Delmar, Cengage Learning Chapter 9 Introduction to ActionScript 3.0.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
JAVA JAVA is an object-oriented programming (OOP) language introduced by Sun Microsystems in In the Java programming language: A program is made.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Classes CS 21a: Introduction to Computing I First Semester,
Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Exploring Computer Science 2/9/15 2/9Moving Scratch 2/10Moving Scratch worksheet 2/11 Scratch Baseball Game 2/12Scratch Alphabet Learning Game 2/13Game.
Java: An Eventful Approach An innovative approach to teaching Java in CS1 † Recursion Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Multiplication Level 3 (facts 0 - 9) Read These Instructions:  Try to get the answer before the computer puts it on the screen.  Do not click on your.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
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?
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
The Cortex-M3 Embedded Systems: Programming With the Kitronix K350QVG-V1-F LCD Refer to “Kitronix Ltd. PRODUCT SPECIFICATION” “SSD2119 Advance Information”
Graphics Tools and Parameters Chris Nevison Barbara Wells.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Java Working with Numbers (Java: An Eventful Approach, Ch 3), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
Multiplication Level 1 (Facts 0-2) Read These Instructions:  Try to get the answer before the computer puts it on the screen.  Do not click on your.
Multiplication Level 2 (facts 0-2, 5, 9) Read These Instructions:  Try to get the answer before the computer puts it on the screen.  Do not click on.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
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.
Multiplication Level Read These instructions:  Try to get the answer before the computer puts it on the screen.  Do not click on your mouse,
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Chapter 3 GC 101 Java Fundamentals.
Building Java Programs
Building Java Programs
Java Classes (Java: An Eventful Approach, Ch. 6),
Chapter 3 Classes and Objects
Implementing Classes Chapter 3.
Java objects: a first view
Classes CS 21a: Introduction to Computing I
Presentation transcript:

Chapter 2 Objects and Mutator Methods

A Sample Program A rudimentary solar simulation: We want a click of the mouse in the window to make the ‘sun’ rise

Mutator Methods Mutator methods: change the state of an object –move to different location, –change size –change color

Moving an Oval Mutator methods: change an object that’s been created move ( 0, -5 ); But how does the computer know what to move? Give the object a name!

Some Building Blocks private FilledOval sun; sun = new FilledOval( 50, 150, 100, 100, canvas ); sun.move( 0, -5 );

Giving Names Declare all instance variables private FilledOval sun; Appropriate Names –Start with letters –Case sensitive –Letters, digits, underscores –Not a word already in Java

Naming Examples 5sun illegal hello_world legal helloWorld legal line25 legal

Comments for Clarity In our line private FilledOval sun; //sun is the name sun is the name is a comment

Mutator Methods in Context We want to move the sun when the user clicks the mouse: public void onMouseClick( Location point){ sun.move( 0, -5 ); }

More Mutator Methods private Text instructions; //Display of instructions instructions = new Text (…); instructions.hide(); instructions.show();

public class RisingSun extends WindowController { private FilledOval sun;// Circle that represents the sun private Text instructions;//Display of instructions public void begin() { //Place the sun and brief instructions on screen sun = new FilledOval( 50, 150, 100, 100, canvas ); instructions = new Text("Please click the mouse ", 20, 20, canvas ); } //Move the sun up each click public void onMouseClick( Location point ) { sun.move( 0, -5 ); instructions.hide(); }

More Classes Classes so far: Line, FilledOval, Text Can also have nongraphical classes! –Color –Location

New Data Type: Color Color represents an R-G-B format –255, 0, 255 (purple) A Color CONSTANT: –Color.YELLOW A Color object: –new Color(255,0,255); A Color variable: –private Color purple;

Colors private Color purple; purple = new Color (255, 0, 255); sun.setColor( Color.YELLOW ); //makes sun yellow instructions.setColor( purple ); //makes instr purple

New Data Type: Location Location represents an x,y coordinate –new Location(50,150); –private Location start; –start = new Location(50,150); –sun.moveTo(start); Locations can use.translate –similar to move for graphical objects –start.translate(10,20);

Locations private Location initialPosition; initialPosition = new Location( 50, 150 ); sun.moveTo ( initialPosition );

Example using translate private Location start; sun.moveTo(start); sun at 50,150 sun.move(0, -1); sun at 50, 149 start.translate(0,-1); start is 50,149 sun.moveTo(start); sun at 50,149

Layering the Canvas Create two overlapping FilledOvals. Which one’s on top? –Answer: The most recently constructed How do we change the order? –Answer: Mutator Methods sendBackward() sendForward() sendToBack() sendToFront()

Using a Mouse Point Recall onMousePress( Location point ); –We can use the Location! Consider: public void onMousePress ( Location point ) { new Text ( "Pressed", point, canvas ); } Displays "Pressed" wherever the mouse is clicked

Using Multiple Points private Location firstPoint; public void onMousePress( Location pressPt ){ new Text("Pressed", pressPt, canvas ); firstPoint = pressPt; } public void onMouseRelease ( Location releasePt){ new Text("Released", releasePt, canvas ); new Line( firstPoint, releasePt, canvas ); }

Multiple Point Picture Can play connect the dots!

Review Graphical and nongraphical objects Names and mutator methods Layering the canvas