📄 jcanvas3d.java
字号:
/* * $RCSfile: JCanvas3D.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.10 $ * $Date: 2007/04/11 02:08:56 $ * $State: Exp $ */package com.sun.j3d.exp.swing;import com.sun.j3d.exp.swing.impl.AutoOffScreenCanvas3D;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.GraphicsConfigTemplate;import java.awt.GraphicsConfiguration;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.lang.reflect.InvocationTargetException;import javax.media.j3d.Canvas3D;import javax.media.j3d.GraphicsConfigTemplate3D;import javax.swing.JPanel;import javax.swing.event.AncestorListener;/** * This class provides a lightweight capability to Java 3D. The component * handles bidirectional messaging between swing and Java 3D so that repaint * ordonned by swing are sent to the universe if necessary and refreshes from * the universe are painted accordingly. In order to get responsive interfaces * during layout changes, the canvas has a feature (disabled by default) that * lets true resizes occur only after a timer expires. Images between real * resizes can eventually be slightly wrong and pixelated, but their display * will be stutterless.<br> * Lightweight canvas also handles redirection to heavyweight canvas for the * following events:<br> * - InputMethodEvent<br> * - KeyEvent<br> * - FocusEvent<br> * - ComponentKeyEvent<br> * - MouseWheelEvent<br> * - MouseEvent<br> * - MouseMotionEvent<br> * <br> * <br> * When Swing is waiting for a canvas to be retrieved and that canvas is in * rendering stage,a loop takes place, which includes small calls to wait(). * The canvas status is tested for readiness before and after the wait(). If * the canvas is not ready to be retrieved after the wait(), counter is * decremented and control is given back to awt thread, which will repaint old * buffer. If the loop goes over a certain amount of iterations, the canvas is * declared 'crashed' and won't be updated anymore. This was done so that a * crashed canvas/universe does not remove control over your GUI and does not * leave you with a frozen application. In current implementation, the delay * before a canvas is declared crashed is of :<br> * <code>30 Math.max(20.0, getView().getMinimumFrameCycleTime() )</code> * * @author Frederic 'pepe' Barachant * * @see getLightweightComponent() * @see setResizeValidationDelay() * @see setResizeMode() * * @since Java 3D 1.5 */public class JCanvas3D extends JPanel implements AncestorListener { /** * Resizing the canvas or component will be done immediately. This * operation might take some time and make the application look sluggish. * * @see setResizeMode() */ public final static int RESIZE_IMMEDIATELY = 0; /** * Resizing the canvas or component will be done if no resizing * occurs after expiration of a certain delay. Rendering will be * eventually stretched or deformed. It can be useful on certain * applications where smooth update of UI during layout is needed or * desired. * * @see setResizeMode() */ public final static int RESIZE_DELAYED = 1; //TODO: FBA: this had been taken from javax.media.j3d.Screen3D. When/IF proper dpi handling comes one day, that part will have to be changed also for consistency /** size of a pixel */ private static double METERS_PER_PIXEL = 0.0254 / 90.0; /** the template to be used for this canvas */ private GraphicsConfigTemplate3D template; /** the graphics configuration used for this canvas */ private GraphicsConfiguration graphicsConfig; /** The canvas that is linked to the component. */ private InternalCanvas3D canvas; /** flag indicating that the JCanvas3D has been added to a container */ private boolean hasBeenAdded = false; /** The resize mode currently being used. */ int resizeMode; /** * the idle delay that will trigger a real resize. ('idle' being * the lack of resizing action from the user) */ int resizeValidationDelay; /** the device to be used by this canvas */ private GraphicsDevice device; //TODO: FBA: the constructor below should be callable. Code should be changed so that it is possible, in order for the canvas to be useable into netbeans. //TODO: FBA: create a netbeans module that installs J3D as a library and the JCanvas3D as a new item in a new J3D category of the swing palette (take from the java.net swash project) /** * Constructs and initializes a new JCanvas3D object that Java 3D * can render into. The screen device is obtained from * <code>GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()</code>, * which might not be the one you should use if you are in a multiscreen environment. * The JCanvas3D is constructed using the following default parameters:<br> * resize mode : RESIZE_IMMEDIATELY<br> * validation delay : 100ms<br> * double buffer enable : false<br> * stereo enable : false<br> */ public JCanvas3D() { this(null, GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice()); } /** * Constructs and initializes a new Canvas3D object that Java 3D * can render into, using the specified graphics device. * * @param device the screen graphics device that will be used to construct * a GraphicsConfiguration. */ public JCanvas3D(GraphicsDevice device) { this(null, device); } /** * Constructs and initializes a new Canvas3D object that Java 3D * can render into, using the specified template. * The screen device is obtained from * <code>GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()</code>, * which might not be the one you should use if you are * in a multiscreen environment. * * @param template The template that will be used to construct a * GraphicsConfiguration. The stereo and doublebuffer properties * are forced to UNNECESSARY. */ public JCanvas3D(GraphicsConfigTemplate3D template) { this(template, GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice()); } /** * Constructs and initializes a new Canvas3D object that Java 3D * can render into, using the specified template and graphics device. * * @param template The template that will be used to construct a * GraphicsConfiguration. The stereo and doublebuffer properties * are forced to UNNECESSARY. * @param device the screen graphics device that will be used to construct * a GraphicsConfiguration in conjunction with the template. */ public JCanvas3D(GraphicsConfigTemplate3D template, GraphicsDevice device) { this.device = device; this.template = new GraphicsConfigTemplate3D(); if (template != null) { // Clone template (it would be easier if GCT3D were cloneable) this.template.setRedSize(template.getRedSize()); this.template.setGreenSize(template.getGreenSize()); this.template.setBlueSize(template.getBlueSize()); this.template.setDepthSize(template.getDepthSize()); this.template.setSceneAntialiasing(template.getSceneAntialiasing()); this.template.setStencilSize(template.getStencilSize());// this.template.setDoubleBuffer(template.getDoubleBuffer());// this.template.setStereo(template.getStereo()); } // Force double-buffer and stereo to UNNECESSARY this.template.setStereo(GraphicsConfigTemplate.UNNECESSARY); this.template.setDoubleBuffer(GraphicsConfigTemplate.UNNECESSARY); graphicsConfig = this.device.getBestConfiguration(this.template); addAncestorListener(this); setDoubleBuffered(false); setResizeMode(RESIZE_IMMEDIATELY); setResizeValidationDelay(100); // so that key events and such can be received. setFocusable(true); } /** * {@inheritDoc} * * @param event {@inheritDoc} */ public void ancestorAdded(javax.swing.event.AncestorEvent event) { // if ( true == isVisible( ) ) // check if the component itself is visible. { Dimension sz = getSize(); if (0 == sz.width) { sz.width = 100; } if (0 == sz.height) { sz.height = 100; } createCanvas(sz.width, sz.height); canvas.addNotifyFlag = true; // make it so that i can call addNotify() without being rejected. canvas.addNotify(); hasBeenAdded = true; } } /** * {@inheritDoc} * * @param event {@inheritDoc} */ public void ancestorMoved(javax.swing.event.AncestorEvent event) { } /** * {@inheritDoc} * * @param event {@inheritDoc} */ public void ancestorRemoved(javax.swing.event.AncestorEvent event) { hasBeenAdded = false; canvas.removeNotify(); } /** * Computes the physical dimensions of the screen in space. */ private void computePhysicalDimensions() { // Fix to Issue : 433 - JCanvas3D crashed when using jogl pipe. Rectangle screenRect = this.graphicsConfig.getBounds(); int screenWidth = (int) screenRect.getWidth(); int screenHeight = (int) screenRect.getHeight(); canvas.getScreen3D().setSize(screenWidth, screenHeight); canvas.getScreen3D() .setPhysicalScreenWidth(((double) screenWidth) * METERS_PER_PIXEL); canvas.getScreen3D() .setPhysicalScreenHeight(((double) screenHeight) * METERS_PER_PIXEL); } /** * Creates a heavyweight canvas and initializes it, or changes the * size of the current one if present. Current heavyweight canvas is * changed only if size is different from the actual one. No canvas is * created if this component has no parent, that is, was not added to a * container. * * @param width the width of the canvas to create. * @param height the height of the canvas to create. */ void createCanvas(int width, int height) { if (getParent() == null) { return; } if (null != canvas) { // i had a canvas, i need to check if i really need to change it if ((width != canvas.getWidth()) || (height != canvas.getHeight())) { if ((null != canvas.getOffScreenBuffer()) && (null != canvas.getOffScreenBuffer().getImage())) { canvas.getOffScreenBuffer().getImage().flush(); // flushing so that eventual resources are freed. } } else { return; } } else { // no canvas, i have to create it. canvas = new InternalCanvas3D(this.graphicsConfig, this); } createOffScreenBuffer(width, height); // whatever happened right above, i need to create the offscreen buffer.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -