chapter 2

22
Chapter 2 Objects and Mutator Methods

Upload: kuper

Post on 04-Jan-2016

27 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 2

Chapter 2

Objects and Mutator Methods

Page 2: Chapter 2

A Sample Program

A rudimentary solar simulation:

We want a click of the mouse in the window to make the ‘sun’ rise

Page 3: Chapter 2

Mutator Methods

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

Page 4: Chapter 2

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!

Page 5: Chapter 2

Some Building Blocks

private FilledOval sun;

sun = new FilledOval( 50, 150, 100, 100, canvas );

sun.move( 0, -5 );

Page 6: Chapter 2

Giving Names

• Declare all instance variablesprivate FilledOval sun;

• Appropriate Names– Start with letters– Case sensitive– Letters, digits, underscores– Not a word already in Java

Page 7: Chapter 2

Naming Examples

• 5sun illegal

• hello_world legal

• helloWorld legal

• line25 legal

Page 8: Chapter 2

Comments for Clarity

• In our line

private FilledOval sun; //sun is the name

• sun is the name is a comment

Page 9: Chapter 2

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

}

Page 10: Chapter 2

More Mutator Methods

private Text instructions; //Display of instructions

instructions = new Text (…);

instructions.hide();

instructions.show();

Page 11: Chapter 2

public class RisingSun extends WindowController {

private FilledOval sun; // Circle that represents the sunprivate Text instructions; //Display of instructions

public void begin() { //Place the sun and brief instructions on screensun = new FilledOval( 50, 150, 100, 100, canvas );instructions = new Text("Please click the mouse ",

20, 20, canvas ); }

//Move the sun up each clickpublic void onMouseClick( Location point ) {

sun.move( 0, -5 );instructions.hide();

}}

Page 12: Chapter 2

More Classes

• Classes so far: Line, FilledOval, Text

• Can also have nongraphical classes!– Color

– Location

Page 13: Chapter 2

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;

Page 14: Chapter 2

Colors

private Color purple;

purple = new Color (255, 0, 255);

sun.setColor( Color.YELLOW ); //makes sun yellow

instructions.setColor( purple ); //makes instr purple

Page 15: Chapter 2

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

Page 16: Chapter 2

Locations

private Location initialPosition;

initialPosition = new Location( 50, 150 );

sun.moveTo ( initialPosition );

Page 17: Chapter 2

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

Page 18: Chapter 2

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

Page 19: Chapter 2

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

Page 20: Chapter 2

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

}

Page 21: Chapter 2

Multiple Point Picture

• Can play connect the dots!

Page 22: Chapter 2

Review

• Graphical and nongraphical objects

• Names and mutator methods

• Layering the canvas