graphics tools and parameters chris nevison barbara wells

Post on 18-Jan-2016

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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

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.

Formal Parameter Example

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

Formal ParameterType of parameter

Using the formal parameter

Declaration of method

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

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

Actual Parameters

elevator.moveTo(point);

instance of FilledRect

call to method

actual parameter

Actual Parameters

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

instance of FilledRect

call to method

actual parameter,

a newly created Location

with no name

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

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

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

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

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

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

Example: Scribble

• from Java, An Eventful Approach

• Uses these WindowController methods– onMousePress– onMouseDrag

• onMouseDrag is called repeatedly while the mouse is dragged

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

}

}

Scribble, attempt 1 result

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

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

top related