Advertisement
Guest User

Untitled

a guest
Apr 7th, 2014
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 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 = (int) ((double)sWIDTH / 12D * 9D);
  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(new Dimension(iWIDTH,iHEIGHT));
  39.         setMaximumSize(new Dimension(iWIDTH,iHEIGHT));
  40.         setPreferredSize(new Dimension(iWIDTH,iHEIGHT));
  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.setSize(iWIDTH, iHEIGHT);
  47.         sFRAME.pack();
  48.        
  49.         sFRAME.setResizable(false);
  50.         sFRAME.setLocationRelativeTo(null);
  51.         sFRAME.setVisible(true);
  52.     }
  53.    
  54.     public synchronized void start() {
  55.         iRUNNING = true;
  56.         new Thread(this).start();
  57.     }
  58.    
  59.     public synchronized void stop() {
  60.         iRUNNING = false;
  61.     }
  62.    
  63.     public void run() {
  64.         long cLASTTIME = System.nanoTime();
  65.         double sNSPERTICK = 1000000000D / 60D;
  66.        
  67.         int cFRAMES = 0;
  68.         int cTICKS = 0;
  69.        
  70.         long cLASTTIMER = System.currentTimeMillis();
  71.         double delta = 0;
  72.        
  73.         while (iRUNNING) {
  74.             long cNOWTIME = System.nanoTime();
  75.             delta += (cNOWTIME - cLASTTIME) / sNSPERTICK;
  76.             cLASTTIME = cNOWTIME;
  77.             boolean shouldRender = true;
  78.            
  79.             while (delta >= 1) { cTICKS++; tick(); delta--; shouldRender = true; }
  80.            
  81.             try { Thread.sleep(2); } catch (InterruptedException e)
  82.             { e.printStackTrace(); }
  83.            
  84.             if (shouldRender) { cFRAMES++; render(); }
  85.             if (System.currentTimeMillis() - cLASTTIMER >= 1000) {
  86.                 cLASTTIMER += 1000;
  87.                 System.out.println(cFRAMES + " frames, " + cTICKS + " ticks");
  88.                 cFRAMES = 0; cTICKS = 0;
  89.             }
  90.         }
  91.     }
  92.    
  93.     public void tick() {
  94.         cTICKCOUNT++;
  95.         /*for (int i = 0; i < pixels.length; i++) {
  96.             pixels[i] = i + cTICKCOUNT;
  97.         }*/
  98.     }
  99.    
  100.     public void render() {
  101.         BufferStrategy sBUFFERSTRATEGY = getBufferStrategy();
  102.         if (sBUFFERSTRATEGY == null) {
  103.             createBufferStrategy(3);
  104.             return;
  105.         }
  106.        
  107.         Graphics sGRAPHICS = sBUFFERSTRATEGY.getDrawGraphics();
  108.         sGRAPHICS.drawImage(iIMAGE, 0, 0, getWidth(), getHeight(), null);
  109.        
  110.         sGRAPHICS.setColor(Color.WHITE);
  111.         sGRAPHICS.fillRect(0, 0, getWidth(), getHeight());
  112.        
  113.         sGRAPHICS.setColor(Color.BLACK);
  114.         gRECT(1,1,1,1,true);
  115.        
  116.         sGRAPHICS.dispose();
  117.         sBUFFERSTRATEGY.show();
  118.     }
  119.  
  120.     public void gRECT(int x1, int y1, int x2, int y2, boolean fill) {
  121.        
  122.     }
  123.    
  124.     public static void main(String[] args) {
  125.         new Game().start();
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement