Advertisement
Guest User

skeleton2

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