001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui.components;
042:
043: import java.awt.Color;
044: import java.awt.Dimension;
045: import java.awt.Graphics;
046: import java.awt.Image;
047: import java.awt.MediaTracker;
048: import java.awt.image.PixelGrabber;
049: import javax.swing.JPanel;
050: import javax.swing.SwingConstants;
051:
052: /**
053: *
054: * @author Jiri Sedlacek
055: */
056: public class ImagePanel extends JPanel {
057: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
058:
059: private static MediaTracker mTracker = new MediaTracker(
060: new JPanel());
061:
062: //~ Instance fields ----------------------------------------------------------------------------------------------------------
063:
064: private Image image;
065: private int imageAlign; // Use SwingConstants.TOP, BOTTOM (LEFT & RIGHT not implemented)
066:
067: //~ Constructors -------------------------------------------------------------------------------------------------------------
068:
069: public ImagePanel(Image image) {
070: this (image, SwingConstants.TOP);
071: }
072:
073: public ImagePanel(Image image, int imageAlign) {
074: setImage(image);
075: setImageAlign(imageAlign);
076: }
077:
078: //~ Methods ------------------------------------------------------------------------------------------------------------------
079:
080: public void setImage(Image image) {
081: this .image = loadImage(image);
082:
083: if (this .image == null) {
084: throw new RuntimeException("Failed to load image"); // NOI18N
085: }
086:
087: setPreferredBackground();
088: setPreferredSize(new Dimension(this .image.getWidth(null),
089: this .image.getHeight(null)));
090:
091: refresh();
092: }
093:
094: public void setImageAlign(int imageAlign) {
095: this .imageAlign = imageAlign;
096:
097: setPreferredBackground();
098:
099: refresh();
100: }
101:
102: protected static Image loadImage(Image image) {
103: mTracker.addImage(image, 0);
104:
105: try {
106: mTracker.waitForID(0);
107: } catch (InterruptedException e) {
108: return null;
109: }
110:
111: mTracker.removeImage(image, 0);
112:
113: return image;
114: }
115:
116: protected void setPreferredBackground() {
117: int[] pixels = new int[1];
118:
119: PixelGrabber pg = null;
120:
121: switch (imageAlign) {
122: case (SwingConstants.TOP):
123: pg = new PixelGrabber(image, 0, image.getHeight(null) - 1,
124: 1, 1, pixels, 0, 1);
125:
126: break;
127: case (SwingConstants.BOTTOM):
128: pg = new PixelGrabber(image, 0, 0, 1, 1, pixels, 0, 1);
129:
130: break;
131: default:
132: pg = new PixelGrabber(image, 0, image.getHeight(null) - 1,
133: 1, 1, pixels, 0, 1);
134: }
135:
136: try {
137: if ((pg != null) && pg.grabPixels()) {
138: setBackground(new Color(pixels[0]));
139: }
140: } catch (InterruptedException e) {
141: }
142: }
143:
144: protected void paintComponent(Graphics graphics) {
145: graphics.setColor(getBackground());
146: graphics.fillRect(0, 0, getWidth(), getHeight());
147:
148: switch (imageAlign) {
149: case (SwingConstants.TOP):
150: graphics.drawImage(image, (getWidth() - image
151: .getWidth(null)) / 2, 0, this );
152:
153: break;
154: case (SwingConstants.BOTTOM):
155: graphics.drawImage(image, (getWidth() - image
156: .getWidth(null)) / 2, getHeight()
157: - image.getHeight(null), this );
158:
159: break;
160: default:
161: graphics.drawImage(image, (getWidth() - image
162: .getWidth(null)) / 2, 0, this );
163: }
164: }
165:
166: private void refresh() {
167: if (isShowing()) {
168: invalidate();
169: repaint();
170: }
171: }
172: }
|