package jmetest.util;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import jmetest.awt.swingui.TestJMEDesktop;
import com.jme.bounding.OrientedBoundingBox;
import com.jme.image.Texture;
import com.jme.input.AbsoluteMouse;
import com.jme.input.InputHandler;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.Timer;
import com.jmex.awt.swingui.JMEDesktop;
public class Desktop{
public JMEDesktop jmeDesktop;
private Node desktopNode;
private DisplaySystem display;
Timer timer;
Node rootNode;
public Desktop(Color color, Node rootNode, Timer timer, InputHandler input) {
// super("dtn");
this.rootNode = rootNode;
this.timer = timer;
jmeDesktop = new JMEDesktop( "dk" );
display = DisplaySystem.getDisplaySystem();
jmeDesktop.setup( display.getWidth(), display.getHeight(), false, input );
jmeDesktop.setLightCombineMode( LightState.OFF );
desktopNode = new Node("desktop node" );
desktopNode.attachChild(jmeDesktop);
rootNode.attachChild(desktopNode);
jmeDesktop.getJDesktop().setBackground( color );
createBoxBorder();
perspective();
createSwingStuff();
}
private void createBoxBorder() {
//create a border from boxes around the desktop
float borderWidth = 10;
float halfBorderWidth = borderWidth / 2;
int halfDesktopWidth = jmeDesktop.getJDesktop().getWidth() / 2;
int halfDesktopHeight = jmeDesktop.getJDesktop().getHeight() / 2;
Box top = new Box( "top border", new Vector3f(),
halfDesktopWidth + halfBorderWidth,
halfBorderWidth, halfBorderWidth );
top.getLocalTranslation().set( 0, - halfDesktopHeight, 0 );
top.setModelBound( new OrientedBoundingBox() );
top.updateModelBound();
desktopNode.attachChild( top );
Box bottom = new Box( "bottom border", new Vector3f(),
halfDesktopWidth + halfBorderWidth,
halfBorderWidth, halfBorderWidth );
bottom.getLocalTranslation().set( 0, halfDesktopHeight, 0 );
bottom.setModelBound( new OrientedBoundingBox() );
bottom.updateModelBound();
desktopNode.attachChild( bottom );
Box left = new Box( "left border", new Vector3f(),
halfBorderWidth,
halfDesktopHeight + halfBorderWidth,
halfBorderWidth );
left.getLocalTranslation().set( - halfDesktopWidth, 0, 0 );
left.setModelBound( new OrientedBoundingBox() );
left.updateModelBound();
desktopNode.attachChild( left );
Box right = new Box( "right border", new Vector3f(),
halfBorderWidth,
halfDesktopHeight + halfBorderWidth,
halfBorderWidth );
right.getLocalTranslation().set( halfDesktopWidth, 0, 0 );
right.setModelBound( new OrientedBoundingBox() );
right.updateModelBound();
desktopNode.attachChild( right );
}
private void perspective() {
desktopNode.getLocalRotation().fromAngleNormalAxis( -0.7f, new Vector3f( 1, 0, 0 ) );
desktopNode.setLocalScale( 24f / jmeDesktop.getJDesktop().getWidth() );
desktopNode.getLocalTranslation().set( 0, 0, 0 );
desktopNode.setRenderQueueMode( Renderer.QUEUE_TRANSPARENT );
}
private void fullScreen() {
final DisplaySystem display = DisplaySystem.getDisplaySystem();
desktopNode.getLocalRotation().set( 0, 0, 0, 1 );
desktopNode.getLocalTranslation().set( display.getWidth() / 2, display.getHeight() / 2, 0 );
desktopNode.getLocalScale().set( 1, 1, 1 );
desktopNode.setRenderQueueMode( Renderer.QUEUE_ORTHO );
}
private AbsoluteMouse cursor;
protected void createSwingStuff() {
final JDesktopPane desktopPane = jmeDesktop.getJDesktop();
desktopPane.removeAll();
createSwingInternalFrame( desktopPane, "My Frame 1", 10, 150 );
createSwingInternalFrame( desktopPane, "My Frame 2", 20, 300 );
createSwingInternalFrame( desktopPane, null, 400, 350 );
JButton fullScreenButton = new JButton( "<html><big>toggle fullscreen</big></html>" );
fullScreenButton.setSize( fullScreenButton.getPreferredSize() );
fullScreenButton.setLocation( ( display.getWidth() - fullScreenButton.getWidth() ) / 2,
display.getHeight() - 40 - fullScreenButton.getHeight() / 2 );
desktopPane.add( fullScreenButton );
fullScreenButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
if ( desktopNode.getRenderQueueMode() == Renderer.QUEUE_ORTHO ) {
perspective();
}
else {
fullScreen();
}
}
} );
createRotateButton( desktopPane, 0.25f );
createRotateButton( desktopPane, -0.25f );
createRotateButton( desktopPane, 0.15f );
createRotateButton( desktopPane, -0.15f );
createRotateButton( desktopPane, 0.45f );
createRotateButton( desktopPane, -0.45f );
desktopPane.repaint();
desktopPane.revalidate();
}
private void createRotateButton( JDesktopPane parent, final float direction ) {
JButton button = new JButton( direction < 0 ? "<" : ">" );
button.setSize( button.getPreferredSize() );
button.setLocation( (int) ( ( display.getWidth() - button.getWidth() ) / 2
+ direction * display.getWidth() ), display.getHeight() - 40 - button.getHeight() / 2 );
parent.add( button );
button.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
if ( desktopNode.getRenderQueueMode() != Renderer.QUEUE_ORTHO ) {
desktopNode.addController( new Controller() {
private static final long serialVersionUID = 1L;
float length = 1;
float endTime = timer.getTimeInSeconds() + length;
Quaternion start = new Quaternion().set( desktopNode.getLocalRotation() );
Quaternion finish = new Quaternion().set( desktopNode.getLocalRotation() ).multLocal(
new Quaternion().fromAngleNormalAxis( direction, new Vector3f( 0, 1, 0 ) ) );
public void update( float time ) {
if ( timer.getTimeInSeconds() > endTime ) {
desktopNode.removeController( this );
}
else {
desktopNode.getLocalRotation().slerp( finish, start, ( endTime - timer.getTimeInSeconds() ) / length );
desktopNode.getLocalRotation().normalize();
}
}
} );
}
}
} );
}
private void createSwingInternalFrame( final JDesktopPane desktopPane, final String title, int x, int y ) {
final JInternalFrame internalFrame = new JInternalFrame( title );
if ( title == null ) {
internalFrame.putClientProperty( "JInternalFrame.isPalette", Boolean.TRUE );
}
internalFrame.setLocation( x, y );
internalFrame.setResizable( true );
internalFrame.getContentPane().setLayout( new FlowLayout() );
}
private void create3DStuff() {
// Normal Scene setup stuff...
final Vector3f axis = new Vector3f( 1, 1, 0.5f ).normalizeLocal();
final Box box = new Box( "Box", new Vector3f( -5, -5, -5 ), new Vector3f( 5, 5, 5 ) );
box.setModelBound( new OrientedBoundingBox() );
box.updateModelBound();
box.setLocalTranslation( new Vector3f( 0, 0, -10 ) );
box.setRandomColors();
box.setLightCombineMode( LightState.OFF );
TextureState ts = display.getRenderer().createTextureState();
ts.setEnabled( true );
ts.setTexture( TextureManager.loadTexture( TestJMEDesktop.class
.getClassLoader().getResource(
"jmetest/data/images/Monkey.jpg" ),
Texture.MM_LINEAR, Texture.FM_LINEAR ) );
box.setRenderState( ts );
//let the box rotate
box.addController( new Controller() {
private static final long serialVersionUID = 1L;
public void update( float time ) {
box.getLocalRotation().fromAngleNormalAxis( timer.getTimeInSeconds(), axis );
}
} );
rootNode.attachChild( box );
}
protected void cleanup() {
jmeDesktop.dispose();
}
}
Any more assistance - please let me know
BTW, am doing picking in the SwingFrame, therefore the provider is AWT
MouseInput.setProvider("AWT");
KeyInput.setProvider("AWT");