Java Reference
In-Depth Information
18. Replace the following line in the
paint
method
g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
with
Color brown =
new
Color(200, 150, 0);
g.setColor(brown);
g.fillOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
Note that there is no predefined color constant
Color.BROWN
, so you need to
define a color for brown. You may prefer some other arguments instead of
(200,
150, 0)
so that you get a shade of brown that is more to your liking.
19.
g.setFont(
new
Font("SansSerif", Font.BOLD, 14));
20.
g.setFont(
new
Font("SansSerif", Font.BOLD|Font.ITALIC, 14));
Programming Projects
Visit
www.myprogramminglab.com
to complete select exercises online and get instant
feedback.
1. A Sierpinski Gasket or Triangle is a type of fractal named after the Polish
mathematician Waclaw Sierpinski who described some of its interesting properties
in 1916. It is a nice example of how an orderly structure can be created as a result
of random, chaotic behavior.
One way to create the fractal is to start with an equilateral triangle. Let us say that
the corners are labeled
X, Y,
and
Z
.
1.
Set
current
equal to point
X.
2. Repeat many times (you can try 10000).
a. Randomly pick
target
as one of the three
X, Y,
or
Z
.
b. Calculate the point halfway between
current
and
target.
c. Set
current
to this halfway point.
d. Draw a pixel at location
current
. One way to do this is to fill or draw a tiny
rectangle at this coordinate.
Write a program that draws a Sierpinski Gasket. You can pick the coordinates for
the corners of the triangle. It may seem like you should get a random mess of dots
but instead you get a very orderly picture!
To draw a single pixel at coordinate (X,Y), use the
drawLine
method where the
start and endpoints are both (X,Y).