001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.photoalbum;
033:
034: import java.util.Vector;
035:
036: import javax.microedition.lcdui.*;
037: import javax.microedition.midlet.*;
038:
039: /**
040: * A quick sample of graphics which generates a series of images
041: * that can be used as a sample animation. The animation
042: * consists of a title, a pie, and a bar chart. The charts
043: * are sized to the requested size so they will look good on
044: * devices with various screen sizes.
045: */
046: class TestChart {
047: /** Width of the canvas */
048: int w;
049:
050: /** Height of the canvas */
051: int h;
052:
053: /** Font used for drawing text */
054: Font font;
055:
056: /** height of the font */
057: int fh;
058:
059: /** Height of the title */
060: int titleHeight;
061:
062: /** Padding used between items */
063: int pad;
064:
065: /** Size of the Pie chart used for width and height */
066: int pieSize;
067:
068: /** Size of the Bar chart used for width and height */
069: int barSize;
070:
071: /** The current frame number */
072: int frameno;
073:
074: /**
075: * Initialize a new TestPattern to match the requested size.
076: * @param width the requested width of the Images
077: * @param height the requested height of the Images
078: */
079: public TestChart(int width, int height) {
080: w = width;
081: h = height;
082: font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
083: Font.SIZE_SMALL);
084: fh = font.getHeight();
085:
086: /*
087: * Compute the sizes of the bar and pie charts
088: * It should use all the space except for the title and event regions
089: * Don't let the charts get too small
090: */
091: pad = 2;
092: titleHeight = fh + (pad * 2);
093: barSize = h - (titleHeight + pad);
094:
095: if (barSize < 10) { // Don't let them get too small
096: barSize = 10;
097: }
098:
099: if (barSize > ((w - pad) / 2)) { // Shrink to 1/2 width
100: barSize = (w - pad) / 2;
101: }
102:
103: pieSize = barSize;
104: }
105:
106: /**
107: * Generate the frames for this animated chart.
108: * It consists of hour frames with the first frame
109: * containing only the base, and frame 2,3,4 adding
110: * the colors to the bars of the bar chart.
111: * @return the Vector of Images.
112: */
113: public Vector generateImages() {
114: Vector v = new Vector(4);
115:
116: for (frameno = 0; frameno < 4; frameno++) {
117: Image image = Image.createImage(w, h);
118: paint(image.getGraphics());
119: v.addElement(image);
120: }
121:
122: return v;
123: }
124:
125: /**
126: * Draw the current frame.
127: * The field frameno contains the current frame number.
128: * @param g the Graphics context
129: */
130: public void paint(Graphics g) {
131: g.setFont(font);
132: g.setGrayScale(255);
133: g.fillRect(0, 0, w, h);
134:
135: // Draw Fill and outline for background of title Text
136: int swidth = (pad * 2) + font.stringWidth("Chart Samples");
137: int title_x = (w - swidth) / 2;
138:
139: g.setGrayScale(128);
140: g.fillRoundRect(title_x, 0, swidth, fh, 5, 5);
141: g.setGrayScale(0);
142: g.drawRoundRect(title_x, 0, swidth, fh, 5, 5);
143:
144: // Sample Text
145: g.setColor(0, 0, 0);
146: g.drawString("Chart Samples", title_x + pad, pad, Graphics.TOP
147: | Graphics.LEFT);
148:
149: g.translate(0, titleHeight + pad); // Translate to below title text
150:
151: // Draw pie chart on the left using the barSize for width and height
152: g.setColor(255, 0, 0);
153: g.fillArc(0, 0, pieSize, pieSize, 45, 270);
154: g.setColor(0, 255, 0);
155: g.fillArc(0, 0, pieSize, pieSize, 0, 45);
156: g.setColor(0, 0, 255);
157: g.fillArc(0, 0, pieSize, pieSize, 0, -45);
158: g.setColor(0);
159: g.drawArc(0, 0, pieSize, pieSize, 0, 360);
160:
161: // Draw Bar chart on right side of the display
162: // scale the values to the pieSize maximum value
163: int yorig = barSize;
164: int h1 = barSize / 3;
165: int h2 = barSize / 2;
166: int h3 = barSize;
167: int avg = (h1 + h2 + h3) / 3;
168:
169: // Move over to draw Bar chart
170: g.translate((w + pad) / 2, 0);
171:
172: int bw = pieSize / 7;
173:
174: if (bw < 2) {
175: bw = 2;
176: }
177:
178: if (frameno > 0) {
179: g.setColor(255, 0, 0);
180: g.fillRect(bw * 1, yorig - h1, bw + 1, h1);
181: }
182:
183: if (frameno > 1) {
184: g.setColor(0, 255, 0);
185: g.fillRect(bw * 3, yorig - h2, bw + 1, h2);
186: }
187:
188: if (frameno > 2) {
189: g.setColor(0, 0, 255);
190: g.fillRect(bw * 5, yorig - h3, bw + 1, h3);
191: }
192:
193: g.setColor(0);
194: g.drawRect(bw * 1, yorig - h1, bw, h1);
195: g.drawRect(bw * 3, yorig - h2, bw, h2);
196: g.drawRect(bw * 5, yorig - h3, bw, h3);
197:
198: // Draw axis for bar chart.
199: g.setGrayScale(0);
200: g.drawLine(0, 0, 0, yorig);
201: g.drawLine(0, yorig, barSize, yorig);
202: g.setStrokeStyle(Graphics.DOTTED);
203: g.drawLine(0, yorig - avg, barSize, yorig - avg);
204: g.setStrokeStyle(Graphics.SOLID);
205:
206: // Restore to left and move down
207: g.translate(-(w + pad) / 2, pieSize + pad);
208: }
209: }
|