001: /*
002: * @(#)ImageDemo.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis.demos;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import java.awt.image.*;
031: import java.net.URL;
032: import basis.Builder;
033:
034: public class ImageDemo extends Demo implements MouseMotionListener,
035: MouseListener {
036: private Image gif;
037: private Image jpg;
038: private Image png;
039: private Image logo;
040: private BufferedImage bi;
041: private int[] original;
042: private boolean drag;
043: private int xorX;
044: private int xorY;
045:
046: public ImageDemo() {
047: gif = loadImage(this , "images/duke.gif");
048: jpg = loadImage(this , "images/duke.jpg");
049: png = loadImage(this , "images/duke.png");
050: logo = loadImage(this , "images/logo.gif");
051: GraphicsEnvironment ge = GraphicsEnvironment
052: .getLocalGraphicsEnvironment();
053: GraphicsDevice gd = ge.getDefaultScreenDevice();
054: GraphicsConfiguration gc = gd.getDefaultConfiguration();
055: int w = gif.getWidth(this );
056: int h = gif.getHeight(this );
057: bi = gc.createCompatibleImage(w, h);
058: while (bi == null) {
059: try {
060: Thread.sleep(100);
061: } catch (InterruptedException ie) {
062: }
063: }
064: Graphics big = bi.getGraphics();
065: big.setColor(Builder.SUN_RED);
066: big.fillRect(0, 0, w, h);
067: big.drawImage(gif, 0, 0, w, h, 0, 0, w, h, this );
068: addMouseMotionListener(this );
069: addMouseListener(this );
070: }
071:
072: public void paint(Graphics g) {
073: Dimension d = getSize();
074: int w = d.width / 6;
075: int h = d.height / 2;
076: g.setColor(Color.black);
077: for (int i = 2; i < d.height - 3; i += 4) {
078: g.drawLine(0, i, d.width, i);
079: }
080: int x = 0;
081: int y = 0;
082: g.drawImage(gif, x, y, w, h, this );
083: x += w;
084: g.drawImage(jpg, x, y, w, h, this );
085: x += w;
086: g.drawImage(png, x, y, w, h, this );
087: x += w;
088: g.drawImage(gif, x, y, w, h, Builder.SUN_BLUE, this );
089: x += w;
090: g.drawImage(logo, x, y, w, h, Builder.SUN_YELLOW, this );
091: x += w;
092: g.drawImage(bi, x, y, w, h, this );
093: x = 0;
094: y += h;
095: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
096: AlphaComposite.SRC_OVER, 0.0f));
097: g.drawImage(gif, x, y, w, h, this );
098: x += w;
099: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
100: AlphaComposite.SRC_OVER, 0.2f));
101: g.drawImage(gif, x, y, w, h, this );
102: x += w;
103: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
104: AlphaComposite.SRC_OVER, 0.4f));
105: g.drawImage(gif, x, y, w, h, this );
106: x += w;
107: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
108: AlphaComposite.SRC_OVER, 0.6f));
109: g.drawImage(gif, x, y, w, h, this );
110: x += w;
111: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
112: AlphaComposite.SRC_OVER, 0.8f));
113: g.drawImage(gif, x, y, w, h, this );
114: x += w;
115: ((Graphics2D) g).setComposite(AlphaComposite.getInstance(
116: AlphaComposite.SRC_OVER, 1.0f));
117: g.drawImage(gif, x, y, w, h, this );
118: }
119:
120: public void mouseDragged(MouseEvent e) {
121: Dimension d = getSize();
122: int w = d.width / 6;
123: int h = d.height / 2;
124: Graphics g = getGraphics();
125: g.setXORMode(Color.black);
126: if (drag) {
127: g.drawImage(gif, xorX, xorY, xorX + w, xorY + h, 0, 0, gif
128: .getWidth(this ), gif.getHeight(this ), this );
129: }
130: xorX = e.getX() - w / 2;
131: xorY = e.getY() - w / 2;
132: g.drawImage(gif, xorX, xorY, xorX + w, xorY + h, 0, 0, gif
133: .getWidth(this ), gif.getHeight(this ), this );
134: drag = true;
135: }
136:
137: public void mouseMoved(MouseEvent e) {
138: }
139:
140: public void mouseClicked(MouseEvent e) {
141: int w = bi.getWidth(this );
142: int h = bi.getHeight(this );
143: if (original == null) {
144: original = bi.getRGB(0, 0, w, h, null, 0, w);
145: }
146: int modifier = e.getModifiers();
147: if ((modifier & InputEvent.BUTTON2_MASK) != 0) {
148: bi.setRGB(0, 0, w, h, original, 0, w);
149: repaint();
150: return;
151: }
152: int increment = -20;
153: if ((modifier & InputEvent.BUTTON3_MASK) != 0) {
154: increment = 20;
155: }
156: int[] rgb = bi.getRGB(0, 0, w, h, null, 0, w);
157: for (int i = 0; i < rgb.length; i++) {
158: int r = increment + ((rgb[i] & 0x00FF0000) >> 16);
159: int g = increment + ((rgb[i] & 0x0000FF00) >> 8);
160: int b = increment + ((rgb[i] & 0x000000FF) >> 0);
161: r = r < 255 ? r : 255;
162: g = g < 255 ? g : 255;
163: b = b < 255 ? b : 255;
164: r = r > 0 ? r : 0;
165: g = g > 0 ? g : 0;
166: b = b > 0 ? b : 0;
167: rgb[i] = rgb[i] & 0xFF000000;
168: rgb[i] += (r << 16);
169: rgb[i] += (g << 8);
170: rgb[i] += b;
171: }
172: bi.setRGB(0, 0, w, h, rgb, 0, w);
173: repaint();
174: }
175:
176: public void mouseEntered(MouseEvent e) {
177: }
178:
179: public void mouseExited(MouseEvent e) {
180: }
181:
182: public void mousePressed(MouseEvent e) {
183: }
184:
185: public void mouseReleased(MouseEvent e) {
186: Dimension d = getSize();
187: int w = d.width / 6;
188: int h = d.height / 2;
189: if (drag) {
190: Graphics g = getGraphics();
191: g.setXORMode(Color.black);
192: g.drawImage(gif, xorX, xorY, xorX + w, xorY + h, 0, 0, gif
193: .getWidth(this ), gif.getHeight(this ), this );
194: }
195: drag = false;
196: }
197:
198: static Image loadImage(Component component, String name) {
199: Image image = null;
200: URL url = null;
201: MediaTracker mt = new MediaTracker(component);
202: //First try from current directory
203: ClassLoader cl = component.getClass().getClassLoader();
204: if (cl != null) {
205: url = cl.getResource(name);
206: }
207: //if not found, try CLASSPATH
208: if (url == null) {
209: url = ClassLoader.getSystemResource(name);
210: }
211: try {
212: image = component.getToolkit().createImage(url);
213: } catch (Exception e) {
214: e.printStackTrace();
215: }
216: try {
217: mt.addImage(image, 0);
218: mt.waitForAll();
219: } catch (Exception e) {
220: e.printStackTrace();
221: }
222: return image;
223: }
224: }
|