Page 579 - Introduction to Programming with Java: A Problem Solving Approach
P. 579

                13.11 GUI Track: Three-Dimensional Graphics (Optional) 545
 /**************************************************************
*
CylinderDriver.java
Dean & Dean
This drives the Cylinder class.
**************************************************************/
*
*
*
import java.util.Scanner;
import javax.swing.*;
// for JFrame and JPanel
public class CylinderDriver
{
}
// end CylinderDriver class
public static void main(String[] args)
{
}
Scanner stdIn = new Scanner(System.in);
JFrame frame = new
Cylinder cylinder;
JFrame("Three-Dimensional Cylinder");
cylinder axis elevation angle in degrees
cylinder axis azmuth angle in degrees
600);
double elev;
//
double azmuth; //
frame.setSize(600,
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.print("Enter axis elevation (-90 to +90): ");
stdInA.pneaxtgDooubleP()D;F Enhancer System.out.print("Enter axis azmuth (-90 to +90): "); azmuth = stdIn.nextDouble();
cylinder = new Cylinder(elev, azmuth); frame.add(cylinder);
frame.setVisible(true);
// end main
elev =
Sample session:
 Enter axis elevation (-90 to +90): -15
Enter axis azmuth (-90 to +90): 60
Figure 13.22 Driver for Cylinder class in Figures 13.24a, 13.24b, and 13.24c
Note the Cylinder constructor in Figure 13.24a. This transforms the input elevation and azimuth angles from degrees to radians, and it also constrains the magnitudes of the input angles to less than 90 degrees. This avoids spurious results. It lets the user see only one end of the cylinder, but since the other end is the same, the program still portrays everything of interest.
Now look at Figure 13.24b. This contains the first part of a large paintComponent method. The JVM automatically calls the paintComponent method when the program first runs and whenever a user does something to alter the contents of the program’s window. (For example, if a user maximizes a window, the JVM calls the program’s paintComponent method.) The paintComponent method defined here overrides a paintComponent method defined in the JComponent class, which is the superclass of the JPanel class and therefore an ancestor of the Cylinder class. To ensure that graphical components























































   577   578   579   580   581