Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (847)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Ultroman (posted 2012-06-15 22:42:45, viewed 343 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182  
package applet;

import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;

public class AppletGameTesterDelta extends Applet implements Runnable{

   private int width, height;
   private volatile boolean running;
   private int fps = 0;
   private int frames = 0;
   private long startTimeOfGameloop = 0L;
   private long totalTime = 0L;
   private long lastTime = 0L;
   private long curTime = 0L;
   private long sleepTime = 0L;
   private int deltaTime = 0;
   private long lastUpdateTime = 0L;
   private long currentUpdateTime = 0L;
   private long endLoopTime = 0L;
   private Canvas drawArea;
   private boolean shiftedX = false, shiftedY = false;
   private int i,j;
   private Thread t;
   private BufferStrategy bufferStrategy;
   DeltaBall ball1, ball2, ball3;

   public void init() {
      Thread timerAccuracyThread = new Thread(new Runnable() {
         public void run() {
            while (true) {
               try {
                  Thread.sleep(Long.MAX_VALUE);
               } catch (Exception e) {
               }
            }
         }
      });
      timerAccuracyThread.setDaemon(true);
      timerAccuracyThread.start();

      // setup the canvas holding the BufferStrategy
      this.setSize(900, 600);
      width = getSize().width;
      height = getSize().height;
      setBackground(Color.BLACK);
      drawArea = new Canvas();
      setIgnoreRepaint(true);

      i=0;
      j=0;

      ball1 = new DeltaBall(Color.BLUE, width, height, 12, width/2, height/2, .02, .02);
      ball2 = new DeltaBall(Color.GREEN, width, height, 12, width/2, height/2, .12, .12);
      ball3 = new DeltaBall(Color.ORANGE, width, height, 4, width/2, height/2, .5, .5);

      running = true;
      t = new Thread(this);
      t.start();
   }

   @Override
   public void run() {
      drawArea.setSize(new Dimension(width,height));
      add(drawArea);
      drawArea.createBufferStrategy(2);
      bufferStrategy = drawArea.getBufferStrategy();
      
      curTime = System.nanoTime(); // FPS counter timer-variable
      currentUpdateTime = System.nanoTime(); // game-loop timing variable
      
      while (running) {
         startTimeOfGameloop = System.nanoTime();

         lastUpdateTime = currentUpdateTime;
         currentUpdateTime = System.nanoTime();

         // I've separated the FPS-calculations for readability purposes.
         updateTimersForFPScounter();

         //Update any sprites or other graphical objects
         updateGameLogic();

         do{
                do{
                   //Handle Drawing
                 Graphics2D g = getGraphics();
                 draw(g); // draws whatever you'd like

                 //Dispose of graphics context
                 g.dispose();
                }while(bufferStrategy.contentsRestored());
                
                bufferStrategy.show();
                
            }while(bufferStrategy.contentsLost());

         // Find out the time it took to render, and deduct it from 1000000000ns/60 = 16666667,
         // to get the amount of time we should sleep after this update, to hit 60fps.
         endLoopTime = System.nanoTime();
         sleepTime = 16666667L - (endLoopTime - startTimeOfGameloop);

         long diff;
         while((diff = System.nanoTime()-endLoopTime) < sleepTime) {
            if(diff < sleepTime*0.8)
               try { Thread.sleep(1); } catch(Exception exc) {}
               else
                  Thread.yield();
         }
      }
   }

   private void updateTimersForFPScounter() {
      lastTime = curTime;
      curTime = System.nanoTime();
      totalTime += curTime - lastTime;
      // count up frames per second...
      frames++;

      if( totalTime >= 1000000000) {
         totalTime = 0;
         fps = frames;
         frames = 0;
      }else{

      }
   }

   private void updateGameLogic() {
      deltaTime = (int) ((currentUpdateTime - lastUpdateTime)/(1000*1000));

      // Controls the movement of the line and (__)
      if(i>width-22)shiftedX=true;
      if(i<2) shiftedX=false;

      if(j>height)shiftedY=true;
      if(j<2) shiftedY=false;

      if(!shiftedX)i++;
      else{i--;}

      if(!shiftedY)j++;
      else{j--;}

      // Update balls
      ball1.update(deltaTime);
      ball2.update(deltaTime);
      ball3.update(deltaTime);
   }

   public Graphics2D getGraphics()
   {
      return (Graphics2D)bufferStrategy.getDrawGraphics();
   }

   public void draw(Graphics g){
      g.setColor(Color.black);
         g.fillRect(0, 0, width, height);
         
      g.setColor(Color.red);
      g.drawLine(0, 0, i+2, j+1);
      g.drawString("FPS: "+fps+" - SkeepRate: "+sleepTime+"ns", 400, 100);
      g.drawString("(__)", i, j);
      ball1.draw(g);
      ball2.draw(g);
      ball3.draw(g);
   }
   
   public void destroy()
   {
      running=false;
      /*Allow Applet to destroy any resources used by this applet*/
      super.destroy();
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
EgonOlsen (364 views)
2018-06-10 19:43:48

EgonOlsen (364 views)
2018-06-10 19:43:44

EgonOlsen (304 views)
2018-06-10 19:43:20

DesertCoockie (540 views)
2018-05-13 18:23:11

nelsongames (871 views)
2018-04-24 18:15:36

nelsongames (854 views)
2018-04-24 18:14:32

ivj94 (1306 views)
2018-03-24 14:47:39

ivj94 (426 views)
2018-03-24 14:46:31

ivj94 (1089 views)
2018-03-24 14:43:53

Solater (445 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!