Advertisement
Guest User

Untitled

a guest
Apr 7th, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package pack.script.game;
  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.image.BufferStrategy;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.DataBufferInt;
  11.  
  12. import javax.swing.JFrame;
  13.  
  14. public class Game extends Canvas implements Runnable {
  15.     private static final long serialVersionUID = 1L;
  16.    
  17.     public boolean iRUNNING = false;
  18.     public int cTICKCOUNT = 0;
  19.    
  20.     public static final String sNAME = "Java Game";
  21.     public static final String sVER = "v1.00";
  22.     public static final int sSIZE = 40;
  23.     public static final int sSCALE = 16;
  24.     public static int iSCALE = 1;
  25.    
  26.     public static final String sDISPLAYNAME = sNAME + " " + sVER;
  27.     public static final int sWIDTH = sSIZE * sSCALE;
  28.     public static final int sHEIGHT = sWIDTH / 12 * 9;
  29.     public static int iWIDTH = sWIDTH * iSCALE;
  30.     public static int iHEIGHT = sHEIGHT * iSCALE;
  31.     public static Dimension sDIMENSION = new Dimension(iWIDTH,iHEIGHT);
  32.    
  33.     private BufferedImage iIMAGE = new BufferedImage (iWIDTH, iHEIGHT, BufferedImage.TYPE_INT_RGB);
  34.     private int[] pixels = ((DataBufferInt) iIMAGE.getRaster().getDataBuffer()).getData();
  35.     private JFrame sFRAME;
  36.    
  37.     public Game() {
  38.         setMinimumSize(sDIMENSION);
  39.         setMaximumSize(sDIMENSION);
  40.         setPreferredSize(sDIMENSION);
  41.        
  42.         sFRAME = new JFrame(sDISPLAYNAME);
  43.         sFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44.         sFRAME.setLayout(new BorderLayout());
  45.         sFRAME.add(this, BorderLayout.CENTER);
  46.         sFRAME.pack();
  47.        
  48.         sFRAME.setResizable(false);
  49.         sFRAME.setLocationRelativeTo(null);
  50.         sFRAME.setVisible(true);
  51.     }
  52.    
  53.     public synchronized void start() {
  54.         iRUNNING = true;
  55.         new Thread(this).start();
  56.     }
  57.    
  58.     public synchronized void stop() {
  59.         iRUNNING = false;
  60.     }
  61.    
  62.     public void run() {
  63.         long cLASTTIME = System.nanoTime();
  64.         double sNSPERTICK = 1000000000D / 60D;
  65.        
  66.         int cFRAMES = 0;
  67.         int cTICKS = 0;
  68.        
  69.         long cLASTTIMER = System.currentTimeMillis();
  70.         double delta = 0;
  71.        
  72.         while (iRUNNING) {
  73.             long cNOWTIME = System.nanoTime();
  74.             delta += (cNOWTIME - cLASTTIME) / sNSPERTICK;
  75.             cLASTTIME = cNOWTIME;
  76.             boolean shouldRender = true;
  77.            
  78.             while (delta >= 1) { cTICKS++; tick(); delta--; shouldRender = true; }
  79.            
  80.             try { Thread.sleep(2); } catch (InterruptedException e)
  81.             { e.printStackTrace(); }
  82.            
  83.             if (shouldRender) { cFRAMES++; render(); }
  84.             if (System.currentTimeMillis() - cLASTTIMER >= 1000) {
  85.                 cLASTTIMER += 1000;
  86.                 System.out.println(cFRAMES + " frames, " + cTICKS + " ticks");
  87.                 cFRAMES = 0; cTICKS = 0;
  88.             }
  89.         }
  90.     }
  91.    
  92.     public void tick() {
  93.         cTICKCOUNT++;
  94.         for (int i = 0; i < pixels.length; i++) {
  95.             pixels[i] = i + cTICKCOUNT;
  96.         }
  97.     }
  98.    
  99.     public void render() {
  100.         BufferStrategy sBUFFERSTRATEGY = getBufferStrategy();
  101.         if (sBUFFERSTRATEGY == null) {
  102.             createBufferStrategy(3);
  103.             return;
  104.         }
  105.        
  106.         Graphics sGRAPHICS = sBUFFERSTRATEGY.getDrawGraphics();
  107.         sGRAPHICS.drawImage(iIMAGE, 0, 0, getWidth(), getHeight(), null);
  108.        
  109.         sGRAPHICS.setColor(Color.WHITE);
  110.         sGRAPHICS.fillRect(0, 0, getWidth(), getHeight());
  111.        
  112.         sGRAPHICS.setColor(Color.BLACK);
  113.         sGRAPHICS.fillRect(2, 2, (getWidth() - 2), (getHeight() - 2));
  114.        
  115.         sGRAPHICS.dispose();
  116.         sBUFFERSTRATEGY.show();
  117.     }
  118.    
  119.     public static void main(String[] args) {
  120.         new Game().start();
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement