import java.awt.*;
public class Oval
{
// Fields (instance variables)
int x, y; // centre of object
int height; // width is half height
int width;
Color c; // color to draw oval
// Constructors
/**
* Creates an oval with the specified x,y and
* height
*
* @param initialX initial x
* @param initialY initial y
* @param initialHeight initial height
**/
public Oval (int initialX, int initialY, int initialHeight)
{
x = initialX;
y = initialY;
height = initialHeight;
width = height / 2;
c = Color.BLUE;
}
/**
* Creates an oval with the specified x,y,
* height, and color
*
* @param initialX initial x
* @param initialY initial y
* @param initialHeight initial height
* @param initialColor initial color
**/
public Oval (int initialX, int initialY, int initialHeight, Color initialColor)
{
x = initialX;
y = initialY;
height = initialHeight;
width = height / 2;
c = initialColor;
}
// Methods
/**
* Set oval position
*
* @param newX New x position
* @param newY New y position
**/
public void setPosition (int newX, int newY)
{
x = newX;
y = newY;
}
/**
* Set oval height
*
* @param newHeight New height
**/
public void setHeight (int newHeight)
{
height = newHeight;
width = height / 2;
}
/**
* Draw oval on graphics object
*
* @param g Graphics object for drawing
**/
public void draw (Graphics g)
{
int drawX = x - width / 2;
int drawY = y - height / 2;
g.setColor(c);
g.fillOval (drawX, drawY, width, height);
}
}
|