1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package jme3test.awt;
34 
35 import com.jme3.app.Application;
36 import com.jme3.app.SimpleApplication;
37 import com.jme3.system.AppSettings;
38 import com.jme3.system.JmeCanvasContext;
39 import com.jme3.util.JmeFormatter;
40 import java.awt.BorderLayout;
41 import java.awt.Canvas;
42 import java.awt.Container;
43 import java.awt.Dimension;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.WindowAdapter;
47 import java.awt.event.WindowEvent;
48 import java.util.concurrent.Callable;
49 import java.util.logging.ConsoleHandler;
50 import java.util.logging.Handler;
51 import java.util.logging.Logger;
52 import javax.swing.*;
53 
54 public class TestCanvas {
55 
56     private static JmeCanvasContext context;
57     private static Canvas canvas;
58     private static Application app;
59     private static JFrame frame;
60     private static Container canvasPanel1, canvasPanel2;
61     private static Container currentPanel;
62     private static JTabbedPane tabbedPane;
63     private static final String appClass = "jme3test.post.TestRenderToTexture";
64 
createTabs()65     private static void createTabs(){
66         tabbedPane = new JTabbedPane();
67 
68         canvasPanel1 = new JPanel();
69         canvasPanel1.setLayout(new BorderLayout());
70         tabbedPane.addTab("jME3 Canvas 1", canvasPanel1);
71 
72         canvasPanel2 = new JPanel();
73         canvasPanel2.setLayout(new BorderLayout());
74         tabbedPane.addTab("jME3 Canvas 2", canvasPanel2);
75 
76         frame.getContentPane().add(tabbedPane);
77 
78         currentPanel = canvasPanel1;
79     }
80 
createMenu()81     private static void createMenu(){
82         JMenuBar menuBar = new JMenuBar();
83         frame.setJMenuBar(menuBar);
84 
85         JMenu menuTortureMethods = new JMenu("Canvas Torture Methods");
86         menuBar.add(menuTortureMethods);
87 
88         final JMenuItem itemRemoveCanvas = new JMenuItem("Remove Canvas");
89         menuTortureMethods.add(itemRemoveCanvas);
90         itemRemoveCanvas.addActionListener(new ActionListener() {
91             public void actionPerformed(ActionEvent e) {
92                 if (itemRemoveCanvas.getText().equals("Remove Canvas")){
93                     currentPanel.remove(canvas);
94 
95                     itemRemoveCanvas.setText("Add Canvas");
96                 }else if (itemRemoveCanvas.getText().equals("Add Canvas")){
97                     currentPanel.add(canvas, BorderLayout.CENTER);
98 
99                     itemRemoveCanvas.setText("Remove Canvas");
100                 }
101             }
102         });
103 
104         final JMenuItem itemHideCanvas = new JMenuItem("Hide Canvas");
105         menuTortureMethods.add(itemHideCanvas);
106         itemHideCanvas.addActionListener(new ActionListener() {
107             public void actionPerformed(ActionEvent e) {
108                 if (itemHideCanvas.getText().equals("Hide Canvas")){
109                     canvas.setVisible(false);
110                     itemHideCanvas.setText("Show Canvas");
111                 }else if (itemHideCanvas.getText().equals("Show Canvas")){
112                     canvas.setVisible(true);
113                     itemHideCanvas.setText("Hide Canvas");
114                 }
115             }
116         });
117 
118         final JMenuItem itemSwitchTab = new JMenuItem("Switch to tab #2");
119         menuTortureMethods.add(itemSwitchTab);
120         itemSwitchTab.addActionListener(new ActionListener(){
121            public void actionPerformed(ActionEvent e){
122                if (itemSwitchTab.getText().equals("Switch to tab #2")){
123                    canvasPanel1.remove(canvas);
124                    canvasPanel2.add(canvas, BorderLayout.CENTER);
125                    currentPanel = canvasPanel2;
126                    itemSwitchTab.setText("Switch to tab #1");
127                }else if (itemSwitchTab.getText().equals("Switch to tab #1")){
128                    canvasPanel2.remove(canvas);
129                    canvasPanel1.add(canvas, BorderLayout.CENTER);
130                    currentPanel = canvasPanel1;
131                    itemSwitchTab.setText("Switch to tab #2");
132                }
133            }
134         });
135 
136         JMenuItem itemSwitchLaf = new JMenuItem("Switch Look and Feel");
137         menuTortureMethods.add(itemSwitchLaf);
138         itemSwitchLaf.addActionListener(new ActionListener(){
139             public void actionPerformed(ActionEvent e){
140                 try {
141                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
142                 } catch (Throwable t){
143                     t.printStackTrace();
144                 }
145                 SwingUtilities.updateComponentTreeUI(frame);
146                 frame.pack();
147             }
148         });
149 
150         JMenuItem itemSmallSize = new JMenuItem("Set size to (0, 0)");
151         menuTortureMethods.add(itemSmallSize);
152         itemSmallSize.addActionListener(new ActionListener(){
153             public void actionPerformed(ActionEvent e){
154                 Dimension preferred = frame.getPreferredSize();
155                 frame.setPreferredSize(new Dimension(0, 0));
156                 frame.pack();
157                 frame.setPreferredSize(preferred);
158             }
159         });
160 
161         JMenuItem itemKillCanvas = new JMenuItem("Stop/Start Canvas");
162         menuTortureMethods.add(itemKillCanvas);
163         itemKillCanvas.addActionListener(new ActionListener() {
164             public void actionPerformed(ActionEvent e) {
165                 currentPanel.remove(canvas);
166                 app.stop(true);
167 
168                 createCanvas(appClass);
169                 currentPanel.add(canvas, BorderLayout.CENTER);
170                 frame.pack();
171                 startApp();
172             }
173         });
174 
175         JMenuItem itemExit = new JMenuItem("Exit");
176         menuTortureMethods.add(itemExit);
177         itemExit.addActionListener(new ActionListener() {
178             public void actionPerformed(ActionEvent ae) {
179                 frame.dispose();
180                 app.stop();
181             }
182         });
183     }
184 
createFrame()185     private static void createFrame(){
186         frame = new JFrame("Test");
187         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
188         frame.addWindowListener(new WindowAdapter(){
189             @Override
190             public void windowClosed(WindowEvent e) {
191                 app.stop();
192             }
193         });
194 
195         createTabs();
196         createMenu();
197     }
198 
createCanvas(String appClass)199     public static void createCanvas(String appClass){
200         AppSettings settings = new AppSettings(true);
201         settings.setWidth(640);
202         settings.setHeight(480);
203 
204         try{
205             Class<? extends Application> clazz = (Class<? extends Application>) Class.forName(appClass);
206             app = clazz.newInstance();
207         }catch (ClassNotFoundException ex){
208             ex.printStackTrace();
209         }catch (InstantiationException ex){
210             ex.printStackTrace();
211         }catch (IllegalAccessException ex){
212             ex.printStackTrace();
213         }
214 
215         app.setPauseOnLostFocus(false);
216         app.setSettings(settings);
217         app.createCanvas();
218         app.startCanvas();
219 
220         context = (JmeCanvasContext) app.getContext();
221         canvas = context.getCanvas();
222         canvas.setSize(settings.getWidth(), settings.getHeight());
223     }
224 
startApp()225     public static void startApp(){
226         app.startCanvas();
227         app.enqueue(new Callable<Void>(){
228             public Void call(){
229                 if (app instanceof SimpleApplication){
230                     SimpleApplication simpleApp = (SimpleApplication) app;
231                     simpleApp.getFlyByCamera().setDragToRotate(true);
232                 }
233                 return null;
234             }
235         });
236 
237     }
238 
main(String[] args)239     public static void main(String[] args){
240         JmeFormatter formatter = new JmeFormatter();
241 
242         Handler consoleHandler = new ConsoleHandler();
243         consoleHandler.setFormatter(formatter);
244 
245         Logger.getLogger("").removeHandler(Logger.getLogger("").getHandlers()[0]);
246         Logger.getLogger("").addHandler(consoleHandler);
247 
248         createCanvas(appClass);
249 
250         try {
251             Thread.sleep(500);
252         } catch (InterruptedException ex) {
253         }
254 
255         SwingUtilities.invokeLater(new Runnable(){
256             public void run(){
257                 JPopupMenu.setDefaultLightWeightPopupEnabled(false);
258 
259                 createFrame();
260 
261                 currentPanel.add(canvas, BorderLayout.CENTER);
262                 frame.pack();
263                 startApp();
264                 frame.setLocationRelativeTo(null);
265                 frame.setVisible(true);
266             }
267         });
268     }
269 
270 }
271