001: package com.technoetic.xplanner.tags;
002:
003: import com.sun.image.codec.jpeg.JPEGCodec;
004: import com.sun.image.codec.jpeg.JPEGEncodeParam;
005: import com.sun.image.codec.jpeg.JPEGImageEncoder;
006: import com.technoetic.xplanner.format.DecimalFormat;
007:
008: import javax.swing.*;
009: import java.awt.*;
010: import java.awt.font.TextLayout;
011: import java.awt.image.BufferedImage;
012: import java.io.FileOutputStream;
013: import java.io.IOException;
014: import java.io.OutputStream;
015: import java.util.Locale;
016:
017: public class ProgressBarImage extends BufferedImage {
018: private double value;
019: private double maxValue;
020: private Color valueColor = Color.BLUE;
021: private Color maxValueColor = Color.RED;
022: private DecimalFormat format;
023: private double captionValue;
024:
025: public static void main(String[] args) throws IOException {
026: JFrame jFrame = new JFrame("Test");
027: jFrame.setSize(800, 700);
028: ProgressBarImage image = new ProgressBarImage(100, 20, 12.9,
029: Color.BLUE, 16.8, Color.RED, new DecimalFormat(Locale
030: .getDefault(), ""));
031: FileOutputStream fos = new FileOutputStream(
032: "c:\\temp\\pic.jpeg");
033: // BufferedOutputStream bos = new BufferedOutputStream( fos );
034: image.encodeJPEG(fos, (float) 1.0);
035: fos.flush();
036: fos.close();
037:
038: jFrame.getContentPane().add(
039: new JLabel(new ImageIcon("c:\\temp\\pic.jpeg")));
040: jFrame.pack();
041: jFrame.show();
042: }
043:
044: public static class Model {
045: public int width;
046: public int height;
047: public double value;
048: public Color valueColor;
049: public double maxValue;
050: public Color maxValueColor;
051: public DecimalFormat format;
052: private double captionValue;
053:
054: public Model(int width, int height, double value,
055: Color valueColor, double maxValue, Color maxValueColor,
056: DecimalFormat format, double captionValue) {
057: this .width = width;
058: this .height = height;
059: this .value = value;
060: this .valueColor = valueColor;
061: this .maxValue = maxValue;
062: this .maxValueColor = maxValueColor;
063: this .format = format;
064: this .captionValue = captionValue;
065: }
066: }
067:
068: public ProgressBarImage(int width, int height, double value,
069: Color valueColor, double maxValue, Color maxValueColor,
070: DecimalFormat format) {
071: this (new Model(width, height, value, valueColor, maxValue,
072: maxValueColor, format, value));
073: }
074:
075: public ProgressBarImage(Model model) {
076: super (model.width, model.height /*model.height*/,
077: BufferedImage.TYPE_INT_RGB);
078: this .value = model.value;
079: this .valueColor = model.valueColor;
080: this .maxValue = model.maxValue;
081: this .maxValueColor = model.maxValueColor;
082: this .format = model.format;
083: this .captionValue = model.captionValue;
084: init();
085: }
086:
087: private void init() {
088: Graphics2D drawGraphics = createGraphics();
089: Graphics2D textGraphics = getTextGraphics();
090:
091: int rightMarginInPixels = 0; //getValueTextWidth(textGraphics, maxValue);
092: int valueInPixels = getPixelsForValue();
093:
094: int textHeight = textGraphics.getFontMetrics().getHeight() - 2;
095:
096: drawRectangle(drawGraphics, 0, 0, getWidth(), getHeight(),
097: Color.WHITE);
098: drawBar(drawGraphics, 0, valueInPixels, getHeight(), valueColor);
099: if (valueInPixels < getWidth())
100: drawBar(drawGraphics, valueInPixels, getWidth()
101: - valueInPixels - rightMarginInPixels, getHeight(),
102: maxValueColor);
103:
104: drawValue(textGraphics, valueInPixels, getHeight() - 2);
105: // if (valueInPixels < getWidth())
106: // drawValue(textGraphics, getWidth()-rightMarginInPixels, getHeight() - 2, maxValue, Color.BLACK);
107: }
108:
109: private void drawValue(Graphics2D g, int x, int y) {
110: int textWidth = getValueTextWidth(g);
111: int xText = x;
112: Color bgColor = maxValueColor;
113: if (textWidth < x) {
114: xText = x - textWidth;
115: bgColor = valueColor;
116: }
117: // g.drawString(text, xText, y);
118: drawValue(g, xText, y, getTextColor(bgColor));
119: }
120:
121: private void drawValue(Graphics2D g, int x, int y, Color color) {
122: g.setColor(color);
123: Font oldFont = g.getFont();
124: // if (!color.equals(Color.BLACK)) {
125: // g.setFont(g.getFont().deriveFont(Font.BOLD));
126: // }
127: TextLayout tl = new TextLayout(getCaption(), g.getFont(), g
128: .getFontRenderContext());
129: tl.draw(g, x, y);
130: g.setFont(oldFont);
131: }
132:
133: private String getCaption() {
134: return format.format(captionValue);
135: }
136:
137: private int getValueTextWidth(Graphics2D g) {
138: return SwingUtilities.computeStringWidth(g.getFontMetrics(),
139: getCaption());
140: }
141:
142: private Graphics2D getTextGraphics() {
143: Graphics2D g = createGraphics();
144: g.setFont(new Font("SansSerif", Font.BOLD, getFontSize()));
145: // g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
146: g.setPaintMode();
147: // g.setXORMode(Color.BLACK);
148: return g;
149: }
150:
151: private int getFontSize() {
152: return (int) Math.round(getHeight() * 0.91);
153: }
154:
155: private Color getTextColor(Color bgColor) {
156: Color color = null;
157: if (bgColor.equals(Color.RED) || bgColor.equals(Color.BLUE))
158: color = Color.WHITE;
159: else
160: color = Color.BLACK;
161: return color;
162: }
163:
164: private void drawBar(Graphics2D g, int x, int width, int height,
165: Color color) {
166: drawRectangle(g, x, 0, width, height, color);
167: }
168:
169: private void drawRectangle(Graphics2D g, int x, int y, int width,
170: int height, Color color) {
171: g.setColor(color);
172: g.fillRect(x, y, width, height);
173: }
174:
175: private int getPixelsForValue() {
176: return (int) Math.round((getWidth() / maxValue) * value);
177: }
178:
179: public void encodeJPEG(OutputStream outputStream,
180: float outputQuality) throws java.io.IOException {
181: int outputWidth = this .getWidth(null);
182: if (outputWidth < 1)
183: throw new IllegalArgumentException("output image width "
184: + outputWidth + " is out of range");
185: int outputHeight = this .getHeight(null);
186: if (outputHeight < 1)
187: throw new IllegalArgumentException("output image height "
188: + outputHeight + " is out of range");
189:
190: // Get a buffered image from the image.
191: BufferedImage bi = new BufferedImage(outputWidth, outputHeight,
192: BufferedImage.TYPE_INT_RGB);
193: Graphics2D biContext = bi.createGraphics();
194: biContext.drawImage(this , 0, 0, null);
195: // Note that additional drawing such as watermarks or logos can be placed here.
196:
197: // com.sun.image.codec.jpeg package is included in sun and ibm sdk 1.3
198: JPEGImageEncoder encoder = JPEGCodec
199: .createJPEGEncoder(outputStream);
200: // The default quality is 0.75.
201: JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(bi);
202: jep.setQuality(outputQuality, true);
203: encoder.encode(bi, jep);
204: // encoder.encode( bi );
205: outputStream.flush();
206: } // encodeImage
207:
208: }
|