Advertisement
Guest User

ido

a guest
May 30th, 2009
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. import java.awt.AWTEvent;
  2. import java.awt.Canvas;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.GraphicsConfiguration;
  8. import java.awt.GraphicsEnvironment;
  9. import java.awt.Transparency;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.image.BufferStrategy;
  12. import java.awt.image.BufferedImage;
  13.  
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16.  
  17. public class Example extends Canvas {
  18.     static final int KEYS = 65536;
  19.  
  20.     int font_height = 10;
  21.     int width_in_chars = 80;
  22.     int height_in_chars = 40;
  23.  
  24.     BufferStrategy strategy;
  25.     BufferedImage buffer;
  26.     boolean keys[];
  27.  
  28.     public Example() {
  29.         Font font = new Font(Font.MONOSPACED, Font.PLAIN, font_height);
  30.         keys = new boolean[KEYS];
  31.  
  32.         int win_width = width_in_chars * getFontMetrics(font).charWidth(' ');
  33.         int win_height = height_in_chars * font_height;
  34.  
  35.         JFrame f = new JFrame();
  36.         JPanel p = (JPanel) f.getContentPane();
  37.  
  38.         p.setPreferredSize(new Dimension(win_width, win_width));
  39.         p.setLayout(null);
  40.  
  41.         setBounds(0, 0, win_width, win_height);
  42.         p.add(this);
  43.  
  44.         f.pack();
  45.         f.setResizable(false);
  46.         f.setTitle("Example Project");
  47.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.         f.setVisible(true);
  49.         f.setFont(font);
  50.         f.setLocationRelativeTo(null);
  51.  
  52.         createBufferStrategy(2);
  53.         strategy = getBufferStrategy();
  54.  
  55.         GraphicsConfiguration gc = GraphicsEnvironment
  56.                 .getLocalGraphicsEnvironment().getDefaultScreenDevice()
  57.                 .getDefaultConfiguration();
  58.         buffer = gc.createCompatibleImage(win_width, win_height,
  59.                 Transparency.OPAQUE);
  60.  
  61.         setIgnoreRepaint(true);
  62.         enableEvents(AWTEvent.KEY_EVENT_MASK);
  63.     }
  64.  
  65.     void mainLoop() {
  66.         while (!keys[KeyEvent.VK_ESCAPE]) {
  67.             try {
  68.                 Thread.sleep(10);
  69.             } catch (InterruptedException e) { }
  70.  
  71.             // draw on buffer
  72.             buffer.getGraphics().drawLine(0, 0, 100, 100);
  73.  
  74.             // blit buffer to screen
  75.             Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
  76.             paint(g);
  77.             g.dispose();
  78.             strategy.show();
  79.         }
  80.     }
  81.  
  82.     @Override
  83.     public void paint(Graphics g) {
  84.         g.drawImage(buffer, 0, 0, this);
  85.     }
  86.  
  87.     @Override
  88.     public void processKeyEvent(KeyEvent e) {
  89.         keys[e.getKeyCode()] = e.getID() == KeyEvent.KEY_PRESSED;
  90.     }
  91.  
  92.     public static void main(String[] args) {
  93.         new Example().mainLoop();
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement