Page 583 - Introduction to Programming with Java: A Problem Solving Approach
P. 583
Figure 13.24c
Cylinder class—part C
13.11 GUI Track: Three-Dimensional Graphics (Optional) 549
// Rotate image from vertical around center
if (cylElev == 0.0)
{
}
else
{
}
}
imageRotAngle =
Math.signum(Math.sin(cylAzm)) * Math.PI / 2.0;
imageRotAngle =
Math.atan(Math.sin(cylAzm) / Math.tan(cylElev));
if (Math.tan(cylElev) < 0)
{
}
imageRotAngle += Math.PI;
g2d.rotate(imageRotAngle, MIDX, MIDY);
// Define and paint curved sides of cylinder
shape = new GeneralPath(rectangle);
shape.append(backEllipse, false);
g2d.setPaint(gradientPaint);
g2d.fill(shape);
viAsipblae genod ofPcDylFindEernhancer c = c2 - (float) ((c2 - c1) * frontEndAngle); g2d.setColor(new Color(c, c, c)); g2d.fill(frontEllipse);
// end paint
// Paint
} // end Cylinder class
Parameter type is interface Shape, which Rectangle implements.
Parameter type is interface Paint, which GradientPaint implements.
You can use your understanding of geometry and trigonometry to verify the calculation of the apparent angle, apparent cylinder height, and minor diameter of the elliptical ends of a cylinder that’s oriented as the input specifies. The declarations under “// Shapes of curved sides and oval ends” use these values to define a rectangle and two ellipses. The rectangle and backEllipse objects will help configure the cylinder’s sides in a GeneralPath object called shape, and the frontEllipse object will enable the program to paint the cylinder’s visible end.
The last declaration in Figure 13.24b instantiates a GradientPaint object. This establishes a color gradation that makes the sides of the cylinder appear to be round. In the range between x (MIDX clyD / 2) and x (MIDX), this creates 16 narrow vertical stripes with color varying linearly from an intensity given by c1 on the left to an intensity given by c2 on the right. The true argument makes the method ramp the shading back down again in the range between x (MIDX) and x (MIDX clyD / 2).
Notice that the rectangle and both ellipses are defined in a vertical orientation, but the cylinder in Fig- ure 13.23 is not vertical! It is oriented at an angle sloping down and to the right. This requires a rotation. Now look at Figure 13.24c. The subordinate statement in the else part of the if statement computes the amount of rotation. The call to Graphics2D’s rotate method after the else clause tells the computer