Page 582 - Introduction to Programming with Java: A Problem Solving Approach
P. 582
548 Chapter 13 Inheritance and Polymorphism
//***********************************************************
public void paintComponent(Graphics g)
{
super.paintComponent(g);
final double MIDX = 0.5 * getWidth();
final double MIDY = 0.5 * getHeight();
Graphics2D g2d = (Graphics2D) g;
double imageRotAngle;
GeneralPath shape;
float c;
// Apparent tipping of cylinder
double tipCosine = Math.cos(cylAzm)
// image rotation angle
// curved cylinder side
// current color level
* Math.cos(cylElev);
double tipSine = Math.sqrt(1.0 - tipCosine * tipCosine);
double frontEndAngle =
Math.acos(tipCosine) * 2.0 / Math.PI;
// Minor diameter of end ovals & apparent cylinder height
double minorD = cylD * tipCosine;
double apparentH = cylH *
// Shapes of curved sides
Rectangle rectangle = new
Apago PDF Enhancer
(int) Math.round(MIDX - cylD / 2),
(int) Math.round(MIDY -
(int) Math.round(cylD),
apparentH / 2),
(int) Math.round(apparentH));
Ellipse2D.Double frontEllipse = new Ellipse2D.Double(
(int) Math.round(MIDX -
(int) Math.round(MIDY -
(int) Math.round(cylD),
cylD / 2),
apparentH / 2 - minorD / 2),
(int) Math.round(minorD));
Ellipse2D.Double backEllipse = new Ellipse2D.Double(
(int) Math.round(MIDX -
(int) Math.round(MIDY +
(int) Math.round(cylD),
cylD / 2),
apparentH / 2 - minorD / 2),
tipSine;
and oval ends
Rectangle(
(int) Math.round(minorD));
// Color for sides of cylinder
GradientPaint gradientPaint = new GradientPaint(
(float) (MIDX - cylD / 2), 0.0f, new Color(c1, c1, c1),
(float) (MIDX), 0.0f, new Color(c2, c2, c2), true);
Passed in object’s class must be in Graphics2D subtree.
Figure 13.24b
Cylinder class—part B
Graphics parameter into a more specific Graphics2D reference. After declaring the working variables, imageRotAngle, shape, and c, it begins a sequence of initialized declarations. These declarations ac- tually implement most of the method’s calculations. Using declarations to implement sequential steps in a calculation provides self-documentation. It makes a long tedious calculation easier to understand by giving intermediate variables understandable names.