PageRenderTime 35ms CodeModel.GetById 16ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/basic/javaGUI/StartSwingExample.java

http://mt4j.googlecode.com/
Java | 239 lines | 175 code | 40 blank | 24 comment | 10 complexity | 1bfcffb69c9421fe315fd9e60b641cf3 MD5 | raw file
  1package basic.javaGUI;
  2
  3import java.awt.BorderLayout;
  4import java.awt.Container;
  5import java.awt.FlowLayout;
  6import java.awt.GridLayout;
  7import java.awt.event.ActionEvent;
  8import java.awt.event.ActionListener;
  9import java.awt.event.KeyEvent;
 10
 11import javax.swing.Box;
 12import javax.swing.JButton;
 13import javax.swing.JDialog;
 14import javax.swing.JFrame;
 15import javax.swing.JLabel;
 16import javax.swing.JMenu;
 17import javax.swing.JMenuBar;
 18import javax.swing.JMenuItem;
 19import javax.swing.JPanel;
 20import javax.swing.JPopupMenu;
 21import javax.swing.UIManager;
 22
 23import org.mt4j.MTApplication;
 24import org.mt4j.components.MTComponent;
 25import org.mt4j.components.visibleComponents.shapes.MTEllipse;
 26import org.mt4j.components.visibleComponents.shapes.MTRectangle;
 27import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
 28import org.mt4j.components.visibleComponents.widgets.MTOverlayContainer;
 29import org.mt4j.input.gestureAction.InertiaDragAction;
 30import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
 31import org.mt4j.util.MTColor;
 32import org.mt4j.util.math.ToolsMath;
 33import org.mt4j.util.math.Vector3D;
 34
 35
 36public class StartSwingExample extends JFrame {
 37	private static final long serialVersionUID = 1L;
 38
 39
 40
 41	public static void main(String[] args) {
 42		try {
 43            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 44        } catch (Exception e) {
 45        	e.printStackTrace();
 46        }
 47//		StartSwingExample swingFrame = 
 48			new StartSwingExample();
 49	}
 50	
 51	
 52	public StartSwingExample(){
 53		this.setTitle("Swing Test");
 54		//Should be called early before initializing opengl stuff
 55		this.setVisible(true); 
 56		
 57		final Container content = this.getContentPane();
 58		this.setLayout(new BorderLayout());
 59        
 60		//Create our mt4j applet
 61        final MTApplication instance = new TestMTApplication();
 62        instance.frame = this; //Important for registering the Windows 7 Touch input
 63        instance.init();
 64        
 65        JPanel panel1 = new JPanel(new FlowLayout());
 66        content.add(panel1, BorderLayout.WEST);
 67       
 68        JButton e2 = new JButton("Clear");
 69        e2.addActionListener(new ActionListener() {
 70			public void actionPerformed(ActionEvent arg0) {
 71				if (instance.getCurrentScene() != null){
 72					instance.invokeLater(new Runnable() {
 73						public void run() {
 74							MTComponent[] ch = instance.getCurrentScene().getCanvas().getChildren();
 75                            for (MTComponent mtComponent : ch) {
 76                                if (!(mtComponent instanceof MTOverlayContainer)) {
 77                                    mtComponent.destroy();
 78                                }
 79                            }
 80						}
 81					});
 82					
 83				}
 84			}
 85		});
 86        panel1.add(e2); // Add components to the content
 87        
 88        //Add MT4j applet
 89        JPanel pane = new JPanel(new GridLayout(0,1));
 90        pane.add(instance);
 91        content.add(pane, BorderLayout.SOUTH);
 92        
 93        /////////MEnu
 94        //So that the menu will overlap the heavyweight opengl canvas
 95        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
 96        
 97        //Create the menu bar.
 98        JMenuBar menuBar = new JMenuBar();
 99
100        //Build the first menu.
101        JMenu menu = new JMenu("Add");
102        menu.setMnemonic(KeyEvent.VK_A);
103        menuBar.add(menu);
104
105        JMenuItem addRectItem = new JMenuItem("MTRectangle", KeyEvent.VK_T);
106        addRectItem.addActionListener(new ActionListener() {
107			public void actionPerformed(ActionEvent arg0) {
108				if (instance.getCurrentScene() != null){
109					//If we want to modify the MT4j applet from the swing thread
110					//we should wrap the actions into an invokeLater runnable which 
111					//will be executed the next time the mt4j thread runs.
112					instance.invokeLater(new Runnable() {
113						public void run() {
114							MTRectangle r = new MTRectangle(instance,0,0,ToolsMath.getRandom(50, 250), ToolsMath.getRandom(50, 250));
115							r.setFillColor(new MTColor(ToolsMath.getRandom(50,255),ToolsMath.getRandom(50,255),ToolsMath.getRandom(50,255)));
116							r.addGestureListener(DragProcessor.class, new InertiaDragAction());
117							instance.getCurrentScene().getCanvas().addChild(r);
118							r.setPositionGlobal(new Vector3D(ToolsMath.getRandom(0, instance.width), ToolsMath.getRandom(0, instance.height)));
119						}
120					});
121					
122				}
123			}
124		});
125        menu.add(addRectItem);
126        
127        JMenuItem addRoundRectItem = new JMenuItem("MTRoundRectangle", KeyEvent.VK_E);
128        addRoundRectItem.addActionListener(new ActionListener() {
129			public void actionPerformed(ActionEvent arg0) {
130				if (instance.getCurrentScene() != null){
131					//If we want to modify the MT4j applet from the swing thread
132					//we should wrap the actions into an invokeLater runnable which 
133					//will be executed the next time the mt4j thread runs.
134					instance.invokeLater(new Runnable() {
135						public void run() {
136							float arc = ToolsMath.getRandom(8, 25);
137							MTRoundRectangle r = new MTRoundRectangle(instance,0,0, 0,ToolsMath.getRandom(50, 250), ToolsMath.getRandom(50, 250), arc, arc);
138							r.setFillColor(new MTColor(ToolsMath.getRandom(50,255),ToolsMath.getRandom(50,255),ToolsMath.getRandom(50,255)));
139							r.addGestureListener(DragProcessor.class, new InertiaDragAction());
140							instance.getCurrentScene().getCanvas().addChild(r);
141							r.setPositionGlobal(new Vector3D(ToolsMath.getRandom(0, instance.width), ToolsMath.getRandom(0, instance.height)));
142						}
143					});
144					
145				}
146			}
147		});
148        menu.add(addRoundRectItem);
149        
150        JMenuItem addEllItem = new JMenuItem("MTEllipse", KeyEvent.VK_E);
151        addEllItem.addActionListener(new ActionListener() {
152			public void actionPerformed(ActionEvent arg0) {
153				if (instance.getCurrentScene() != null){
154					//If we want to modify the MT4j applet from the swing thread
155					//we should wrap the actions into an invokeLater runnable which 
156					//will be executed the next time the mt4j thread runs.
157					instance.invokeLater(new Runnable() {
158						public void run() {
159							MTEllipse e = new MTEllipse(instance, new Vector3D(0,0), ToolsMath.getRandom(50, 150),ToolsMath.getRandom(50, 150));
160							e.setFillColor(new MTColor(ToolsMath.getRandom(50,255),ToolsMath.getRandom(50,255),ToolsMath.getRandom(50,255)));
161							e.addGestureListener(DragProcessor.class, new InertiaDragAction());
162							instance.getCurrentScene().getCanvas().addChild(e);
163							e.setPositionGlobal(new Vector3D(ToolsMath.getRandom(0, instance.width), ToolsMath.getRandom(0, instance.height)));
164						}
165					});
166					
167				}
168			}
169		});
170        menu.add(addEllItem);
171        
172        //Help Menu
173        JMenu helpMenu = new JMenu("Help");
174        helpMenu.setMnemonic(KeyEvent.VK_H);
175        JMenuItem aboutItem = new JMenuItem("About", KeyEvent.VK_A);
176        aboutItem.addActionListener(new ActionListener() {
177			public void actionPerformed(ActionEvent arg0) {
178				JDialog f = new SimpleAboutDialog(StartSwingExample.this);
179			    f.setVisible(true);
180			}
181		});
182        helpMenu.add(aboutItem);
183        menuBar.add(helpMenu);
184        
185        this.setJMenuBar(menuBar);
186        ////////Menu
187        
188        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
189        this.pack();   // does layout of components.
190	}
191	
192	
193	private class TestMTApplication extends MTApplication{
194		private static final long serialVersionUID = 1L;
195
196		@Override
197		public void startUp() {
198			//This causeD the craetion of a new rendrer (-> new opengl context)
199			//so opengl objects like displaylists etc would get deleted!
200//			setPreferredSize(new Dimension(MT4jSettings.getInstance().getScreenWidth(),MT4jSettings.getInstance().getScreenHeight()));
201//			pack();
202//			setResizable(false);
203			
204			this.addScene(new SwingIntegrationScene(this, "test scene"));
205		}
206	}
207	
208	
209	
210	public class SimpleAboutDialog extends JDialog {
211		private static final long serialVersionUID = 1L;
212
213		public SimpleAboutDialog(JFrame parent) {
214		    super(parent, "About", true);
215
216		    Box b = Box.createVerticalBox();
217		    b.add(Box.createGlue());
218		    b.add(new JLabel(" 	MT4j example application"));
219		    b.add(new JLabel(" 	This shows how to integrate MT4j into"));
220		    b.add(new JLabel(" 	a java swing/awt application"));
221		    b.add(new JLabel(" 	Visit www.mt4j.org"));
222		    b.add(Box.createGlue());
223		    getContentPane().add(b, BorderLayout.CENTER);
224
225		    JPanel p2 = new JPanel();
226		    JButton ok = new JButton("Ok");
227		    p2.add(ok);
228		    getContentPane().add(p2, BorderLayout.SOUTH);
229
230		    ok.addActionListener(new ActionListener() {
231		      public void actionPerformed(ActionEvent evt) {
232		        setVisible(false);
233		      }
234		    });
235		    setSize(250, 150);
236		  }
237		}
238	
239}