Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.DisplayMode;
- import java.awt.Graphics2D;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.Rectangle;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.image.BufferStrategy;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Main implements Runnable
- {
- private int WIDTH = 1000;
- private int HEIGHT = 500;
- private long desiredFPS = 60;
- private String title = "Maple Story";
- private long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
- private boolean running = true;
- private JFrame frame;
- private Canvas canvas;
- private BufferStrategy bufferStrategy;
- private GraphicsDevice graphicsDevice;
- private DisplayMode originalDisplayMode;
- public Main()
- {
- GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
- graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
- originalDisplayMode = graphicsDevice.getDisplayMode();
- // create new Frame
- frame = new JFrame(title);
- frame.setUndecorated(true);
- graphicsDevice.setFullScreenWindow(frame);
- if (graphicsDevice.isDisplayChangeSupported())
- {
- graphicsDevice.setDisplayMode(getBestDisplayMode(graphicsDevice));
- }
- Rectangle bounds = frame.getBounds();
- WIDTH = (int)bounds.getWidth();
- HEIGHT = (int)bounds.getHeight();
- // create new JPanel with specified width and height
- JPanel panel = (JPanel) frame.getContentPane();
- panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
- panel.setLayout(null);
- // create a canvas inside the JPanel with specified width and height
- canvas = new Canvas();
- //canvas.setBounds(0, 0, WIDTH, HEIGHT);
- canvas.setBounds(bounds);
- canvas.setIgnoreRepaint(true);
- panel.add(canvas);
- // Add Mouse and Key Listeners
- canvas.addMouseListener(new MouseControl());
- canvas.addKeyListener(new KeyInputHandler());
- // initialize the window
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.pack();
- frame.setResizable(false);
- frame.setVisible(true);
- setup();
- // set graphics device
- canvas.createBufferStrategy(2);
- bufferStrategy = canvas.getBufferStrategy();
- // focus on the window
- canvas.requestFocus();
- }
- /*
- * Main game loop
- */
- public void run()
- {
- long beginLoopTime;
- long endLoopTime;
- long currentUpdateTime = System.nanoTime();
- long lastUpdateTime;
- long deltaLoop;
- while(running)
- {
- beginLoopTime = System.nanoTime();
- render();
- lastUpdateTime = currentUpdateTime;
- currentUpdateTime = System.nanoTime();
- update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
- endLoopTime = System.nanoTime();
- deltaLoop = endLoopTime - beginLoopTime;
- if(deltaLoop > desiredDeltaLoop)
- {
- //Do nothing. We are already late.
- }
- else
- {
- try
- {
- Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
- }
- catch(InterruptedException e)
- {
- //Do nothing
- }
- }
- }
- }
- /*
- * Method to initialise graphics that are going to be painted to the screen
- */
- private void render()
- {
- Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
- g.clearRect(0, 0, WIDTH, HEIGHT);
- render(g);
- g.dispose();
- bufferStrategy.show();
- }
- /*
- * Initialise all of your Objects in here
- */
- public void setup()
- {
- }
- /*
- * Method that handles all the movements that are supposed to happen
- * So initialise all the x and y changes here
- * deltaTime tells you how many nanoseconds have passed by since this function
- * was last called
- */
- protected void update(int deltaTime)
- {
- }
- /*
- * Method where you paint to the screen
- */
- protected void render(Graphics2D g)
- {
- /*
- * START PAINTING
- * START PAINTING
- * START PAINTING
- * START PAINTING
- * START PAINTING
- * START PAINTING
- */
- /*
- * STOP PAINTING
- * STOP PAINTING
- * STOP PAINTING
- * STOP PAINTING
- * STOP PAINTING
- * STOP PAINTING
- */
- }
- /*
- * Keyboard Action listeners
- * Key esc closes the program by default
- */
- private class KeyInputHandler extends KeyAdapter
- {
- public void keyPressed(KeyEvent e)
- {
- if(e.getKeyCode()==27)
- {
- graphicsDevice.setDisplayMode(originalDisplayMode);
- graphicsDevice.setFullScreenWindow(null);
- System.exit(0);
- }
- }
- public void keyReleased(KeyEvent e)
- {
- }
- }
- /*
- * Mouse Action Listeners
- */
- private class MouseControl extends MouseAdapter
- {
- }
- /*
- * public static void main
- * I do not even have to explain this
- */
- public static void main(String [] args)
- {
- Main ex = new Main();
- new Thread(ex).start();
- }
- private static DisplayMode MODES[] = new DisplayMode[]
- {
- new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 16, 0),
- new DisplayMode(640, 480, 8, 0)
- };
- private static DisplayMode getBestDisplayMode(GraphicsDevice device)
- {
- for (int x = 0, xn = MODES.length; x < xn; x++)
- {
- DisplayMode[] modes = device.getDisplayModes();
- for (int i = 0, in = modes.length; i < in; i++)
- {
- if (modes[i].getWidth() == MODES[x].getWidth() && modes[i].getHeight() == MODES[x].getHeight() && modes[i].getBitDepth() == MODES[x].getBitDepth())
- {
- return MODES[x];
- }
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement