/examples/basic/javaGUI/StartSwingExample.java

http://mt4j.googlecode.com/ · Java · 239 lines · 175 code · 40 blank · 24 comment · 10 complexity · 1bfcffb69c9421fe315fd9e60b641cf3 MD5 · raw file

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