001: /*
002: * $RCSfile: TextureControlPanel.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.BorderLayout;
048: import java.awt.Color;
049: import java.awt.Dimension;
050: import java.awt.Rectangle;
051: import java.awt.event.ActionEvent;
052: import java.awt.event.ActionListener;
053: import java.awt.image.BufferedImage;
054:
055: import java.net.URL;
056:
057: import javax.imageio.ImageIO;
058:
059: import javax.swing.JCheckBox;
060: import javax.swing.JComponent;
061: import javax.swing.JDialog;
062: import javax.swing.JLabel;
063: import javax.swing.JPanel;
064: import javax.swing.JSlider;
065: import javax.swing.event.ChangeEvent;
066: import javax.swing.event.ChangeListener;
067: import org.jdesktop.j3d.examples.Resources;
068:
069: /**
070: * A control panel for Dot3Demo.
071: * It enables user change LightMap, enables/disables textures units states
072: * and toggles geometry wireframes on/off
073: */
074: public class TextureControlPanel extends JDialog implements
075: ChangeListener, ActionListener {
076: /** renderer for lightMap, with support for mouse interaction **/
077: private MyCanvas canvas = null;
078: /** file name for light mask */
079: private String maskFileName = "resources/images/mask.png";
080: /** a slider to change Z light direction, i.e, blue channel */
081: private JSlider sliderZ = new JSlider(JSlider.HORIZONTAL, 1, 255,
082: 142);
083: /** target demo instance to be controled **/
084: private Dot3Demo dot3DemoFrame;
085:
086: // some checkboxes for user interaction
087: private JCheckBox cbWireframe = new JCheckBox("Show as Wireframe",
088: false);
089: private JCheckBox cbDot3 = new JCheckBox("Show Dot3 texture", true);
090: private JCheckBox cbShowLightMap = new JCheckBox(
091: "Show LightMap texture", true);
092: private JCheckBox cbShowColor = new JCheckBox("Show Color texture",
093: true);
094: private JCheckBox cbDragLightMask = new JCheckBox("Drag light mask");
095:
096: private JLabel lbSliderZ = new JLabel();
097: private JLabel lbMessage = new JLabel();
098:
099: public TextureControlPanel(Dot3Demo owner) {
100: super (owner);
101: dot3DemoFrame = owner;
102: try {
103: URL url = Resources.getResource(maskFileName);
104: BufferedImage mask = ImageIO.read(url);
105: canvas = new MyCanvas(mask);
106: init();
107: } catch (Exception e) {
108: e.printStackTrace();
109: }
110: }
111:
112: public TextureControlPanel() {
113: this (null);
114: }
115:
116: /**
117: * Creates Graphical User Interface
118: * @throws Exception
119: */
120: private void init() throws Exception {
121: Dimension dim = new Dimension(540, 350);
122: this .setSize(dim);
123: this .setPreferredSize(dim);
124: this .setTitle("DOT3Demo Texture Control Panel");
125: this .setLayout(new BorderLayout());
126:
127: JPanel panel = new JPanel();
128: this .getContentPane().add(panel, BorderLayout.CENTER);
129: canvas.setSize(new Dimension(256, 256));
130: canvas.setBounds(new Rectangle(40, 40, 256, 256));
131:
132: sliderZ.setBounds(new Rectangle(310, 190, 205, 45));
133: sliderZ.setPaintTicks(true);
134: sliderZ.setMajorTickSpacing(63);
135:
136: cbWireframe.setBounds(new Rectangle(310, 50, 200, 20));
137: cbWireframe.setToolTipText("Toggles Wireframe");
138: cbDot3.setBounds(new Rectangle(310, 70, 150, 20));
139: cbShowLightMap.setBounds(new Rectangle(310, 90, 200, 20));
140: cbShowLightMap.setToolTipText("Toggles DOT3 texture");
141: cbShowColor.setBounds(new Rectangle(310, 110, 200, 20));
142: cbShowColor.setToolTipText("Toggles Color texture");
143:
144: panel.setLayout(null);
145:
146: cbDragLightMask.setBounds(new Rectangle(310, 135, 200, 20));
147: lbMessage
148: .setText("<html>Left-click and drag to change Light Direction."
149: + " Right-click and drag to move spotlight.</html>");
150: lbMessage.setBounds(new Rectangle(305, 245, 210, 60));
151:
152: lbSliderZ.setText("Blue Light (Dot3 Z axis)");
153: lbSliderZ.setBounds(new Rectangle(310, 170, 210, 15));
154: lbSliderZ.setToolTipText("changes light intensity from Z axis");
155:
156: panel.add(cbDragLightMask, null);
157: panel.add(lbMessage, null);
158: panel.add(lbSliderZ, null);
159: panel.add(sliderZ, null);
160: panel.add(canvas, null);
161: panel.add(cbShowColor, null);
162: panel.add(cbShowLightMap, null);
163: panel.add(cbWireframe, null);
164: panel.add(cbDot3, null);
165:
166: sliderZ.addChangeListener(this );
167:
168: cbDot3.addActionListener(this );
169: cbShowColor.addActionListener(this );
170: cbShowLightMap.addActionListener(this );
171: cbWireframe.addActionListener(this );
172: cbDragLightMask.addActionListener(this );
173: }
174:
175: public void stateChanged(ChangeEvent ev) {
176: JComponent source = (JComponent) ev.getSource();
177: if (sliderZ.equals(source)) {
178: int xVal = canvas.getBgColor().getRed();
179: int yVal = canvas.getBgColor().getGreen();
180: int zVal = sliderZ.getValue();
181: Color ligtDir = new Color(xVal, yVal, zVal);
182: updateLightMap(ligtDir);
183: }
184: }
185:
186: private void updateLightMap(Color ligtDir) {
187: canvas.setBgColor(ligtDir);
188: canvas.repaint();
189: dot3DemoFrame.updateLighMap(canvas.getTextureImage());
190: }
191:
192: public void actionPerformed(ActionEvent ev) {
193: JComponent source = (JComponent) ev.getSource();
194: if (cbWireframe.equals(source)) {
195: dot3DemoFrame.setWireframeMode(cbWireframe.isSelected());
196: } else if (cbDot3.equals(source) || cbShowColor.equals(source)
197: || cbShowLightMap.equals(source)) {
198: dot3DemoFrame.showTextures(cbShowLightMap.isSelected(),
199: cbDot3.isSelected(), cbShowColor.isSelected());
200: } else if (cbDragLightMask.equals(source)) {
201: canvas.dragMask = cbDragLightMask.isSelected();
202: }
203:
204: }
205:
206: /**
207: * Wrapper method call for MyCanvas. hasTextureImageReady()
208: * @return true if exists a new texture image available
209: */
210: public boolean hasTextureImageReady() {
211: return canvas.hasTextureImageReady();
212: }
213:
214: /**
215: * Wrapper method call for MyCanvas.getTextureImage()
216: * Returns a texture image.<br>
217: * Avoid calling the same image several times
218: * by cheking hasTextureImageReady() first.
219: * @return latest texture image available
220: */
221: public BufferedImage getTextureImage() {
222: return canvas.getTextureImage();
223: }
224:
225: /**
226: * Wrapper method to MyCanvas.setLightMask(mask)
227: * @param mask a new light mask
228: */
229: public void setLightMask(BufferedImage mask) {
230: canvas.setLightMask(mask);
231: }
232: }
|