001/** 002 * Copyright (c) 2008-2014 Ardor Labs, Inc. 003 * 004 * This file is part of Ardor3D. 005 * 006 * Ardor3D is free software: you can redistribute it and/or modify it 007 * under the terms of its license which may be found in the accompanying 008 * LICENSE file or at <http://www.ardor3d.com/LICENSE>. 009 */ 010 011package com.ardor3d.example.canvas; 012 013import java.awt.Dimension; 014import java.awt.GridLayout; 015import java.awt.event.WindowAdapter; 016import java.awt.event.WindowEvent; 017import java.io.IOException; 018import java.net.URISyntaxException; 019import java.util.HashMap; 020import java.util.Map; 021 022import javax.swing.JFrame; 023import javax.swing.JLabel; 024import javax.swing.SwingConstants; 025 026import com.ardor3d.example.Purpose; 027import com.ardor3d.framework.Canvas; 028import com.ardor3d.framework.DisplaySettings; 029import com.ardor3d.framework.FrameHandler; 030import com.ardor3d.framework.jogl.JoglCanvasRenderer; 031import com.ardor3d.framework.jogl.awt.JoglSwingCanvas; 032import com.ardor3d.image.util.awt.AWTImageLoader; 033import com.ardor3d.input.ControllerWrapper; 034import com.ardor3d.input.Key; 035import com.ardor3d.input.MouseCursor; 036import com.ardor3d.input.PhysicalLayer; 037import com.ardor3d.input.awt.AwtFocusWrapper; 038import com.ardor3d.input.awt.AwtKeyboardWrapper; 039import com.ardor3d.input.awt.AwtMouseManager; 040import com.ardor3d.input.awt.AwtMouseWrapper; 041import com.ardor3d.input.logical.DummyControllerWrapper; 042import com.ardor3d.input.logical.InputTrigger; 043import com.ardor3d.input.logical.KeyPressedCondition; 044import com.ardor3d.input.logical.LogicalLayer; 045import com.ardor3d.input.logical.TriggerAction; 046import com.ardor3d.input.logical.TwoInputStates; 047import com.ardor3d.util.Timer; 048import com.ardor3d.util.resource.ResourceLocatorTool; 049import com.ardor3d.util.resource.SimpleResourceLocator; 050 051/** 052 * This examples demonstrates how to render OpenGL (via JOGL) on a Swing canvas. FIXME: fix the thumbnail and the 053 * description 054 */ 055@Purpose(htmlDescriptionKey = "com.ardor3d.example.canvas.JoglAwtExample", // 056thumbnailPath = "com/ardor3d/example/media/thumbnails/canvas_JoglAwtExample.jpg", // 057maxHeapMemory = 64) 058public class JoglSwingExample { 059 static MouseCursor _cursor1; 060 static MouseCursor _cursor2; 061 062 static Map<Canvas, Boolean> _showCursor1 = new HashMap<>(); 063 064 public static void main(final String[] args) throws Exception { 065 System.setProperty("ardor3d.useMultipleContexts", "true"); 066 067 final Timer timer = new Timer(); 068 final FrameHandler frameWork = new FrameHandler(timer); 069 070 final MyExit exit = new MyExit(); 071 final LogicalLayer logicalLayer = new LogicalLayer(); 072 073 final ExampleScene scene1 = new ExampleScene(); 074 final RotatingCubeGame game1 = new RotatingCubeGame(scene1, exit, logicalLayer, Key.T); 075 076 final ExampleScene scene2 = new ExampleScene(); 077 final RotatingCubeGame game2 = new RotatingCubeGame(scene2, exit, logicalLayer, Key.G); 078 079 frameWork.addUpdater(game1); 080 frameWork.addUpdater(game2); 081 082 final JFrame frame = new JFrame("AWT Example"); 083 frame.addWindowListener(new WindowAdapter() { 084 @Override 085 public void windowClosing(final WindowEvent e) { 086 exit.exit(); 087 } 088 }); 089 090 frame.setLayout(new GridLayout(2, 3)); 091 092 AWTImageLoader.registerLoader(); 093 094 try { 095 final SimpleResourceLocator srl = new SimpleResourceLocator(ResourceLocatorTool.getClassPathResource( 096 JoglSwingExample.class, "com/ardor3d/example/media/")); 097 ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, srl); 098 } catch (final URISyntaxException ex) { 099 ex.printStackTrace(); 100 } 101 102 final AWTImageLoader awtImageLoader = new AWTImageLoader(); 103 _cursor1 = createMouseCursor(awtImageLoader, "com/ardor3d/example/media/input/wait_cursor.png"); 104 _cursor2 = createMouseCursor(awtImageLoader, "com/ardor3d/example/media/input/movedata.gif"); 105 106 addCanvas(frame, scene1, logicalLayer, frameWork); 107 frame.add(new JLabel( 108 "<html>" 109 + "<table>" 110 + "<tr><th align=\"left\" style=\"font-size: 16\">Action</th><th align=\"left\" style=\"font-size: 16\">Command</th></tr>" 111 + "<tr><td>WS</td><td>Move camera position forward/back</td></tr>" 112 + "<tr><td>AD</td><td>Turn camera left/right</td></tr>" 113 + "<tr><td>T</td><td>Toggle cube rotation for scene 1 on press</td></tr>" 114 + "<tr><td>G</td><td>Toggle cube rotation for scene 2 on press</td></tr>" 115 + "<tr><td>U</td><td>Toggle both cube rotations on release</td></tr>" 116 + "<tr><td>0 (zero)</td><td>Reset camera position</td></tr>" 117 + "<tr><td>9</td><td>Face camera towards cube without changing position</td></tr>" 118 + "<tr><td>ESC</td><td>Quit</td></tr>" 119 + "<tr><td>Mouse</td><td>Press left button to rotate camera.</td></tr>" + "</table>" 120 + "</html>", SwingConstants.CENTER)); 121 addCanvas(frame, scene1, logicalLayer, frameWork); 122 frame.add(new JLabel("", SwingConstants.CENTER)); 123 addCanvas(frame, scene2, logicalLayer, frameWork); 124 frame.add(new JLabel("", SwingConstants.CENTER)); 125 126 frame.pack(); 127 frame.setVisible(true); 128 129 game1.init(); 130 game2.init(); 131 132 while (!exit.isExit()) { 133 frameWork.updateFrame(); 134 Thread.yield(); 135 } 136 137 frame.dispose(); 138 System.exit(0); 139 } 140 141 private static MouseCursor createMouseCursor(final AWTImageLoader awtImageLoader, final String resourceName) 142 throws IOException { 143 final com.ardor3d.image.Image image = awtImageLoader.load( 144 ResourceLocatorTool.getClassPathResourceAsStream(JoglSwingExample.class, resourceName), false); 145 146 return new MouseCursor("cursor1", image, 0, image.getHeight() - 1); 147 } 148 149 private static void addCanvas(final JFrame frame, final ExampleScene scene, final LogicalLayer logicalLayer, 150 final FrameHandler frameWork) throws Exception { 151 final JoglCanvasRenderer canvasRenderer = new JoglCanvasRenderer(scene); 152 153 final DisplaySettings settings = new DisplaySettings(400, 300, 24, 0, 0, 16, 0, 0, false, false); 154 final JoglSwingCanvas theCanvas = new JoglSwingCanvas(settings, canvasRenderer); 155 156 frame.add(theCanvas); 157 158 _showCursor1.put(theCanvas, true); 159 160 theCanvas.setSize(new Dimension(400, 300)); 161 theCanvas.setVisible(true); 162 163 final AwtKeyboardWrapper keyboardWrapper = new AwtKeyboardWrapper(theCanvas); 164 final AwtFocusWrapper focusWrapper = new AwtFocusWrapper(theCanvas); 165 final AwtMouseManager mouseManager = new AwtMouseManager(theCanvas); 166 final AwtMouseWrapper mouseWrapper = new AwtMouseWrapper(theCanvas, mouseManager); 167 theCanvas.setMouseManager(mouseManager); 168 final ControllerWrapper controllerWrapper = new DummyControllerWrapper(); 169 170 final PhysicalLayer pl = new PhysicalLayer(keyboardWrapper, mouseWrapper, controllerWrapper, focusWrapper); 171 172 logicalLayer.registerInput(theCanvas, pl); 173 174 logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.H), new TriggerAction() { 175 @Override 176 public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { 177 if (source != theCanvas) { 178 return; 179 } 180 181 if (_showCursor1.get(theCanvas)) { 182 mouseManager.setCursor(_cursor1); 183 } else { 184 mouseManager.setCursor(_cursor2); 185 } 186 187 _showCursor1.put(theCanvas, !_showCursor1.get(theCanvas)); 188 } 189 })); 190 logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.J), new TriggerAction() { 191 @Override 192 public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) { 193 if (source != theCanvas) { 194 return; 195 } 196 197 mouseManager.setCursor(MouseCursor.SYSTEM_DEFAULT); 198 } 199 })); 200 201 frameWork.addCanvas(theCanvas); 202 203 } 204 205 private static class MyExit implements Exit { 206 private volatile boolean exit = false; 207 208 @Override 209 public void exit() { 210 exit = true; 211 } 212 213 public boolean isExit() { 214 return exit; 215 } 216 } 217}