Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.AWTEvent;
- import java.awt.Font;
- import java.awt.Graphics2D;
- import java.awt.event.KeyEvent;
- import java.awt.image.BufferStrategy;
- import javax.swing.JFrame;
- public class Example extends JFrame {
- final int FRAME_TIME = 1000 / 60; // ~60fps
- int font_height = 10;
- int width_in_chars = 80;
- int height_in_chars = 40;
- BufferStrategy strategy;
- boolean keys[] = new boolean[65536];
- boolean mouse[] = new boolean[8];
- int mouseX = 0;
- int mouseY = 0;
- public Example() {
- Font font = new Font(Font.MONOSPACED, Font.PLAIN, font_height);
- int win_width = width_in_chars * getFontMetrics(font).charWidth(' ');
- int win_height = height_in_chars * font_height;
- setTitle("Example Project");
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setUndecorated(true);
- setFont(font);
- setSize(win_width, win_height);
- setLocationRelativeTo(null);
- setIgnoreRepaint(true);
- setVisible(true);
- enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK
- | AWTEvent.MOUSE_MOTION_EVENT_MASK);
- createBufferStrategy(2);
- strategy = getBufferStrategy();
- }
- void mainLoop() {
- long lastFrame = System.nanoTime();
- while (!keys[KeyEvent.VK_ESCAPE]) {
- drawToBuffer();
- strategy.show();
- do
- try {
- Thread.sleep(FRAME_TIME / 2);
- } catch (InterruptedException e) { }
- while (System.nanoTime() < lastFrame + FRAME_TIME * 1000000);
- lastFrame = System.nanoTime();
- }
- System.exit(0);
- }
- private void drawToBuffer() {
- Graphics2D buffer = (Graphics2D) strategy.getDrawGraphics();
- buffer.drawLine(0, 0, 100, 100);
- buffer.dispose();
- }
- @Override
- public void processKeyEvent(KeyEvent e) {
- keys[e.getKeyCode()] = e.getID() == KeyEvent.KEY_PRESSED;
- }
- public static void main(String[] args) {
- new Example().mainLoop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement