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

                544 Chapter 13 Inheritance and Polymorphism
object and executes that method. The protected modifier allows direct access to variables and methods
from anywhere in the protected member’s subtree.
13.11 GUI Track: Three-Dimensional Graphics (Optional)
Now that you know how inheritance and interface-based polymorphism work, you should be able to fol- low some of the subtleties that make graphical painting work. This section provides a typical illustration of polymorphism in Java API usage.
The Java API provides several classes which together enable you to draw and color many two- dimensional shapes. In addition, the Graphics2D class includes two methods (draw3DRect and fill3DRect) that enable you to portray a simple three-dimensional shape. They draw a rectangle with shading that makes it look like the rectangle is either raised slightly above the page or depressed slightly below the page. In Chapters 16 and 17 we’ll show you how you can use these two methods to simulate an un-pressed or pressed button. But that’s about the extent of the help you can get from the Java API in the creation of what appear to be three-dimensional images.
Portraying a general three-dimensional image in Java requires consideration of geometry and trigono- metric calculations. In this section we’ll give you a taste of this by portraying an arbitrarily oriented solid cylinder. Figure 13.22 shows a driver for a class that displays such an object. In the declarations section, the JFrame constructor instantiates a window called frame. The subsequent method calls, setSize and setDefaultCloseOperation, establish that window’s size in pixels and what should happen when the user clicks the x box in its upper-right corner.
Now look at the two user proAmptsa. TghisoprogPramDuFses aEspnherhical cnoocrdeinarte system. In this kind of co- ordinate system, elevation is an angle that’s like latitude. A zero elevation input says the cylinder should lie flat, with its axis pointing at the equator. A plus or minus 90 degree elevation input says the cylinder should stand up, with its axis pointing at either the north pole or the south pole. Azimuth is an angle that’s like east longitude. With elevation at zero, a zero azimuth input says the cylinder axis should point right at the viewer. A positive azimuth input says it should point to the right, and a negative azimuth input says it should point to the left. The Cylinder constructor call instantiates the Cylinder object, and the subsequent add method call puts that object in the window. The setVisible method call makes the window’s contents visible. Figure 13.23 shows the resulting display for the input 􏰂15􏰃 elevation and 􏰀60􏰃 azimuth angles.
The class that defines this shape and describes how to paint it appears in Figures 13.24a, 13.24b, and 13.24c. In Figure 13.24a, notice that our Cylinder class extends the JPanel class, which is imported from the Java API. The instance variables include the basic attributes of the object—its height (cylH) and diameter (cylD). These are pixel values, which are inherently integers, but we declare them to be double. Why do we use double instead of int? Three-dimensional graphics typically involves a considerable amount of calculation. Declaring variables to be double forces automatic promotion of any int factors that might appear in expressions. This keeps track of fractional information and provides the best possible visual display.
The other instance variables describe attributes of the displayed image—its orientation angles and its il- lumination extremes. For simplicity, the program uses “white” illumination, which contains identical values for red, green, and blue components. The c1 and c2 variables represent the intensity of these components for shaded and directly illuminated surfaces, respectively. The program declares these variables to be of type float because that’s the type of the parameters in the Java API Color constructor. Zero corresponds to pitch black, and unity corresponds to pure white, so the specified c1 and c2 values correspond to two differ- ent shades of gray—the darkest and lightest shades seen on the curved sides of the cylinder in Figure 13.23.
 
























































































   576   577   578   579   580