Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pack.script.game;
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- //import java.awt.image.DataBufferInt;
- import javax.swing.JFrame;
- public class Game extends Canvas implements Runnable {
- private static final long serialVersionUID = 1L;
- public boolean iRUNNING = false;
- public int cTICKCOUNT = 0;
- public static final String sNAME = "Java Game";
- public static final String sVER = "v1.00";
- public static final int sSIZE = 40;
- public static final int sSCALE = 16;
- public static int iSCALE = 1;
- public static final String sDISPLAYNAME = sNAME + " " + sVER;
- public static final int sWIDTH = sSIZE * sSCALE;
- public static final int sHEIGHT = (int) ((double)sWIDTH / 12D * 9D);
- public static int iWIDTH = sWIDTH * iSCALE;
- public static int iHEIGHT = sHEIGHT * iSCALE;
- public static Dimension sDIMENSION = new Dimension(iWIDTH,iHEIGHT);
- private BufferedImage iIMAGE = new BufferedImage (iWIDTH, iHEIGHT, BufferedImage.TYPE_INT_RGB);
- //private int[] pixels = ((DataBufferInt) iIMAGE.getRaster().getDataBuffer()).getData();
- private JFrame sFRAME;
- public Game() {
- setMinimumSize(new Dimension(iWIDTH,iHEIGHT));
- setMaximumSize(new Dimension(iWIDTH,iHEIGHT));
- setPreferredSize(new Dimension(iWIDTH,iHEIGHT));
- sFRAME = new JFrame(sDISPLAYNAME);
- sFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- sFRAME.setLayout(new BorderLayout());
- sFRAME.add(this, BorderLayout.CENTER);
- sFRAME.setSize(iWIDTH, iHEIGHT);
- sFRAME.pack();
- sFRAME.setResizable(false);
- sFRAME.setLocationRelativeTo(null);
- sFRAME.setVisible(true);
- }
- public synchronized void start() {
- iRUNNING = true;
- new Thread(this).start();
- }
- public synchronized void stop() {
- iRUNNING = false;
- }
- public void run() {
- long cLASTTIME = System.nanoTime();
- double sNSPERTICK = 1000000000D / 60D;
- int cFRAMES = 0;
- int cTICKS = 0;
- long cLASTTIMER = System.currentTimeMillis();
- double delta = 0;
- while (iRUNNING) {
- long cNOWTIME = System.nanoTime();
- delta += (cNOWTIME - cLASTTIME) / sNSPERTICK;
- cLASTTIME = cNOWTIME;
- boolean shouldRender = true;
- while (delta >= 1) { cTICKS++; tick(); delta--; shouldRender = true; }
- try { Thread.sleep(2); } catch (InterruptedException e)
- { e.printStackTrace(); }
- if (shouldRender) { cFRAMES++; render(); }
- if (System.currentTimeMillis() - cLASTTIMER >= 1000) {
- cLASTTIMER += 1000;
- System.out.println(cFRAMES + " frames, " + cTICKS + " ticks");
- cFRAMES = 0; cTICKS = 0;
- }
- }
- }
- public void tick() {
- cTICKCOUNT++;
- /*for (int i = 0; i < pixels.length; i++) {
- pixels[i] = i + cTICKCOUNT;
- }*/
- }
- public void render() {
- BufferStrategy sBUFFERSTRATEGY = getBufferStrategy();
- if (sBUFFERSTRATEGY == null) {
- createBufferStrategy(3);
- return;
- }
- Graphics sGRAPHICS = sBUFFERSTRATEGY.getDrawGraphics();
- sGRAPHICS.drawImage(iIMAGE, 0, 0, getWidth(), getHeight(), null);
- sGRAPHICS.setColor(Color.BLACK);
- sGRAPHICS.fillRect(0, 0, getWidth(), getHeight());
- sGRAPHICS.setColor(Color.WHITE);
- gRECT(sGRAPHICS,5,5,645,485,true);
- sGRAPHICS.setColor(Color.BLACK);
- gPIX(sGRAPHICS,10,10);
- sGRAPHICS.dispose();
- sBUFFERSTRATEGY.show();
- }
- public void gRECT(Graphics g, int x1, int y1, int x2, int y2, boolean fill) {
- int x[] = {x1,x2,x2,x1,x1};
- int y[] = {y1,y1,y2,y2,y1};
- if (fill == false) {
- g.drawPolygon(x, y, 5);
- } else {
- g.fillPolygon(x, y, 5);
- }
- }
- public void gPIX(Graphics g, int x, int y) {
- g.drawLine(x, y, x, y);
- }
- public static void main(String[] args) {
- new Game().start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement