package part2;
import java.awt.Color;
import java.awt.Point;
public class Triangle extends Shape {
private Point a, b, c;
/**
* Create a triangle with the specified vertices and color.
* @param a The first vertex.
* @param b The second vertex.
* @param c The third vertex.
* @param color The color of the triangle.
*/
public Triangle(Point a, Point b, Point c, Color color) {
super(color);
this.a = a;
this.b = b;
this.c = c;
}
/**
* Compute and return the area of this triangle.
*/
@Override
public double getArea() {
return 0.5 * Math.abs(
this.a.x * (this.b.y - this.c.y) +
this.b.x * (this.c.y - this.a.y) +
this.c.x * (this.a.y - this.b.y)
);
}
/**
* Compute and return the perimeter of this triangle.
*/
@Override
public double getPerimeter() {
return a.distance(b) + b.distance(c) + c.distance(a);
}
/**
* Move the triangle by the specified amount.
* @param p The amount to move the triangle.
*/
@Override
public void translate(Point p) {
this.a.translate(p.x, p.y);
this.b.translate(p.x, p.y);
this.c.translate(p.x, p.y);
}
public Point getVertexA() {
return this.a;
}
public Point getVertexB() {
return this.b;
}
public Point getVertexC() {
return this.c;
}
}
Triangle
has accessor methods for its "triangle-specific" instance variables.
Triangle
has accessor methods for its "triangle-specific" instance variables.
Triangle
has three instance variables, one for each vertex of the triangle.
It also has the color
instance variable that it inherits from Shape
.
Triangle
has three instance variables, one for each vertex of the triangle.
It also has the color
instance variable that it inherits from Shape
.
We call the superclass's constructor to initialize the color
instance variable.
This MUST happen as the very first line in the constructor.
If this line is missing, Java will move forward as if you had written
super();
instead. This will cause a compile-time error if the Shape
class does not have a default constructor (constructor with no parameters).
We call the superclass's constructor to initialize the color
instance variable.
This MUST happen as the very first line in the constructor.
If this line is missing, Java will move forward as if you had written
super();
instead. This will cause a compile-time error if the Shape
class does not have a default constructor (constructor with no parameters).
The extends
keyword is used to declare that this class is a subclass of
Shape
. This means that Triangle
inherits all of the instance variables
and methods of Shape
, and that Triangle
must implement the abstract
methods of Shape
.
The extends
keyword is used to declare that this class is a subclass of
Shape
. This means that Triangle
inherits all of the instance variables
and methods of Shape
, and that Triangle
must implement the abstract
methods of Shape
.
Triangle
overrides the getArea
, getPerimeter
, and translate
methods of Shape
.
Triangle
overrides the getArea
, getPerimeter
, and translate
methods of Shape
.
(x,y)
,
by dx
along the x
axis and dy
along the y
axis so that it now represents the point
(x+dx,y+dy)
.Point2D
to a
specified Point2D
.Point2D
to a
specified Point2D
.pt
- the specified point to be measured
against this Point2D
Point2D
and
the specified Point2D
.int
value.
If the argument is not negative, the argument is returned.
If the argument is negative, the negation of the argument is returned.
int
value.
If the argument is not negative, the argument is returned.
If the argument is negative, the negation of the argument is returned.
Note that if the argument is equal to the value of Integer.MIN_VALUE
, the most negative representable int
value, the result is that same value, which is negative. In
contrast, the absExact(int)
method throws an
ArithmeticException
for this value.
a
- the argument whose absolute value is to be determinedPoint
.
If no Y coordinate is set it will default to 0.Point
.
If no X coordinate is set it will default to 0.Color
class is used to encapsulate colors in the default
sRGB color space or colors in arbitrary color spaces identified by a
ColorSpace
. Every color has an implicit alpha value of 1.0 or
an explicit one provided in the constructor. The alpha value
defines the transparency of a color and can be represented by
a float value in the range 0.0 - 1.0 or 0 - 255.
An alpha value of 1.0 or 255 means that the color is completely
opaque and an alpha value of 0 or 0.0 means that the color is
completely transparent.
When constructing a Color
with an explicit alpha or
getting the color/alpha components of a Color
, the color
components are never premultiplied by the alpha component.
Color
class is used to encapsulate colors in the default
sRGB color space or colors in arbitrary color spaces identified by a
ColorSpace
. Every color has an implicit alpha value of 1.0 or
an explicit one provided in the constructor. The alpha value
defines the transparency of a color and can be represented by
a float value in the range 0.0 - 1.0 or 0 - 255.
An alpha value of 1.0 or 255 means that the color is completely
opaque and an alpha value of 0 or 0.0 means that the color is
completely transparent.
When constructing a Color
with an explicit alpha or
getting the color/alpha components of a Color
, the color
components are never premultiplied by the alpha component.
The default color space for the Java 2D(tm) API is sRGB, a proposed standard RGB color space. For further information on sRGB, see http://www.w3.org/pub/WWW/Graphics/Color/sRGB.html .
(x,y)
coordinate space,
specified in integer precision.(x,y)
coordinate space,
specified in integer precision.(x,y)
coordinate space,
specified in integer precision.(x,y)
coordinate space,
specified in integer precision.(x,y)
coordinate space,
specified in integer precision.(x,y)
coordinate space,
specified in integer precision.(x,y)
coordinate space,
specified in integer precision.(x,y)
coordinate space,
specified in integer precision.