Guest User

Untitled

a guest
May 13th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.56 KB | None | 0 0
  1. package com.gej.core;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.DisplayMode;
  5. import java.awt.GraphicsDevice;
  6. import java.awt.GraphicsEnvironment;
  7. import java.awt.Robot;
  8. import java.awt.Toolkit;
  9. import java.awt.Window;
  10.  
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15.  
  16. import javax.swing.JFrame;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JRootPane;
  19. import javax.swing.Timer;
  20.  
  21. /**
  22.  * This class acts as a configured JFrame which is equipped with special
  23.  * abilities. It can be configured to start as a full screen application, or a
  24.  * windowed application. It is invoked like this.<br>
  25.  *
  26.  * <pre>
  27.  * GWindow.setup(Game game);
  28.  * </pre>
  29.  *
  30.  * where the values provided are the default ones. The constructors are
  31.  * discussed again at their respective positions.<br>
  32.  * <br>
  33.  * It's other features are..<br>
  34.  * <br>
  35.  * <ul>
  36.  * <li>FullScreen, using the FullScreen Exclusive Mode by Sun.</li>
  37.  * <li>Uses the Java Metal theme's Title bar</li>
  38.  * <li>Monitors and changes the window size, title, fullscreen state by scanning
  39.  * the Global class
  40.  * <li>Confirmation on the close button</li>
  41.  * <li>Also includes a fix for repaint issue on older machines</li>
  42.  * </ul>
  43.  *
  44.  * @author Sri Harsha Chilakapati
  45.  */
  46. public final class GWindow extends JFrame implements ActionListener {
  47.  
  48.     /**
  49.      *
  50.      */
  51.     private static final long serialVersionUID = 449311978071928834L;
  52.  
  53.     /** The timer object we use to re-change the title, width and height */
  54.     Timer timer = null;
  55.     /** The Game object (passed as an argument) */
  56.     Game game = null;
  57.    
  58.     private static GWindow window = null;
  59.  
  60.     /** The width of resolution of the screen */
  61.     public static int RESOLUTION_X = Global.WIDTH;
  62.     /** The height of resolution of the screen */
  63.     public static int RESOLUTION_Y = Global.HEIGHT;
  64.  
  65.     /** The screen device */
  66.     GraphicsDevice device = null;
  67.  
  68.     boolean fullscreen = false;
  69.    
  70.     /**
  71.      * This method fixes the repaint issue (flickering) on old machines with
  72.      * latest JRE. It is called automatically by the timer
  73.      */
  74.     public final void repaintFix(){
  75.         setIgnoreRepaint(true);
  76.     }
  77.  
  78.     /**
  79.      * This is our default constructor. Create instances by using the setup()
  80.      * method. The arguments to be passed except the Game are just the default
  81.      * one's. They can be changed during the game play. (use global.HEIGHT =
  82.      * 240;)
  83.      *
  84.      * @param game The Game object to be displayed
  85.      * @param title The default title (also used on the task bar)
  86.      * @param width The default window width
  87.      * @param height The default window height
  88.      */
  89.     private GWindow(Game game, String title, int width, int height) {
  90.         // Configure and create the title bar
  91.         super(title);
  92.         Global.WEB_MODE = false;
  93.         setUndecorated(true);
  94.         setResizable(false);
  95.         setTitle(title);
  96.         setSize(width, height);
  97.         // Center the window on the screen
  98.         setLocationRelativeTo(null);
  99.         setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  100.         getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  101.         // Set and add the game object
  102.         this.game = game;
  103.         add(game);
  104.         game.setSize(getSize());
  105.         // Fix the repaint issue on some older machines
  106.         repaintFix();
  107.         device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  108.         // Create and start the timer
  109.         timer = new Timer(500, this);
  110.         timer.start();
  111.         addWindowListener(new WindowAdapter() {
  112.             @Override
  113.             public void windowClosing(WindowEvent e){
  114.                 int retVal = JOptionPane.showOptionDialog(null, "Are you sure want to exit?", Global.TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] {
  115.                         "Sure, close it!", "Sorry! Go Back!" }, "Sorry! Go Back!");
  116.                 if (retVal == JOptionPane.YES_OPTION) {
  117.                     closeWindow();
  118.                 } else {
  119.                     // Do nothing
  120.                 }
  121.             }
  122.         });
  123.         setVisible(true);
  124.         this.game.start();
  125.     }
  126.  
  127.     /**
  128.      * Called by the timer each second to update the window state, such as
  129.      * title, window size, resolution in full-screen state etc.,
  130.      */
  131.     public final void actionPerformed(ActionEvent e){
  132.         // Set it's properties and center the window on screen
  133.         setTitle(Global.TITLE);
  134.         if (!Global.FULLSCREEN) {
  135.             setLocationRelativeTo(null);
  136.             setSize(Global.WIDTH, Global.HEIGHT);
  137.         }
  138.         // Check and change the full screen state
  139.         setFullScreen(Global.FULLSCREEN);
  140.         // Resolution
  141.         DisplayMode mode = device.getDisplayMode();
  142.         RESOLUTION_X = mode.getWidth();
  143.         RESOLUTION_Y = mode.getHeight();
  144.     }
  145.  
  146.     /**
  147.      * This method changes the state of full screen. To change the full screen
  148.      * state from the game use the global object.
  149.      *
  150.      * <pre>
  151.      * Global.FULLSCREEN = true;
  152.      * </pre>
  153.      *
  154.      * @param bool The boolean value which is used to switch full screen state.
  155.      */
  156.     public final void setFullScreen(boolean bool){
  157.         // If true, switch to FullScreen
  158.         if (bool == true && fullscreen == false) {
  159.             getRootPane().setWindowDecorationStyle(JRootPane.NONE);
  160.             device.setFullScreenWindow((Window) this);
  161.             Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  162.             if (device.isDisplayChangeSupported()) {
  163.                 // Get the available display modes
  164.                 DisplayMode[] modes = device.getDisplayModes();
  165.                 // Cycle and set the best display mode
  166.                 for (DisplayMode mode : modes) {
  167.                     if (mode.getWidth() == Global.WIDTH
  168.                             && mode.getHeight() == Global.HEIGHT
  169.                             && (mode.getBitDepth() == 32 || mode.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI)) {
  170.                         device.setDisplayMode(mode);
  171.                     }
  172.                 }
  173.             }
  174.             // Center the mouse using the robot class
  175.             try {
  176.                 Robot robot = new Robot();
  177.                 robot.mouseMove(dim.width / 4, dim.height / 4);
  178.                 fullscreen = true;
  179.             } catch (Exception e) {
  180.             }
  181.         } else if (bool == false && fullscreen == true) {
  182.             // else change to windowed mode
  183.             getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  184.             device.setFullScreenWindow(null);
  185.             fullscreen = false;
  186.         }
  187.     }
  188.  
  189.     /**
  190.      * Creates a new GWindow instance and packs a game into it.
  191.      *
  192.      * @param game The game to be packed.
  193.      * @return The GWindow instance.
  194.      */
  195.     public static final GWindow setup(Game game){
  196.         window = new GWindow(game, Global.TITLE, Global.WIDTH, Global.HEIGHT);
  197.         return window;
  198.     }
  199.    
  200.     /**
  201.      * Restores the windowed mode and closes this window.
  202.      * Terminates the window if not in WEBMODE
  203.      */
  204.     public static void closeWindow(){
  205.         Global.FULLSCREEN = false;
  206.         try {
  207.             window.timer.stop();
  208.             window.setFullScreen(false);
  209.             window.dispose();
  210.             if (!Global.WEB_MODE){
  211.                 // terminate the VM
  212.                 System.exit(0);
  213.             }
  214.         } catch (Exception e){}
  215.     }
  216.  
  217. }
Add Comment
Please, Sign In to add comment