1 / 22

Chapter 2

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.

kuper
Download Presentation

Chapter 2

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. Chapter 2 Objects and Mutator Methods

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

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

  4. 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!

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

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

  7. Naming Examples • 5sun illegal • hello_world legal • helloWorld legal • line25 legal

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

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

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

  11. 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(); } }

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

  13. 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;

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

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

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

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

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

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

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

  21. Multiple Point Picture • Can play connect the dots!

  22. Review • Graphical and nongraphical objects • Names and mutator methods • Layering the canvas

More Related