Description of a flower:
(1) a flower has:
- color (a string)
- position (two numbers)
- spin angle (a number)
(2) a flower can be created:
- by giving value for its color
the other properties set to
random values
(3) a flower can do:
//
// - tell us its color,
// position, etc.
// (omitted from example)
//
- update its X, Y position
- rotate itself by a fixed
angle of 30 degrees
- draw itself based on
its location, color
and angle of spin
|
package cs1.ParkApp;
import cs1.app.*;
public class Flower
{
// data members: the "features/characteristics" of Flower
// typically private
private String color;
private double curX;
private double curY;
private int curAngle;
// constructor(s): same name as class, no return type
public Flower( String initColor )
{
color = initColor;
curX = canvas.getRandomInt( 0, canvas.getWidth() );
curY = canvas.getRandomInt( 0.75*canvas.getHeight(), canvas.getHeight() );
curAngle = 0;
}
// methods: the "actions/behaviors" of Flower
// typically public
//
// could put methods/behaviors
// getColor(), getX(), getY()
// omitted from this example
//
public void spin( )
{
curAngle = ( curAngle + 30 ) % 360; // increase angle by 30 degrees
// (use % as alternative to if)
}
public void draw( )
{
String filename = "flower" + curAngle + ".png"
canvas.drawImage( curX, curY, fileName );
canvas.drawCircle( curX, curY, 10, color );
}
}
|