|
InheritanceExercise
Oval.java
import java.awt.*;
/**
* Oval class
*
* @author Chuck Cusack
* @version 1.0, September, 2006
*/
public class Oval
{
private int x;
private int y;
private int width;
private int height;
private Color color;
private boolean isFilled;
/**
* The defualt constructor, which creates a 0 by 0 object at (0,0)
* that is white and not filled.
*/
public Oval() {
setLocationAndSize(0,0,0,0);
setColor(Color.white);
setFilled(false);
}
/**
* The constructor that creates an object at (x,y) of size
* width by height, of the given color, and filled or hollow
* based on the value of isFilled
*/
public Oval(int x, int y, int width, int height,
Color color, boolean isFilled) {
setLocationAndSize(x,y,width,height);
setColor(color);
setFilled(isFilled);
}
/*
* The standard get and set methods
*/
public void setFilled(boolean isFilled) {
this.isFilled=isFilled;
}
public boolean getFilled() {
return isFilled;
}
public void setColor(Color color) {
this.color=color;
}
public Color getColor() {
return color;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width=width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height=height;
}
public void setSize(int width,int height) {
setWidth(width);
setHeight(height);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x=x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y=y;
}
public void setLocation(int x,int y) {
setX(x);
setY(y);
}
public void setLocationAndSize(int x,int y,int width,int height) {
setLocation(x,y);
setSize(width,height);
}
/**
* Overriding drawObject from GraphicalObject so it actually
* draws something.
*/
public void drawObject(Graphics g) {
// Save the current color so we can reset it when we are done
Color oldColor=g.getColor();
g.setColor(this.getColor());
if(isFilled) {
drawFilledShape(g);
} else {
drawHollowShape(g);
}
// Reset the color back to the original.
g.setColor(oldColor);
}
//-------------------------------------------------------------------
// The methods to draw the shapes, based on whether they are filled
// or not. The changing of the color is taken care of already,
// so these methods only need to draw the object.
/**
* The method to draw the oval if it is filled in.
*/
private void drawFilledShape(Graphics g) {
g.fillOval(x,y,width,height);
}
/**
* The method to draw the oval if it is not filled in.
*/
private void drawHollowShape(Graphics g) {
g.drawOval(x,y,width,height);
}
}
|