Package edu.ups.gamedev.examples.four

Source Code of edu.ups.gamedev.examples.four.SwingExample

package edu.ups.gamedev.examples.four;

import java.awt.Color;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.input.MouseInput;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.SceneElement;
import com.jme.scene.state.LightState;
import com.jmex.awt.swingui.JMEDesktop;

public class SwingExample extends SimpleGame {
  Node guiNode;

  /**
   * Sets up and runs this example. It first asks the user if it should run as a
   * client or as a server. Then it displays the jMonkey option panel before
   * launching the actual program.
   *
   * @param args  the array containing any command-line arguments
   */
  public static void main(String[] args) {   
    //get a URL for the image to use instead of jMonkey's default image
    URL pic;
    try {
      pic = new File("resources/tank.jpg").toURI().toURL(); //File's toURL method is deprecated, so we must first convert it to a URI and then to a URL
    } catch (MalformedURLException e) {
      System.err.println("Couldn't find image:");
      e.printStackTrace();
      pic = null;
    }
   
    //create and run the game
    final SwingExample app = new SwingExample(); //create an instance of our class
    app.setDialogBehaviour(AbstractGame.ALWAYS_SHOW_PROPS_DIALOG, pic); //make sure the properities dialog shows up and tell it to use our image
    app.start();
  }

  /**
   * Initializes the "game" by creating the <code>JMEDesktop</code> object and
   * adding a <code>JTextArea</code> and <code>JButton</code> to the <code>
   * JMEDesktop</code>. This method is automatically called by SimpleGame to
   * give us a chance to setup our custom environment.
   *
   * @see SimpleGame
   */
  @Override
  protected void simpleInitGame() {
    //create a node for our GUI and change which render queue it's in
    guiNode = new Node("gui node");
    guiNode.setRenderQueueMode(Renderer.QUEUE_ORTHO); //this is make sure it is drawn correctly
   
    //create the actual desktop object and attach it to our node
    final JMEDesktop gui = new JMEDesktop("GUI", 500, 400, input);
    guiNode.attachChild(gui);
   
    //set the desktop's location
    gui.getLocalTranslation().set(display.getWidth() / 2 - 30, display.getHeight() / 2 + 50, 0);
   
    //Swing stuff can't happen in the jMonkey thread, so we use SwingUtilities's
    //invokeLater method to create a new thread in which all the Swing stuff is
    //setup. It's a round-about way of doing things, but it's necessary.
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        gui.getJDesktop().setBackground(new Color(0f, .6f, .2f, .8f)); //set the background color
       
        //create our GUI components
        JTextArea txtChat = new JTextArea("Hello World", 4, 20);
        JButton btnSubmit = new JButton("Submit");
       
        //add our components to the desktop
        gui.getJDesktop().add(txtChat);
        gui.getJDesktop().add(btnSubmit);
       
        //set their locations and sizes
        txtChat.setLocation(200, 200);
        btnSubmit.setLocation(200, 275);
        txtChat.setSize(txtChat.getPreferredSize());
        btnSubmit.setSize(btnSubmit.getPreferredSize());
      }
    });
   
    guiNode.setCullMode(SceneElement.CULL_NEVER); //make sure our GUI is always shown, regardless of where the camera is looking
    guiNode.setLightCombineMode(LightState.OFF); //turn off lighting so that it's not too dark to see our GUI
   
    //update our Node's states so that our changes are made right away
    guiNode.updateRenderState();
    guiNode.updateGeometricState(0, true);
   
    MouseInput.get().setCursorVisible(true); //make the mouse visible
  }
 
  /**
   * Allows custom updating at each render pass. This method is designed to be overriden
   * and is provided by SimpleGame so that the subclass can do custom updates on each
   * frame.
   */
  @Override
  protected void simpleUpdate() {
    display.getRenderer().draw(guiNode); //draw our GUI
    //note: this won't happen automatically because we never added our Node
    //to the scene graph. If we didn't make this call, it would never get
    //displayed.
  }
}
TOP

Related Classes of edu.ups.gamedev.examples.four.SwingExample

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.