Advertisement
Guest User

Untitled

a guest
Jun 7th, 2012
10,731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Canvas;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferStrategy;
  7.  
  8. import javax.swing.JFrame;
  9.  
  10. public class Game extends Canvas implements Runnable {
  11.     private static final long serialVersionUID = 1L;
  12.    
  13.     private boolean running;
  14.    
  15.     public static int WIDTH = 400;
  16.     public static int HEIGHT = 300;
  17.     public static String NAME = "TUTORIAL 1";
  18.    
  19.     public void start() {
  20.         running = true;
  21.         new Thread(this).start();
  22.     }
  23.    
  24.     public void run() {
  25.         long lastTime = System.currentTimeMillis();
  26.         long delta;
  27.        
  28.         init();
  29.        
  30.         while(running) {
  31.             delta = System.currentTimeMillis() - lastTime;
  32.             lastTime = System.currentTimeMillis(); 
  33.             render();
  34.             update(delta);
  35.         }
  36.     }
  37.    
  38.     public void init() {
  39.        
  40.     }
  41.    
  42.     public void render() {
  43.         BufferStrategy bs = getBufferStrategy();
  44.         if (bs == null) {
  45.             createBufferStrategy(2);
  46.             requestFocus();
  47.             return;
  48.         }
  49.        
  50.         Graphics g = bs.getDrawGraphics(); //получаем Graphics из созданной нами BufferStrategy
  51.         g.setColor(Color.black); //выбрать цвет
  52.         g.fillRect(0, 0, getWidth(), getHeight()); //заполнить прямоугольник
  53.         g.dispose();
  54.         bs.show(); //показать
  55.     }
  56.    
  57.     public void update(long delta) {
  58.        
  59.     }
  60.  
  61.     public static void main(String[] args) {
  62.         Game game = new Game();
  63.         game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
  64.         JFrame frame = new JFrame(Game.NAME);
  65.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66.         frame.setLayout(new BorderLayout());
  67.         frame.add(game, BorderLayout.CENTER);
  68.         frame.pack();
  69.         frame.setResizable(false);
  70.         frame.setVisible(true);
  71.         game.start();
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement