Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
bouncing-ball/SmartEllipse.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
68 lines (64 sloc)
2.12 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* SmartEllipse.java | |
* Extends Java's Ellipse2D.Double class, adding the capabilities to | |
* set color, rotation, location, and size, to move to a specified | |
* location, and to display itself on a panel. | |
*/ | |
public class SmartEllipse extends java.awt.geom.Ellipse2D.Double { | |
private java.awt.Color _borderColor, _fillColor; // attributes | |
private int _rotation; | |
private final int STROKE_WIDTH = 2; | |
public SmartEllipse(java.awt.Color aColor){ | |
_borderColor = aColor; | |
_fillColor = aColor; // solid color to start | |
_rotation = 0; // no rotation for now | |
} | |
// methods not provided by Java | |
public void setBorderColor (java.awt.Color aColor) { | |
_borderColor = aColor; | |
} | |
public void setFillColor (java.awt.Color aColor) { | |
_fillColor = aColor; | |
} | |
public void setRotation (int aRotation) { | |
_rotation = aRotation; | |
} | |
public java.awt.Color getFillColor() | |
{ | |
return _fillColor; | |
} | |
public java.awt.Color getBorderColor() | |
{ | |
return _borderColor; | |
} | |
// more readable versions of methods provided by Java | |
public void setLocation (double x, double y) { | |
this.setFrame (x, y, this.getWidth(), | |
this.getHeight()); | |
} | |
public void setSize (int aWidth, int aHeight) { | |
this.setFrame(this.getX(), this.getY(), | |
aWidth, aHeight); | |
} | |
public void move (int aChangeInX, int aChangeInY) { | |
this.setFrame((int)this.getX()+aChangeInX, | |
(int)this.getY()+aChangeInY, | |
this.getWidth(), | |
this.getHeight()); | |
} | |
public void fill (java.awt.Graphics2D aBetterBrush){ | |
java.awt.Color savedColor = aBetterBrush.getColor(); | |
aBetterBrush.setColor(_fillColor); | |
aBetterBrush.fill(this); // paint a solid ellipse | |
aBetterBrush.setColor(savedColor); | |
} | |
public void draw (java.awt.Graphics2D aBrush) { | |
java.awt.Color savedColor = aBrush.getColor(); | |
aBrush.setColor(_borderColor); | |
java.awt.Stroke savedStroke = aBrush.getStroke(); | |
aBrush.setStroke(new java.awt.BasicStroke(STROKE_WIDTH)); | |
aBrush.draw(this); | |
aBrush.setStroke(savedStroke); | |
aBrush.setColor(savedColor); | |
} | |
} |