001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.AlphaComposite;
024: import java.awt.BorderLayout;
025: import java.awt.Color;
026: import java.awt.Component;
027: import java.awt.Composite;
028: import java.awt.Graphics;
029: import java.awt.Graphics2D;
030: import java.awt.GraphicsConfiguration;
031: import java.awt.Image;
032: import java.awt.Insets;
033: import java.awt.RenderingHints;
034: import java.awt.Transparency;
035: import java.awt.Window;
036: import java.awt.font.FontRenderContext;
037: import java.awt.image.BufferedImage;
038: import java.awt.image.ImageObserver;
039:
040: /**
041: * This class overrides <code>WingTooltit</code> to provide
042: * Java version dependent methods for Java >= 1.2
043: * <br>
044: * <br>
045: * <b>This class is thread safe.</b>
046: **/
047: public class WingToolkit12 extends WingToolkit {
048:
049: public Color createColor(int argb) {
050: return new Color(argb ^ 0xff000000, true);
051: }
052:
053: public void setTextAntialias(Graphics g, boolean antialias) {
054: ((Graphics2D) g).setRenderingHint(
055: RenderingHints.KEY_TEXT_ANTIALIASING,
056: antialias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
057: : RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
058: }
059:
060: protected Object mulComposite(Graphics g, int alpha) {
061: if (alpha <= 0 || alpha >= 256)
062: return null;
063:
064: Graphics2D g2 = (Graphics2D) g;
065: Composite c = g2.getComposite();
066: float a = (255 - ((float) alpha)) / ((float) 255);
067: if (c instanceof AlphaComposite) {
068: a = a * ((AlphaComposite) c).getAlpha();
069: }
070: g2.setComposite(AlphaComposite.getInstance(
071: AlphaComposite.SRC_OVER, a));
072: return c;
073: }
074:
075: protected void clrComposite(Graphics g, Object composite) {
076: if (composite != null)
077: ((Graphics2D) g).setComposite((Composite) composite);
078: }
079:
080: public boolean drawSmoothImage(Graphics g, WingImage img, int x,
081: int y, int w, int h, ImageObserver o) {
082: Image stretchBuffer = img.stretchBuffer;
083: if ((img.src.width == w && img.src.height == h)) {
084: return img.drawImage(g, x, y, w, h, o);
085: } else if (stretchBuffer != null
086: && stretchBuffer.getWidth(null) == w
087: && stretchBuffer.getHeight(null) == h) {
088: return g.drawImage(stretchBuffer, x, y, o);
089: } else {
090: //TODO check performance
091: // Graphics2D g2= (Graphics2D)g;
092: // Object oldrq= g2.getRenderingHint(RenderingHints.KEY_RENDERING);
093: // g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
094: // boolean r= img.drawImage(g2,x,y,w,h,o);
095: // g2.setRenderingHint(RenderingHints.KEY_RENDERING, oldrq);
096: // return r;
097:
098: //TODO check performance
099: if (stretchBuffer != null) {
100: ((BufferedImage) stretchBuffer).flush();
101: }
102: GraphicsConfiguration gc = ((Graphics2D) g)
103: .getDeviceConfiguration();
104: BufferedImage buf = gc.createCompatibleImage(w, h,
105: Transparency.TRANSLUCENT);
106: if (!buf.getColorModel().hasAlpha()) {
107: buf = new BufferedImage(w, h,
108: BufferedImage.TYPE_INT_ARGB);
109: }
110: img.stretchBuffer = buf;
111: if (buf == null) {
112: return img.drawImage(g, x, y, w, h, o);
113: }
114: Graphics2D g2 = buf.createGraphics();
115: g2.setRenderingHint(RenderingHints.KEY_RENDERING,
116: RenderingHints.VALUE_RENDER_QUALITY);
117: boolean r = img.drawImage(g2, 0, 0, w, h, o);
118: g2.dispose();
119: if (r) {
120: r &= g.drawImage(buf, x, y, o);
121: }
122: return r;
123: }
124: }
125:
126: protected Window createPopup(WingComponent origin,
127: WingComponent content, boolean focusableWindow) {
128: Component c = origin;
129: for (; c != null; c = c.getParent()) {
130: if (c instanceof Window)
131: break;
132: }
133: if (c == null)
134: return null;
135: final WingRootPane root = new WingRootPane();
136: Window w = new Window((Window) c) {
137: public void update(Graphics g) {
138: paint(g);
139: }
140:
141: public void paint(Graphics g) {
142: Insets insets = getInsets();
143: g.translate(insets.left, insets.top);
144: root.paint(g);
145: }
146: };
147: w.add(root, BorderLayout.CENTER);
148: root.setStyleId("popup");
149: root.setContentPane(content);
150: return w;
151: }
152:
153: public int stringWidth(WingFont font, String s) {
154: return (int) font.font.getStringBounds(s,
155: new FontRenderContext(null, font.antialias, false))
156: .getWidth();
157: }
158:
159: public int charsWidth(WingFont font, char[] data, int offset,
160: int length) {
161: return (int) font.font.getStringBounds(data, offset,
162: offset + length,
163: new FontRenderContext(null, font.antialias, false))
164: .getWidth();
165: }
166:
167: }
|