001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.util;
021:
022: import java.awt.*;
023: import java.util.Vector;
024:
025: /**
026: * This utility class is used to create images that can be streamed back to the browser via the GifEncoder class.
027: */
028: public class ImageFactory {
029: static boolean _showFrame = false;
030: static Frame _frame;
031:
032: static {
033: _frame = new Frame();
034: _frame.addNotify();
035: _frame.setBounds(0, 0, 200, 200);
036: _frame.setVisible(_showFrame);
037: }
038:
039: /**
040: * Create a new image factory
041: */
042: public ImageFactory() {
043: super ();
044: //_frame = new Frame();
045: //_frame.addNotify();
046: //_frame.setBounds(0,0,200,200);
047: //_frame.setVisible(true);
048: }
049:
050: /**
051: * This method creates an oval shaped button.
052: * @return java.awt.Image The button image
053: * @param width int The width of the button
054: * @param height int The height of the button
055: * @param f java.awt.Font The font to use to draw the text
056: * @param text java.lang.String The font to use to draw the text
057: * @param textColor java.awt.Color The color to draw the text in
058: * @param backColor java.awt.Color The color for the button background
059: * @param topColor java.awt.Color The color for the top and left border
060: * @param bottomColor java.awt.Color The color for the bottom and right border
061: * @param transparentColor java.awt.Color The color for the areas on the button that need to be transparent
062: * @param clicked boolean True if you want the button to appear to be clicked
063: * @param enabled boolean True if you want the button to appear to be enabled
064: */
065: public synchronized Image createOvalButton(int width, int height,
066: Font f, String text, Color textColor, Color backColor,
067: Color topColor, Color bottomColor, Color transparentColor,
068: boolean clicked, boolean enabled) {
069: Image img = _frame.createImage(width, height);
070: Graphics g = img.getGraphics();
071:
072: FontMetrics fm = g.getFontMetrics(f);
073: Vector lines = wordWrap(fm, width, text);
074:
075: int fh = (fm.getHeight() + 1) * lines.size();
076: int textY = (((height - fh) / 2) + fm.getHeight()) - 4;
077: if (clicked)
078: textY += 1;
079:
080: width--;
081: height--;
082:
083: g.setFont(f);
084:
085: g.setColor(transparentColor);
086: g.fillRect(0, 0, width + 1, height + 1);
087:
088: if (clicked)
089: g.setColor(bottomColor);
090: else
091: g.setColor(topColor);
092: g.fillOval(0, 0, width - 3, height - 3);
093:
094: if (clicked)
095: g.setColor(topColor);
096: else
097: g.setColor(bottomColor);
098: g.fillOval(3, 3, width - 3, height - 3);
099:
100: g.setColor(backColor);
101: g.fillOval(1, 1, width - 3, height - 3);
102:
103: if (enabled)
104: g.setColor(textColor);
105: for (int i = 0; i < lines.size(); i++) {
106: String line = (String) lines.elementAt(i);
107: int textX = ((width - fm.stringWidth(line)) / 2)
108: + (clicked ? 1 : 0);
109: if (enabled)
110: g.drawString(line, textX, textY);
111: else {
112: g.setColor(topColor);
113: g.drawString(line, textX + 1, textY + 1);
114: g.setColor(bottomColor);
115: g.drawString(line, textX, textY);
116: }
117: textY += fm.getHeight() + 1;
118: }
119:
120: if (_showFrame)
121: _frame.getGraphics().drawImage(img, 0, height, _frame);
122:
123: return img;
124: }
125:
126: /**
127: * This method creates a rectangular shaped button where the width is based on the size of the text.
128: * @return java.awt.Image The button image
129: * @param height int The height of the button
130: * @param f java.awt.Font The font to use to draw the text
131: * @param text java.lang.String The font to use to draw the text
132: * @param textColor java.awt.Color The color to draw the text in
133: * @param backColor java.awt.Color The color for the button background
134: * @param topColor java.awt.Color The color for the top and left border
135: * @param bottomColor java.awt.Color The color for the bottom and right border
136: * @param transparentColor java.awt.Color The color to use for transparent areas on the button
137: * @param clicked boolean True if you want the button to appear to be clicked
138: * @param enabled boolean True if you want the button to appear to be enabled
139: */
140: public Image createOvalButton(int height, Font f, String text,
141: Color textColor, Color backColor, Color topColor,
142: Color bottomColor, Color transparentColor, boolean clicked,
143: boolean enabled) {
144: Graphics g = _frame.getGraphics();
145: FontMetrics fm = g.getFontMetrics(f);
146: int width = fm.stringWidth(text) + 14;
147: return createOvalButton(width, height, f, text, textColor,
148: backColor, topColor, bottomColor, transparentColor,
149: clicked, enabled);
150: }
151:
152: /**
153: * This method creates a rectangular shaped button.
154: * @return java.awt.Image The button image
155: * @param width int The width of the button
156: * @param height int The height of the button
157: * @param f java.awt.Font The font to use to draw the text
158: * @param text java.lang.String The font to use to draw the text
159: * @param textColor java.awt.Color The color to draw the text in
160: * @param backColor java.awt.Color The color for the button background
161: * @param topColor java.awt.Color The color for the top and left border
162: * @param bottomColor java.awt.Color The color for the bottom and right border
163: * @param clicked boolean True if you want the button to appear to be clicked
164: * @param enabled boolean True if you want the button to appear to be enabled
165: */
166: public synchronized Image createRectangleButton(int width,
167: int height, Font f, String text, Color textColor,
168: Color backColor, Color topColor, Color bottomColor,
169: boolean clicked, boolean enabled) {
170: Image img = _frame.createImage(width, height);
171: Graphics g = img.getGraphics();
172:
173: FontMetrics fm = g.getFontMetrics(f);
174: Vector lines = wordWrap(fm, width, text);
175:
176: int fh = (fm.getHeight() + 1) * lines.size();
177: int textY = (((height - fh) / 2) + fm.getHeight()) - 2;
178:
179: if (clicked)
180: textY += 1;
181:
182: width--;
183: height--;
184:
185: g.setColor(backColor);
186: g.fillRect(0, 0, width, height);
187:
188: g.setFont(f);
189:
190: if (enabled)
191: g.setColor(textColor);
192: for (int i = 0; i < lines.size(); i++) {
193: String line = (String) lines.elementAt(i);
194: int textX = ((width - fm.stringWidth(line)) / 2)
195: + (clicked ? 1 : 0);
196: if (enabled)
197: g.drawString(line, textX, textY);
198: else {
199: g.setColor(topColor);
200: g.drawString(line, textX + 1, textY + 1);
201: g.setColor(bottomColor);
202: g.drawString(line, textX, textY);
203: }
204:
205: textY += fm.getHeight() + 1;
206: }
207: if (clicked)
208: g.setColor(bottomColor);
209: else
210: g.setColor(topColor);
211:
212: g.drawLine(0, 0, width, 0);
213: g.drawLine(0, 0, 0, height);
214:
215: if (clicked)
216: g.setColor(topColor);
217: else
218: g.setColor(bottomColor);
219:
220: g.drawLine(width, 0, width, height);
221: g.drawLine(1, height, width, height);
222:
223: if (_showFrame)
224: _frame.getGraphics().drawImage(img, 0, height, _frame);
225: return img;
226: }
227:
228: /**
229: * This method creates a rectangular shaped button where the width is based on the size of the text.
230: * @return java.awt.Image The button image
231: * @param height int The height of the button
232: * @param f java.awt.Font The font to use to draw the text
233: * @param text java.lang.String The font to use to draw the text
234: * @param textColor java.awt.Color The color to draw the text in
235: * @param backColor java.awt.Color The color for the button background
236: * @param topColor java.awt.Color The color for the top and left border
237: * @param bottomColor java.awt.Color The color for the bottom and right border
238: * @param clicked boolean True if you want the button to appear to be clicked
239: * @param enabled boolean True if you want the button to appear to be enabled
240: */
241: public Image createRectangleButton(int height, Font f, String text,
242: Color textColor, Color backColor, Color topColor,
243: Color bottomColor, boolean clicked, boolean enabled) {
244: Graphics g = _frame.getGraphics();
245: FontMetrics fm = g.getFontMetrics(f);
246: int width = fm.stringWidth(text) + 10;
247: return createRectangleButton(width, height, f, text, textColor,
248: backColor, topColor, bottomColor, clicked, enabled);
249: }
250:
251: /**
252: * This method creates a tab image.
253: * @return java.awt.Image The tab image
254: * @param width int The width of the tab
255: * @param height int The height of the tab
256: * @param f java.awt.Font The font to use to draw the text
257: * @param text java.lang.String The font to use to draw the text
258: * @param textColor java.awt.Color The color to draw the text in
259: * @param backColor java.awt.Color The color for the button background
260: * @param borderColor1 java.awt.Color The color for the top and left border
261: * @param borderColor2 java.awt.Color The color for the right border
262: * @param selected boolean True if you want the button to appear to be selected (no underline)
263: */
264: public Image createTab(int width, int height, Font f, String text,
265: Color textColor, Color backColor, Color borderColor1,
266: Color borderColor2, Color transColor, boolean selected) {
267: Image img = _frame.createImage(width, height);
268: Graphics g = img.getGraphics();
269:
270: FontMetrics fm = g.getFontMetrics(f);
271: Vector lines = wordWrap(fm, width, text);
272:
273: int fh = (fm.getHeight() + 1) * lines.size();
274: int textY = (((height - fh) / 2) + fm.getHeight()) - 2;
275:
276: g.setColor(backColor);
277: g.fillRect(0, 0, width, height);
278:
279: int tabCornerSize = 6;
280:
281: g.setColor(transColor);
282: g.fillRect(0, 0, tabCornerSize, tabCornerSize);
283: g.fillRect(width - tabCornerSize, 0, tabCornerSize,
284: tabCornerSize);
285:
286: g.setColor(backColor);
287: g.fillArc(0, 0, tabCornerSize * 2, tabCornerSize * 2, 90, 135);
288:
289: Polygon p = new Polygon();
290: p.addPoint(width, tabCornerSize);
291: p.addPoint(width - tabCornerSize, 0);
292: p.addPoint(width - tabCornerSize, tabCornerSize);
293: g.fillPolygon(p);
294:
295: g.setFont(f);
296: g.setColor(textColor);
297:
298: for (int i = 0; i < lines.size(); i++) {
299: String line = (String) lines.elementAt(i);
300: int textX = ((width - fm.stringWidth(line)) / 2);
301: g.drawString(line, textX, textY);
302: textY += fm.getHeight() + 1;
303: }
304:
305: g.setColor(borderColor2);
306: g.drawLine(width - tabCornerSize, 0, width, tabCornerSize);
307: g.drawLine(width - 1, height - 2, width - 1, tabCornerSize);
308:
309: g.setColor(borderColor1);
310: g.drawLine(0, tabCornerSize, 0, height);
311: g.drawArc(0, 0, tabCornerSize * 2, tabCornerSize * 2, 90, 115);
312: g.drawLine(tabCornerSize, 0, width - tabCornerSize, 0);
313: if (!selected) {
314: g.drawLine(0, height - 1, width - 1, height - 1);
315: g.setColor(borderColor2);
316: g.drawLine(0, height - 2, width - 2, height - 2);
317:
318: }
319: if (_showFrame)
320: _frame.getGraphics().drawImage(img, 0, height, _frame);
321: return img;
322: }
323:
324: /**
325: * This method creates a tab image. The width of the tab is determined by the text width.
326: * @return java.awt.Image The tab image
327: * @param width int The width of the tab
328: * @param height int The height of the tab
329: * @param f java.awt.Font The font to use to draw the text
330: * @param text java.lang.String The font to use to draw the text
331: * @param textColor java.awt.Color The color to draw the text in
332: * @param backColor java.awt.Color The color for the button background
333: * @param borderColor1 java.awt.Color The color for the top and left border
334: * @param borderColor2 java.awt.Color The color for the right border
335: * @param selected boolean True if you want the button to appear to be selected (no underline)
336: */
337: public Image createTab(int height, Font f, String text,
338: Color textColor, Color backColor, Color borderColor1,
339: Color borderColor2, Color transColor, boolean selected) {
340: Graphics g = _frame.getGraphics();
341: FontMetrics fm = g.getFontMetrics(f);
342: int width = fm.stringWidth(text) + 10;
343: return createTab(width, height, f, text, textColor, backColor,
344: borderColor1, borderColor2, transColor, selected);
345:
346: }
347:
348: protected void finalize() {
349: //_frame.dispose();
350: }
351:
352: private Vector wordWrap(FontMetrics fm, int width, String text) {
353: String replaceS = "\uFFFD";
354: char replaceC = replaceS.charAt(0);
355: text += replaceC;
356:
357: int lastSpace = -1, currentPos = 0, currentWidth = 0, startPos = 0, endPos = 0;
358: int availWidth = width - 6;
359: int lastPos = text.length();
360: char lastChar = ' ';
361: String textLine;
362: Vector multiLineText = new Vector();
363:
364: for (currentPos = 0; currentPos < lastPos; currentPos++) {
365: lastChar = text.charAt(currentPos);
366: if (lastChar != replaceC)
367: currentWidth += fm.charWidth(lastChar);
368: if (currentWidth >= availWidth) {
369: if (lastSpace == -1)
370: endPos = currentPos - 2;
371: else
372: endPos = lastSpace + 1;
373: textLine = text.substring(startPos, endPos);
374: startPos = endPos;
375: currentPos = startPos - 1;
376: multiLineText.addElement(textLine.trim());
377:
378: lastSpace = -1;
379: currentWidth = 0;
380: } else {
381: if (lastChar == ' ')
382: lastSpace = currentPos;
383: if (lastChar == replaceC) {
384: endPos = currentPos;
385: textLine = text.substring(startPos, endPos);
386: startPos = endPos;
387: currentPos = endPos;
388: multiLineText.addElement(textLine.trim());
389: lastSpace = -1;
390: currentWidth = 0;
391: }
392: }
393: }
394:
395: return multiLineText;
396: }
397:
398: /**
399: * This method creates a 1 pixel image.
400: * @return java.awt.Image The button image
401: * @param color java.awt.Color The color to draw the text in
402: */
403: public synchronized Image createOnePixelImage(Color color) {
404: Image img = _frame.createImage(1, 1);
405: Graphics g = img.getGraphics();
406:
407: g.setColor(color);
408: g.fillRect(0, 0, 1, 1);
409:
410: return img;
411: }
412: }
|