Advertisement
Guest User

Untitled

a guest
Jun 7th, 2012
11,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package org.tides.tutorial;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Canvas;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.awt.GraphicsConfiguration;
  9. import java.awt.GraphicsEnvironment;
  10. import java.awt.Image;
  11. import java.awt.Toolkit;
  12. import java.awt.Transparency;
  13. import java.awt.image.BufferStrategy;
  14. import java.awt.image.BufferedImage;
  15. import java.io.IOException;
  16. import java.net.URL;
  17.  
  18. import javax.imageio.ImageIO;
  19. import javax.swing.JFrame;
  20.  
  21. public class Game extends Canvas implements Runnable {
  22.     private static final long serialVersionUID = 1L;
  23.    
  24.     private boolean running;
  25.    
  26.     public static int WIDTH = 400;
  27.     public static int HEIGHT = 300;
  28.     public static String NAME = "TUTORIAL 1";
  29.    
  30.     public static Sprite hero;
  31.    
  32.     public void start() {
  33.         running = true;
  34.         new Thread(this).start();
  35.     }
  36.    
  37.     public void run() {
  38.         long lastTime = System.currentTimeMillis();
  39.         long delta;
  40.        
  41.         init();
  42.        
  43.         while(running) {
  44.             delta = System.currentTimeMillis() - lastTime;
  45.             lastTime = System.currentTimeMillis(); 
  46.             render();
  47.             update(delta);
  48.         }
  49.     }
  50.    
  51.     public void init() {
  52.         hero = getSprite("man.png");
  53.     }
  54.    
  55.     public void render() {
  56.         BufferStrategy bs = getBufferStrategy();
  57.         if (bs == null) {
  58.             createBufferStrategy(2);
  59.             requestFocus();
  60.             return;
  61.         }
  62.        
  63.         Graphics g = bs.getDrawGraphics(); //получаем Graphics из созданной нами BufferStrategy
  64.         g.setColor(Color.black); //выбрать цвет
  65.         g.fillRect(0, 0, getWidth(), getHeight()); //заполнить прямоугольник
  66.         hero.draw(g, 20, 20);
  67.         g.dispose();
  68.         bs.show(); //показать
  69.     }
  70.    
  71.     public void update(long delta) {
  72.        
  73.     }
  74.  
  75.     public Sprite getSprite(String path) {
  76.         BufferedImage sourceImage = null;
  77.        
  78.         try {
  79.             URL url = this.getClass().getClassLoader().getResource(path);
  80.             sourceImage = ImageIO.read(url);
  81.         } catch (IOException e) {
  82.             e.printStackTrace();
  83.         }
  84.  
  85.         Sprite sprite = new Sprite(Toolkit.getDefaultToolkit().createImage(sourceImage.getSource()));
  86.        
  87.         return sprite;
  88.     }
  89.    
  90.     public static void main(String[] args) {
  91.         Game game = new Game();
  92.         game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
  93.         JFrame frame = new JFrame(Game.NAME);
  94.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  95.         frame.setLayout(new BorderLayout());
  96.         frame.add(game, BorderLayout.CENTER);
  97.         frame.pack();
  98.         frame.setResizable(false);
  99.         frame.setVisible(true);
  100.         game.start();
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement