21
Graphics Tools and Parameters Chris Nevison Barbara Wells

Graphics Tools and Parameters Chris Nevison Barbara Wells

Embed Size (px)

Citation preview

Page 1: Graphics Tools and Parameters Chris Nevison Barbara Wells

Graphics Tools and Parameters

Chris Nevison

Barbara Wells

Page 2: Graphics Tools and Parameters Chris Nevison Barbara Wells

Two non-displayable classes

• Color– specifies a color– from java.awt library

• Location– specifies a location in two dimensions– part of objectdraw library

Page 3: Graphics Tools and Parameters Chris Nevison Barbara Wells

class Color

• constants– Color.white, Color.blue, Color.yellow, etc

• constructor: – takes red, green, blue parametersColor skyBlue = new Color(200, 200, 255);

• often used to set color of another objectelevator.setColor(Color.blue);

elevator.setColor(new Color(100, 150, 50));

Page 4: Graphics Tools and Parameters Chris Nevison Barbara Wells

class Location

• represents a position in 2 dimensional space– uses real value coordinates (double)

• has accessor methods to return coordinates– getX(), getY()

• has modifier or mutator methods to changeloc.translate(dx, dy);

• constructor sets initial positionLocation loc = new Location(dx, dy);

Page 5: Graphics Tools and Parameters Chris Nevison Barbara Wells

Formal Parameters

• Specified in the definition of the method

• Represent information passed to the method when it is called

• Can be used to determine the behavior of the method or in calculations carried out by the method.

Page 6: Graphics Tools and Parameters Chris Nevison Barbara Wells

Formal Parameter Example

public void onMouseClick(Location point) { elevator.moveTo(point); }

Formal ParameterType of parameter

Using the formal parameter

Declaration of method

Page 7: Graphics Tools and Parameters Chris Nevison Barbara Wells

Actual Parameters

• the values given as parameters when a method is called– may be a literal value (number, string literal)– may be a variable in the current context– may be an expression– expression may be a newly created object

• new ...

Page 8: Graphics Tools and Parameters Chris Nevison Barbara Wells

Actual Parameters

• Expression used for actual parameter must match the type specified in the declaration of the method

• Example: FilledRect documentation specifies that the moveTo method has a parameter of type Location– call to moveTo for an instance of FilledRect

must supply an actual parameter of that type

Page 9: Graphics Tools and Parameters Chris Nevison Barbara Wells

Actual Parameters

elevator.moveTo(point);

instance of FilledRect

call to method

actual parameter

Page 10: Graphics Tools and Parameters Chris Nevison Barbara Wells

Actual Parameters

elevator.moveTo(new Location(30,60));

instance of FilledRect

call to method

actual parameter,

a newly created Location

with no name

Page 11: Graphics Tools and Parameters Chris Nevison Barbara Wells

Communicating among methods using parameters

• objective: draw a line from location where mouse is pressed to location where mouse is released

• use WindowController methods– onMousePress– onMouseRelease– each has a formal parameter Location point

• create a Line with constructor that takes two Location parameters for the two ends

Page 12: Graphics Tools and Parameters Chris Nevison Barbara Wells

LineDraw, first attempt

public class LineDraw1 extends WindowController{ public void onMousePress(Location point) { Location startLine = point; }

public void onMouseRelease(Location point) { Location endLine = point; new Line(startLine, endLine, canvas); }}

Page 13: Graphics Tools and Parameters Chris Nevison Barbara Wells

Compile Error

C:\apwork\Workshops\summer02\ObjDrawCode\LineDraw\LineDraw1.java:15: cannot resolve symbolsymbol : variable startLine location: class LineDraw1 new Line(startLine, endLine, canvas); ^1 error

error type

offending word

location of error

Page 14: Graphics Tools and Parameters Chris Nevison Barbara Wells

LineDraw, first attempt

public class LineDraw1 extends WindowController{ public void onMousePress(Location point) { Location startLine = point; }

public void onMouseRelease(Location point) { Location endLine = point; new Line(startLine, endLine, canvas); }}

local variables

parameters

local to other method, not visible here

Page 15: Graphics Tools and Parameters Chris Nevison Barbara Wells

Scope

• variables declared within a method are only visible within that method

• formal parameters for a method are only visible within that method

• instance variables are visible within the class– an instance variable provides a means of

communicating between methods within the same class

Page 16: Graphics Tools and Parameters Chris Nevison Barbara Wells

LineDraw, correctpublic class LineDraw2 extends WindowController{

Location startLine;

public void onMousePress(Location point) { startLine = point;// assigns to instance variable }

public void onMouseRelease(Location point) { Location endLine = point; new Line(startLine, endLine, canvas); }}

instance variable

assign to the instance variable

use the instance variable

Page 17: Graphics Tools and Parameters Chris Nevison Barbara Wells

Example: Scribble

• from Java, An Eventful Approach

• Uses these WindowController methods– onMousePress– onMouseDrag

• onMouseDrag is called repeatedly while the mouse is dragged

Page 18: Graphics Tools and Parameters Chris Nevison Barbara Wells

Scribble, attempt 1public class Scribble1 extends WindowController{

Location startLine;

public void onMousePress(Location point)

{

startLine = point;

}

public void onMouseDrag(Location point)

{

Location endLine = point;

new Line(startLine, endLine, canvas);

}

}

Page 19: Graphics Tools and Parameters Chris Nevison Barbara Wells

Scribble, attempt 1 result

Page 20: Graphics Tools and Parameters Chris Nevison Barbara Wells

Scribble, correctpublic class Scribble2 extends WindowController{

Location previousPoint; Location currentPoint;

public void onMousePress(Location point) { previousPoint = point; }

public void onMouseDrag(Location point) { currentPoint = point; new Line(previousPoint, currentPoint, canvas); previousPoint = currentPoint; }}

Page 21: Graphics Tools and Parameters Chris Nevison Barbara Wells

State of an object• Instance variables specify the attributes of

an object

• State of an object is the set of attributes that determine its current configuration, that may change

• In Scribble class, state is given by– previousPoint– currentPoint

• Modifier or Mutator methods change state– affect the future behavior of the object