Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eu.inmensia.learn;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.image.BufferStrategy;
- import javax.swing.JFrame;
- public class Client extends Canvas implements Runnable {
- private static final long serialVersionUID = 1L;
- public static final int WIDTH = 300;
- public static final int HEIGHT = WIDTH / 16 * 9;
- public static final byte SCALE = 3;
- private Thread thread;
- private JFrame frame = new JFrame();
- private boolean running = false;
- public Client() {
- Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
- setPreferredSize(size);
- }
- public synchronized void start() {
- running = true;
- thread = new Thread(this, "display");
- thread.start(); // starts the thread
- }
- public synchronized void stop() {
- running = false;
- try {
- thread.join(); // ends the thread
- } catch(InterruptedException e){ e.printStackTrace(); }
- }
- public void run() {
- while(running){
- tick();
- render();
- }
- }
- public void tick() {
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if(bs == null){
- createBufferStrategy(3);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- {
- // background
- g.setColor(Color.BLACK);
- g.drawRect(0, 0, getWidth(), getHeight());
- }
- g.dispose();
- bs.show();
- }
- public static void main(String[] args) {
- Client client = new Client();
- client.frame.setResizable(false);
- client.frame.setTitle("Learning");
- client.frame.add(client);
- client.frame.pack();
- client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- client.frame.setLocationRelativeTo(null);
- client.frame.setVisible(true);
- client.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment