Advertisement
Guest User

skeleton4

a guest
Apr 14th, 2010
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. import java.awt.Canvas;
  2. import java.awt.Dimension;
  3. import java.awt.DisplayMode;
  4. import java.awt.Graphics2D;
  5. import java.awt.GraphicsDevice;
  6. import java.awt.GraphicsEnvironment;
  7. import java.awt.Rectangle;
  8. import java.awt.event.KeyAdapter;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.image.BufferStrategy;
  12.  
  13. import javax.swing.JFrame;
  14. import javax.swing.JPanel;
  15.  
  16. public class Main implements Runnable
  17. {
  18.     private int WIDTH = 1000;
  19.     private int HEIGHT = 500;
  20.     private long desiredFPS = 60;
  21.    
  22.     private String title = "Maple Story";
  23.    
  24.     private long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
  25.     private boolean running = true;
  26.    
  27.     private JFrame frame;
  28.     private Canvas canvas;
  29.     private BufferStrategy bufferStrategy;
  30.    
  31.     private GraphicsDevice graphicsDevice;
  32.     private DisplayMode originalDisplayMode;
  33.    
  34.     public Main()
  35.     {
  36.         GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
  37.         graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
  38.         originalDisplayMode = graphicsDevice.getDisplayMode();
  39.        
  40.         // create new Frame
  41.         frame = new JFrame(title);
  42.         frame.setUndecorated(true);
  43.  
  44.         graphicsDevice.setFullScreenWindow(frame);
  45.         if (graphicsDevice.isDisplayChangeSupported())
  46.         {
  47.             graphicsDevice.setDisplayMode(getBestDisplayMode(graphicsDevice));
  48.         }
  49.        
  50.         Rectangle bounds = frame.getBounds();
  51.         WIDTH = (int)bounds.getWidth();
  52.         HEIGHT = (int)bounds.getHeight();      
  53.        
  54.         // create new JPanel with specified width and height
  55.         JPanel panel = (JPanel) frame.getContentPane();
  56.         panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
  57.         panel.setLayout(null);
  58.         // create a canvas inside the JPanel with specified width and height
  59.         canvas = new Canvas();
  60.         //canvas.setBounds(0, 0, WIDTH, HEIGHT);
  61.         canvas.setBounds(bounds);
  62.         canvas.setIgnoreRepaint(true);
  63.        
  64.         panel.add(canvas);
  65.         // Add Mouse and Key Listeners
  66.         canvas.addMouseListener(new MouseControl());
  67.         canvas.addKeyListener(new KeyInputHandler());
  68.         // initialize the window
  69.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.         frame.pack();
  71.         frame.setResizable(false);
  72.         frame.setVisible(true);
  73.        
  74.         setup();
  75.         // set graphics device
  76.         canvas.createBufferStrategy(2);
  77.         bufferStrategy = canvas.getBufferStrategy();
  78.         // focus on the window
  79.         canvas.requestFocus();
  80.     }
  81.     /*
  82.      * Main game loop
  83.      */
  84.     public void run()
  85.     {
  86.         long beginLoopTime;
  87.         long endLoopTime;
  88.         long currentUpdateTime = System.nanoTime();
  89.         long lastUpdateTime;
  90.         long deltaLoop;
  91.        
  92.         while(running)
  93.         {
  94.             beginLoopTime = System.nanoTime();
  95.            
  96.             render();
  97.            
  98.             lastUpdateTime = currentUpdateTime;
  99.             currentUpdateTime = System.nanoTime();
  100.             update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
  101.            
  102.             endLoopTime = System.nanoTime();
  103.             deltaLoop = endLoopTime - beginLoopTime;
  104.            
  105.             if(deltaLoop > desiredDeltaLoop)
  106.             {
  107.                 //Do nothing. We are already late.
  108.             }
  109.             else
  110.             {
  111.                 try
  112.                 {
  113.                     Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
  114.                 }
  115.                 catch(InterruptedException e)
  116.                 {
  117.                     //Do nothing
  118.                 }
  119.             }
  120.         }
  121.     }
  122.     /*
  123.      * Method to initialise graphics that are going to be painted to the screen
  124.      */
  125.     private void render()
  126.     {
  127.         Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
  128.         g.clearRect(0, 0, WIDTH, HEIGHT);
  129.         render(g);
  130.         g.dispose();
  131.         bufferStrategy.show();
  132.     }
  133.     /*
  134.      * Initialise all of your Objects in here
  135.      */
  136.     public void setup()
  137.     {
  138.        
  139.     }
  140.     /*
  141.      * Method that handles all the movements that are supposed to happen
  142.      * So initialise all the x and y changes here
  143.      * deltaTime tells you how many nanoseconds have passed by since this function
  144.      * was last called
  145.      */
  146.     protected void update(int deltaTime)
  147.     {
  148.        
  149.     }
  150.     /*
  151.      * Method where you paint to the screen
  152.      */
  153.     protected void render(Graphics2D g)
  154.     {
  155.         /*
  156.          * START PAINTING
  157.          * START PAINTING
  158.          * START PAINTING
  159.          * START PAINTING
  160.          * START PAINTING
  161.          * START PAINTING
  162.          */
  163.  
  164.        
  165.        
  166.         /*
  167.          * STOP PAINTING
  168.          * STOP PAINTING
  169.          * STOP PAINTING
  170.          * STOP PAINTING
  171.          * STOP PAINTING
  172.          * STOP PAINTING
  173.          */
  174.     }
  175.     /*
  176.      * Keyboard Action listeners
  177.      * Key esc closes the program by default
  178.      */
  179.     private class KeyInputHandler extends KeyAdapter
  180.     {
  181.         public void keyPressed(KeyEvent e)
  182.         {
  183.             if(e.getKeyCode()==27)
  184.             {
  185.                 graphicsDevice.setDisplayMode(originalDisplayMode);
  186.                 graphicsDevice.setFullScreenWindow(null);
  187.                 System.exit(0);
  188.             }
  189.         }
  190.        
  191.         public void keyReleased(KeyEvent e)
  192.         {
  193.            
  194.         }
  195.     }
  196.     /*
  197.      * Mouse Action Listeners
  198.      */
  199.     private class MouseControl extends MouseAdapter
  200.     {
  201.        
  202.     }
  203.     /*
  204.      * public static void main
  205.      * I do not even have to explain this
  206.      */
  207.     public static void main(String [] args)
  208.     {
  209.         Main ex = new Main();
  210.         new Thread(ex).start();
  211.     }
  212.    
  213.     private static DisplayMode MODES[] = new DisplayMode[]
  214.     {
  215.         new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 16, 0),
  216.         new DisplayMode(640, 480, 8, 0)
  217.     };
  218.  
  219.     private static DisplayMode getBestDisplayMode(GraphicsDevice device)
  220.     {
  221.         for (int x = 0, xn = MODES.length; x < xn; x++)
  222.         {
  223.             DisplayMode[] modes = device.getDisplayModes();
  224.             for (int i = 0, in = modes.length; i < in; i++)
  225.             {
  226.                 if (modes[i].getWidth() == MODES[x].getWidth() && modes[i].getHeight() == MODES[x].getHeight() && modes[i].getBitDepth() == MODES[x].getBitDepth())
  227.                 {
  228.                     return MODES[x];
  229.                 }
  230.             }
  231.         }
  232.         return null;
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement