Advertisement
Guest User

ido

a guest
May 30th, 2009
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.awt.AWTEvent;
  2. import java.awt.Font;
  3. import java.awt.Graphics2D;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.image.BufferStrategy;
  6.  
  7. import javax.swing.JFrame;
  8.  
  9. public class Example extends JFrame {
  10.     final int FRAME_TIME = 1000 / 60; // ~60fps
  11.     int font_height = 10;
  12.     int width_in_chars = 80;
  13.  
  14.     int height_in_chars = 40;
  15.     BufferStrategy strategy;
  16.     boolean keys[] = new boolean[65536];
  17.     boolean mouse[] = new boolean[8];
  18.     int mouseX = 0;
  19.  
  20.     int mouseY = 0;
  21.  
  22.     public Example() {
  23.         Font font = new Font(Font.MONOSPACED, Font.PLAIN, font_height);
  24.         int win_width = width_in_chars * getFontMetrics(font).charWidth(' ');
  25.         int win_height = height_in_chars * font_height;
  26.  
  27.         setTitle("Example Project");
  28.         setResizable(false);
  29.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.         setUndecorated(true);
  31.         setFont(font);
  32.         setSize(win_width, win_height);
  33.         setLocationRelativeTo(null);
  34.         setIgnoreRepaint(true);
  35.         setVisible(true);
  36.  
  37.         enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK
  38.                 | AWTEvent.MOUSE_MOTION_EVENT_MASK);
  39.         createBufferStrategy(2);
  40.  
  41.         strategy = getBufferStrategy();
  42.     }
  43.  
  44.     void mainLoop() {
  45.         long lastFrame = System.nanoTime();
  46.  
  47.         while (!keys[KeyEvent.VK_ESCAPE]) {
  48.             drawToBuffer();
  49.             strategy.show();
  50.  
  51.             do
  52.                 try {
  53.                     Thread.sleep(FRAME_TIME / 2);
  54.                 } catch (InterruptedException e) { }
  55.             while (System.nanoTime() < lastFrame + FRAME_TIME * 1000000);
  56.             lastFrame = System.nanoTime();
  57.         }
  58.         System.exit(0);
  59.     }
  60.  
  61.     private void drawToBuffer() {
  62.         Graphics2D buffer = (Graphics2D) strategy.getDrawGraphics();
  63.         buffer.drawLine(0, 0, 100, 100);
  64.         buffer.dispose();
  65.     }
  66.    
  67.     @Override
  68.     public void processKeyEvent(KeyEvent e) {
  69.         keys[e.getKeyCode()] = e.getID() == KeyEvent.KEY_PRESSED;
  70.     }
  71.    
  72.     public static void main(String[] args) {
  73.         new Example().mainLoop();
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement