Java Reference
In-Depth Information
constructor when you create a BigDecimal . You may also provide a MathContext to var-
ious BigDecimal methods that perform calculations. Class MathContext contains several
pre-configured MathContext objects that you can learn about at
http://docs.oracle.com/javase/7/docs/api/java/math/MathContext.html
By default, each pre-configured MathContext uses so called “bankers rounding” as ex-
plained for the RoundingMode constant HALF_EVEN at:
http://docs.oracle.com/javase/7/docs/api/java/math/
RoundingMode.html#HALF_EVEN
Scaling BigDecimal Values
A BigDecimal 's scale is the number of digits to the right of its decimal point. If you need
a BigDecimal rounded to a specific digit, you can call BigDecimal method setScale . For
example, the following expression returns a BigDecimal with two digits to the right of the
decimal point and using bankers rounding:
amount.setScale( 2 , RoundingMode.HALF_EVEN )
8.16 (Optional) GUI and Graphics Case Study: Using
Objects with Graphics
Most of the graphics you've seen to this point did not vary with each program execution.
Exercise 6.2 in Section 6.13 asked you to create a program that generated shapes and col-
ors at random. In that exercise, the drawing changed every time the system called paint-
Component . To create a more consistent drawing that remains the same each time it's
drawn, we must store information about the displayed shapes so that we can reproduce
them each time the system calls paintComponent . To do this, we'll create a set of shape
classes that store information about each shape. We'll make these classes “smart” by allow-
ing objects of these classes to draw themselves by using a Graphics object.
Class MyLine
Figure 8.17 declares class MyLine , which has all these capabilities. Class MyLine imports
classes Color and Graphics (lines 3-4). Lines 8-11 declare instance variables for the co-
ordinates of the endpoints needed to draw a line, and line 12 declares the instance variable
that stores the color of the line. The constructor at lines 15-22 takes five parameters, one
for each instance variable that it initializes. Method draw at lines 25-29 requires a Graph-
ics object and uses it to draw the line in the proper color and between the endpoints.
1
// Fig. 8.17: MyLine.java
2
// MyLine class represents a line.
3
import java.awt.Color;
4
import java.awt.Graphics;
5
6
public class MyLine
7
{
Fig. 8.17 | MyLine class represents a line. (Part 1 of 2.)
 
 
Search WWH ::




Custom Search