Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.awt.Graphics;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import java.util.Random;
- import javax.swing.JApplet;
- public class Main extends JApplet implements Runnable {
- private static final long serialVersionUID = 1L;
- public static final int GWIDTH = 320;
- public static final int GHEIGHT = 180;
- boolean running = false;
- Thread thread;
- Canvas canvas;
- Input input;
- Random rand = new Random();
- BufferedImage image = new BufferedImage(GWIDTH, GHEIGHT, BufferedImage.TYPE_INT_RGB);
- int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
- public void initGame() {
- input = new Input();
- canvas.addKeyListener(input);
- }
- public void update() {
- }
- public void draw() {
- BufferStrategy bs = canvas.getBufferStrategy();
- if (bs == null) {
- canvas.createBufferStrategy(3);
- return;
- }
- clear();
- // Render stuff
- Graphics g = bs.getDrawGraphics();
- g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- g.dispose();
- bs.show();
- }
- public void clear() {
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = 0;
- }
- }
- public void run() {
- long lastTime = System.currentTimeMillis();
- final double ns = 1000.0 / 60.0;
- double delta = 0;
- long nowTime;
- int frames = 0, updates = 0;
- long timer = System.currentTimeMillis();
- while (running) {
- nowTime = System.currentTimeMillis();
- delta += (nowTime - lastTime) / ns;
- lastTime = nowTime;
- while (delta >= 1) {
- update();
- updates++;
- delta--;
- }
- draw();
- frames++;
- if (System.currentTimeMillis() - timer > 1000) {
- timer = System.currentTimeMillis();
- System.out.println("FPS: " + frames + ", UPS: " + updates);
- frames = 0;
- updates = 0;
- }
- }
- }
- /**
- * Applet stuff
- */
- public void init() {
- canvas = new Canvas();
- setLayout(new BorderLayout());
- add(canvas);
- this.setSize(640, 360);
- }
- public synchronized void start() {
- running = true;
- thread = new Thread(this);
- initGame();
- thread.start();
- }
- public synchronized void stop() {
- running = false;
- try {
- thread.join();
- } catch(Exception ex) {
- ex.printStackTrace();
- System.exit(0);
- }
- }
- class Input implements KeyListener {
- public boolean[] key = new boolean[65536];
- public void keyPressed(KeyEvent e) {
- key[e.getKeyCode()] = true;
- if (e.getKeyCode() >= 65536) {
- System.out.println("Caught a keypress with a keycode >=65536!");
- }
- }
- public void keyReleased(KeyEvent e) {
- key[e.getKeyCode()] = false;
- if (e.getKeyCode() >= 65536) {
- System.out.println("Caught a keypress with a keycode >=65536!");
- }
- }
- public void keyTyped(KeyEvent e) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement