/Users/lyon/j4p/src/bookExamples/ch27BusinessGraphics/charts/DrawUtil.java
|
1 package bookExamples.ch27BusinessGraphics.charts;
2
3 import java.awt.*;
4
5 /**
6 * DrawUtil is the drawing utility class which draws the actual
7 * graphs and their labels. It can only be constructed with an
8 * instance of DoubleDataBean. It is invoked by each type of graphics.graph.
9 * Global Variables:
10 * <UL>
11 * <LI> DoubleDataBean: instance of DoubleDataBean so the graphics.graph is drawn
12 * with the proper data
13 * <LI> width: from DoubleDataBean, the width of the image
14 * <LI> height: from DoubleDataBean, the height of the image
15 * </UL>
16 *
17 * @author Allison McHenry
18 * @author Douglas Lyon, PhD
19 * @since JDK 1.3
20 */
21
22 public class DrawUtil {
23
24 //GLOBAL VARIABLES
25 private DoubleDataBean dd = null;
26 private int width = 0;
27 private int height = 0;
28
29
30 //CONSTRUCTOR
31 /**
32 * Default constructor, used to instantiate an instance of DrawUtil by
33 * the LineGraph, PieGraph and BarGraph classes
34 * @param dd Instance of DoubleDataBean containing data to be drawn
35 */
36 protected DrawUtil(DoubleDataBean _dd) {
37 dd = _dd;
38 width = dd.getWidth();
39 height = dd.getHeight();
40 }
41
42
43 /**
44 * Draws the title of the X axis for line graphics.graph and bar graphics.graph,
45 * in the center of the axis.
46 *
47 * @param g Graphics context for drawing
48 * @see LineGraph.#paint
49 * @see BarGraph.#paint
50 * @see DoubleDataBean.#getYAxisCoord
51 * @see java.awt.Graphics.#drawString
52 * @see java.awt.Graphics.#FontMetrics
53 * @see java.awt.Graphics.#setFont
54 */
55 protected void drawXAxisLabel(Graphics g) {
56 String xLabel = dd.getXLabel();
57 double yOrigin = dd.getYAxisCoord();
58
59 g.setColor(Color.black);
60 g.setFont(new Font("SansSerif", Font.PLAIN, 12));
61 FontMetrics fm = g.getFontMetrics();
62
63 int xStringStartX = ((width / 2) - (fm.stringWidth(xLabel) / 2));
64 int xStringStartY = (int) (yOrigin + (2 * dd.Y_OFFSET));
65 g.drawString(xLabel, xStringStartX, xStringStartY);
66 }
67
68 /**
69 * Draws the title of the Y axis for line graphics.graph and bar graphics.graph,
70 * in the upper left corner of the graphics.graph.
71 *
72 * @param g Graphics context for drawing
73 * @see LineGraph.#paint
74 * @see BarGraph.#paint
75 * @see DoubleDataBean.#getXAxisCoord
76 * @see java.awt.Graphics.#drawString
77 */
78
79 protected void drawYAxisLabel(Graphics g) {
80 String yLabel = dd.getYLabel();
81 double xAxisCoord = dd.getXAxisCoord();
82
83 g.setColor(Color.black);
84 g.setFont(new Font("SansSerif", Font.PLAIN, 12));
85 FontMetrics fm = g.getFontMetrics();
86 int yStringStartX = (int) (xAxisCoord + dd.X_OFFSET);
87 int yStringStartY = 4 * dd.Y_OFFSET;
88
89 if (xAxisCoord == (dd.getWidth() - dd.X_OFFSET)) {
90 yStringStartX = (int) (xAxisCoord - fm.stringWidth(yLabel));
91 yStringStartY = 4 * dd.Y_OFFSET;
92 }
93 g.drawString(yLabel, yStringStartX, yStringStartY);
94 }
95
96 /**
97 * Draws the title of the graphics.graph for line graphics.graph and bar graphics.graph,
98 * in the bottom center of the graphics.graph.
99 *
100 * @param g Graphics context for drawing
101 * @see LineGraph.#paint
102 * @see BarGraph.#paint
103 * @see java.awt.Graphics.#drawString
104 * @see java.awt.Graphics.#FontMetrics
105 * @see java.awt.Graphics.#setFont
106 */
107 protected void drawTitle(Graphics g) {
108 String title = dd.getTitle();
109
110 g.setColor(Color.yellow);
111 g.setFont(new Font("SansSerif", Font.BOLD, 16));
112 FontMetrics fontM = g.getFontMetrics();
113
114 int titleStringStartX = ((width / 2) - (fontM.stringWidth(title) / 2));
115 int titleStringStartY = height - dd.Y_OFFSET;
116 g.drawString(title, titleStringStartX, titleStringStartY);
117 }
118
119 /**
120 * Draws the title of the graphics.graph for line graphics.graph and bar graphics.graph,
121 * at the location specified as (x,y)
122 *
123 * @param g Graphics context for drawing
124 * @param xCoord x coordinate for where the string should be drawn
125 * @param yCoord y coordinate for where the string should be drawn
126 * @see PieGraph.#paint
127 * @see java.awt.Graphics.#drawString
128 * @see java.awt.Graphics.#FontMetrics
129 * @see java.awt.Graphics.#setFont
130 */
131 protected void drawTitle(Graphics g, int xCoord, int yCoord) {
132 String title = dd.getTitle();
133
134 g.setColor(Color.yellow);
135 g.setFont(new Font("SansSerif", Font.BOLD, 16));
136 FontMetrics fontM = g.getFontMetrics();
137
138 int titleStringStartX = xCoord;
139 int titleStringStartY = yCoord;
140 g.drawString(title, titleStringStartX, titleStringStartY);
141 }
142 }