Advertisement
Luninariel

289 - Swing Easel

Apr 8th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.36 KB | None | 0 0
  1. //
  2. //  SwingEasel.java
  3. //  SwingEasel
  4. //
  5. //  Created by Philip Rhodes on 8/3/05.
  6. //  Copyright (c) 2005 Philip J Rhodes. All rights reserved.
  7. //
  8. //  For information on setting Java configuration information, including
  9. //  setting Java properties, refer to the documentation at
  10. //      http://developer.apple.com/techpubs/java/java.html
  11. //
  12.  
  13. import java.util.Locale;
  14. import java.util.ResourceBundle;
  15.  
  16. import java.awt.*;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.KeyEvent;
  19.  
  20. import javax.swing.*;
  21.  
  22.  
  23.  
  24. public class SwingEasel extends JFrame {
  25.  
  26.     protected Action newAction, openAction, closeAction, saveAction, saveAsAction, quitAction,
  27.                      undoAction, cutAction, copyAction, pasteAction, clearAction, selectAllAction;
  28.     static final JMenuBar mainMenuBar = new JMenuBar();
  29.     protected JMenu fileMenu, editMenu;
  30.    
  31.     public SwingEasel() {
  32.        
  33.         super("Easel");
  34.        
  35.         this.getContentPane().setLayout(null);
  36.         this.setBackground(Color.gray);
  37.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.      
  39.         createActions();    
  40.         addMenus();
  41.       setSize(1200, 800);  // Set the window size
  42.  
  43.       Shape s1 = new Sphere(300, 16, 16);
  44.       Shape s2 = new Cube(50);
  45.       Shape s3 = new Cylinder(100, 300, 15, 5);
  46.       Shape s4 = new Cube(200);
  47.       Shape s5 = new Tetrahedron(100);
  48.       Shape s6 = new Sphere(100, 4, 2);
  49.       Shape s7 = new Sphere(50, 5, 3);
  50.      
  51.       s1.setXRotation(45 * Math.PI / 180);
  52.      
  53.       s2.setTranslation(500, 0, 0);
  54.       s2.setZRotation(45 * Math.PI / 180);
  55.  
  56.       s3.setTranslation(0, -150, 0);
  57.       s3.setZRotation(10 * Math.PI / 180);
  58.      
  59.       s4.setTranslation(-100, -100, -100);
  60.       s4.setXRotation(10 * Math.PI / 180);
  61.  
  62.       s5.setTranslation(0, 50, 0);
  63.       s5.setXRotation(10 * Math.PI / 180);
  64.      
  65.       s6.setXRotation(10 * Math.PI / 180);
  66.      
  67.       s7.setXRotation(10 * Math.PI / 180);
  68.      
  69.       World w = new World();
  70.      
  71.      
  72.       w.addShape(s1, -200, 0, 0);
  73.       w.addShape(s2, -200, 0, 0);
  74.       w.addShape(s3, 375, 200, 0);
  75.       w.addShape(s4, 250, -200, 0);
  76.       w.addShape(s5, 0, -400, 0);
  77.       w.addShape(s6, 150, 200, 0);
  78.       w.addShape(s7, -430, 300, 0);
  79.      
  80.       // Make a 1024x768 Renderer with fps display
  81.       Renderer r = new SimpleRenderer(w, 1024, 768, true);
  82.      
  83.       this.getContentPane().add(r);
  84.       r.setLocation(5, 5);
  85.              
  86.       setVisible(true); // Call this BEFORE calling r.init()
  87.       r.init();
  88.          
  89.      
  90.       double theta=0;  
  91.      
  92.       while(true){
  93.          
  94.          s1.setYRotation(theta);
  95.          
  96.          s2.setYRotation(5 * theta);
  97.          
  98.          s3.setXRotation(theta);
  99.          
  100.          s4.setYRotation(theta);
  101.          
  102.          s5.setYRotation(theta);
  103.          
  104.          s6.setYRotation(theta);
  105.          
  106.          s7.setYRotation(theta);
  107.          
  108.          r.render();
  109.          
  110.          theta += 0.01;
  111.       }
  112.      
  113.     }
  114.  
  115.  
  116.     public void createActions() {
  117.         int shortcutKeyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  118.  
  119.         //Create actions that can be used by menus, buttons, toolbars, etc.
  120.         newAction = new newActionClass( "New",
  121.                                             KeyStroke.getKeyStroke(KeyEvent.VK_N, shortcutKeyMask) );
  122.         openAction = new openActionClass( "Open",
  123.                                             KeyStroke.getKeyStroke(KeyEvent.VK_O, shortcutKeyMask) );
  124.         closeAction = new closeActionClass( "Close",
  125.                                             KeyStroke.getKeyStroke(KeyEvent.VK_W, shortcutKeyMask) );
  126.         saveAction = new saveActionClass("Save",
  127.                                             KeyStroke.getKeyStroke(KeyEvent.VK_S, shortcutKeyMask) );
  128.         saveAsAction = new saveAsActionClass( "Save As" );
  129.      
  130.         quitAction = new quitActionClass("Quit",
  131.                                             KeyStroke.getKeyStroke(KeyEvent.VK_Q, shortcutKeyMask) );
  132.  
  133.         undoAction = new undoActionClass( "Undo",
  134.                                             KeyStroke.getKeyStroke(KeyEvent.VK_Z, shortcutKeyMask) );
  135.         cutAction = new cutActionClass( "Cut",
  136.                                             KeyStroke.getKeyStroke(KeyEvent.VK_X, shortcutKeyMask) );
  137.         copyAction = new copyActionClass( "Copy",
  138.                                             KeyStroke.getKeyStroke(KeyEvent.VK_C, shortcutKeyMask) );
  139.         pasteAction = new pasteActionClass( "Paste",
  140.                                             KeyStroke.getKeyStroke(KeyEvent.VK_V, shortcutKeyMask) );
  141.         clearAction = new clearActionClass( "Clear" );
  142.         selectAllAction = new selectAllActionClass( "Select All",
  143.                                             KeyStroke.getKeyStroke(KeyEvent.VK_A, shortcutKeyMask) );
  144.     }
  145.    
  146.     public void addMenus() {
  147.  
  148.         fileMenu = new JMenu("File");
  149.         fileMenu.add(new JMenuItem(newAction));
  150.         fileMenu.add(new JMenuItem(openAction));
  151.         fileMenu.add(new JMenuItem(closeAction));
  152.         fileMenu.add(new JMenuItem(saveAction));
  153.         fileMenu.add(new JMenuItem(saveAsAction));
  154.       fileMenu.add(new JMenuItem(quitAction));
  155.         mainMenuBar.add(fileMenu);
  156.  
  157.         editMenu = new JMenu("Edit");
  158.         editMenu.add(new JMenuItem(undoAction));
  159.         editMenu.addSeparator();
  160.         editMenu.add(new JMenuItem(cutAction));
  161.         editMenu.add(new JMenuItem(copyAction));
  162.         editMenu.add(new JMenuItem(pasteAction));
  163.         editMenu.add(new JMenuItem(clearAction));
  164.         editMenu.addSeparator();
  165.         editMenu.add(new JMenuItem(selectAllAction));
  166.         mainMenuBar.add(editMenu);
  167.  
  168.         setJMenuBar (mainMenuBar);
  169.     }
  170.  
  171.    
  172.  
  173.    
  174.     public class newActionClass extends AbstractAction {
  175.         public newActionClass(String text, KeyStroke shortcut) {
  176.             super(text);
  177.             putValue(ACCELERATOR_KEY, shortcut);
  178.         }
  179.         public void actionPerformed(ActionEvent e) {
  180.             System.out.println("New...");
  181.         }
  182.     }
  183.  
  184.     public class openActionClass extends AbstractAction {
  185.         public openActionClass(String text, KeyStroke shortcut) {
  186.             super(text);
  187.             putValue(ACCELERATOR_KEY, shortcut);
  188.         }
  189.         public void actionPerformed(ActionEvent e) {
  190.             System.out.println("Open...");
  191.         }
  192.     }
  193.    
  194.     public class closeActionClass extends AbstractAction {
  195.         public closeActionClass(String text, KeyStroke shortcut) {
  196.             super(text);
  197.             putValue(ACCELERATOR_KEY, shortcut);
  198.         }
  199.         public void actionPerformed(ActionEvent e) {
  200.             System.out.println("Close...");
  201.         }
  202.     }
  203.    
  204.     public class saveActionClass extends AbstractAction {
  205.         public saveActionClass(String text, KeyStroke shortcut) {
  206.             super(text);
  207.             putValue(ACCELERATOR_KEY, shortcut);
  208.         }
  209.         public void actionPerformed(ActionEvent e) {
  210.             System.out.println("Save...");
  211.         }
  212.     }
  213.    
  214.     public class saveAsActionClass extends AbstractAction {
  215.         public saveAsActionClass(String text) {
  216.             super(text);
  217.         }
  218.         public void actionPerformed(ActionEvent e) {
  219.             System.out.println("Save As...");
  220.         }
  221.     }
  222.  
  223.     public class quitActionClass extends AbstractAction {
  224.    
  225.         public quitActionClass(String text, KeyStroke shortcut) {
  226.             super(text);
  227.          putValue(ACCELERATOR_KEY, shortcut);
  228.         }
  229.         public void actionPerformed(ActionEvent e) {
  230.            
  231.          System.exit(0);
  232.         }
  233.     }
  234.  
  235.    
  236.     public class undoActionClass extends AbstractAction {
  237.         public undoActionClass(String text, KeyStroke shortcut) {
  238.             super(text);
  239.             putValue(ACCELERATOR_KEY, shortcut);
  240.         }
  241.         public void actionPerformed(ActionEvent e) {
  242.             System.out.println("Undo...");
  243.         }
  244.     }
  245.    
  246.     public class cutActionClass extends AbstractAction {
  247.         public cutActionClass(String text, KeyStroke shortcut) {
  248.             super(text);
  249.             putValue(ACCELERATOR_KEY, shortcut);
  250.         }
  251.         public void actionPerformed(ActionEvent e) {
  252.             System.out.println("Cut...");
  253.         }
  254.     }
  255.    
  256.     public class copyActionClass extends AbstractAction {
  257.         public copyActionClass(String text, KeyStroke shortcut) {
  258.             super(text);
  259.             putValue(ACCELERATOR_KEY, shortcut);
  260.         }
  261.         public void actionPerformed(ActionEvent e) {
  262.             System.out.println("Copy...");
  263.         }
  264.     }
  265.    
  266.     public class pasteActionClass extends AbstractAction {
  267.         public pasteActionClass(String text, KeyStroke shortcut) {
  268.             super(text);
  269.             putValue(ACCELERATOR_KEY, shortcut);
  270.         }
  271.         public void actionPerformed(ActionEvent e) {
  272.             System.out.println("Paste...");
  273.         }
  274.     }
  275.    
  276.     public class clearActionClass extends AbstractAction {
  277.         public clearActionClass(String text) {
  278.             super(text);
  279.         }
  280.         public void actionPerformed(ActionEvent e) {
  281.             System.out.println("Clear...");
  282.         }
  283.     }
  284.    
  285.     public class selectAllActionClass extends AbstractAction {
  286.         public selectAllActionClass(String text, KeyStroke shortcut) {
  287.             super(text);
  288.             putValue(ACCELERATOR_KEY, shortcut);
  289.         }
  290.         public void actionPerformed(ActionEvent e) {
  291.             System.out.println("Select All...");
  292.         }
  293.     }
  294.    
  295.      public static void main(String args[]) {
  296.         new SwingEasel();
  297.      }
  298.  
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement