001: /*
002: * $RCSfile: MyCanvas.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:21:36 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.dot3;
046:
047: import java.awt.*;
048: import java.awt.image.*;
049: import java.awt.event.*;
050: import javax.swing.*;
051: import javax.swing.event.MouseInputListener;
052:
053: /**
054: * A mouse interactive canvas for lightMap image
055: */
056: public class MyCanvas extends JPanel implements MouseInputListener {
057: BufferedImage lightMask = null;
058: BufferedImage textureImage = null;
059: Graphics2D gr = null;
060: Point location = new Point();
061: // default color light map
062: Color bgColor = new Color(147, 147, 147);
063:
064: int x = 0;
065: int y = 0;
066: int z = 142;
067: // texture image size
068: private static final int textureSize = 256;
069: boolean mouseOut = true;
070: //flag about image is ready or not for use
071: boolean imageReady = false;
072: // allows mask be dragged with mouse
073: boolean dragMask = false;
074: boolean updateLightDir = false;
075: boolean updateMaskPosition = false;
076:
077: /**
078: * Creates a MyCanvas object with a image lightMask.
079: * Also creates a default ImageLight map
080: * @param mask light lightMask used
081: */
082: public MyCanvas(BufferedImage mask) {
083: super ();
084: this .lightMask = mask;
085: // create a light map
086: setTextureImage(new BufferedImage(textureSize, textureSize,
087: BufferedImage.TYPE_INT_RGB));
088: // Graphics used to update lightmap
089: gr = getTextureImage().createGraphics();
090:
091: Dimension dimSize = new Dimension(textureSize, textureSize);
092: // lock size
093: this .setSize(dimSize);
094: this .setMaximumSize(dimSize);
095: this .setMinimumSize(dimSize);
096:
097: this .setDoubleBuffered(true);
098: this .setOpaque(true);
099: this .addMouseMotionListener(this );
100: this .addMouseListener(this );
101: }
102:
103: /**
104: * Handles mouse click event.
105: * Get mouse coords call repaint for proper imageLight update
106: * @param ev mouse event
107: */
108: public void mouseClicked(MouseEvent ev) {
109: x = ev.getX();
110: y = this .getHeight() - ev.getY();
111: updateLightDir = true;
112: repaint();
113: }
114:
115: public void mousePressed(MouseEvent e) {
116: }
117:
118: public void mouseReleased(MouseEvent e) {
119: }
120:
121: /**
122: * Handles mouse drag event.
123: * get current mouse position and calls repaint for proper imageLight update
124: * @param ev
125: */
126: public void mouseDragged(MouseEvent ev) {
127: if (!mouseOut) {
128: x = ev.getX();
129: y = this .getHeight() - ev.getY();
130:
131: //changes lightDir
132: if ((ev.getModifiers() & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) {
133: updateLightDir = true;
134: updateMaskPosition = false;
135: }
136: //updates light mask position
137: if ((ev.getModifiers() & ev.BUTTON2_MASK) == ev.BUTTON2_MASK
138: || (ev.getModifiers() & ev.BUTTON3_MASK) == ev.BUTTON3_MASK) {
139: updateLightDir = false;
140: updateMaskPosition = true;
141: }
142: repaint();
143: }
144: }
145:
146: public void mouseMoved(MouseEvent ev) {
147: // disable updates on lightMap
148: updateLightDir = false;
149: updateMaskPosition = false;
150: }
151:
152: public void mouseEntered(MouseEvent e) {
153: mouseOut = false;
154: }
155:
156: public void mouseExited(MouseEvent e) {
157: mouseOut = true;
158: }
159:
160: /**
161: * updates imageLight using current setings
162: * @param g
163: */
164: public void paintComponent(Graphics g) {
165: imageReady = false;
166: Graphics2D g2d = (Graphics2D) g;
167:
168: // ligthDir has changed, we must update bgColor li
169: if (updateLightDir) {
170: int blue = bgColor.getBlue();
171: //clamp values to 255
172: y = y > 255 ? 255 : y;
173: x = x > 255 ? 255 : x;
174: bgColor = new Color(y, x, blue);
175: }
176: // paint lightMap
177: gr.setColor(bgColor);
178: gr.fillRect(0, 0, textureSize, textureSize);
179:
180: // draw mask on mouse position
181: if (dragMask || updateMaskPosition) {
182: int maskWH = lightMask.getWidth() / 2;
183: int mx = x - maskWH;
184: int my = textureSize - y - maskWH; // y value is inverted
185: // clamp mouse position, to avoid drawing outside imageLigh bounds
186: mx = mx > textureSize ? textureSize : mx;
187: my = my > textureSize ? textureSize : my;
188: // draw light mask
189: gr.drawImage(lightMask, mx, my, this );
190: }
191:
192: g2d.drawImage(getTextureImage(), 0, 0, this );
193: imageReady = true;
194: }
195:
196: /**
197: *
198: * @return true if exists a new texture image available
199: */
200: public boolean hasTextureImageReady() {
201: return imageReady;
202: }
203:
204: /**
205: * Returns a texture image.<br>
206: * You can avoid calling the same image several times
207: * by checking hasTextureImageReady() first.
208: * @return latest texture image available
209: */
210: public BufferedImage getTextureImage() {
211: // sign as texture used for next call;
212: imageReady = false;
213: //return image
214: return textureImage;
215: }
216:
217: public Image getMask() {
218: return lightMask;
219: }
220:
221: public void setLightMask(BufferedImage mask) {
222: this .lightMask = mask;
223: }
224:
225: public Color getBgColor() {
226: return bgColor;
227: }
228:
229: public void setBgColor(Color bgColor) {
230: this .bgColor = bgColor;
231: }
232:
233: public void setTextureImage(BufferedImage textureImage) {
234: this.textureImage = textureImage;
235: }
236:
237: }
|