Page 581 - Introduction to Programming with Java: A Problem Solving Approach
P. 581
Figure 13.24a
Cylinder class—part A
13.11 GUI Track: Three-Dimensional Graphics (Optional) 547
/**************************************************************
*
Cylinder.java
Dean & Dean
This displays a cylinder illuminated from viewing direction.
**************************************************************/
*
*
*
import javax.swing.JPanel;
import java.awt.*;
// for Graphics, Graphics2D, Color
// for Ellipse2D and GeneralPath
import java.awt.GradientPaint;
import java.awt.Rectangle;
import java.awt.geom.*;
public class Cylinder extends JPanel
{
private double cylElev;
private double cylAzm;
private double cylH = 400;
private double cylD = 200;
private float c1 = 0.3f;
private float c2 = 0.7f;
// cylinder axis elevation radians
// cylinder axis azimuth radians
// cylinder height in pixels
// cylinder diameter in pixels
// minimum illumination brightness
// maximum illumination brightness
//***********************************************************
Apago PDF Enhancer
public Cylinder(double elev, double azimuth)
{
}
cylElev = Math.toRadians(elev);
if (Math.abs(cylElev) >= Math.PI / 2.0)
{
}
{
}
cylAzm = Math.signum(cylAzm) * Math.PI / 2.0001;
cylElev = Math.signum(cylElev) * Math.PI / 2.0001;
cylAzm = Math.toRadians(azimuth);
if (Math.abs(cylAzm) >= Math.PI / 2.0)
inheritance from the API JPanel class
lier in the chapter, it’s always legal to pass a descendant-class argument into a ancestor-class parameter. Also you may recall that to call descendant-class methods with the ancestor-class parameter, first you need to assign the ancestor-class parameter into a descendant-class variable. Here’s the relevant code from Figure 13.24b:
Graphics2D g2d = (Graphics2D) g;
Notice that everything in Figure 13.24b is some kind of declaration. As before, the program uses double for the MIDX and MIDY pixel positions to preserve fractional information. For use later, it casts the incoming