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 16:08:35, viewed 233 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  
package applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

public class AppletGameTester extends Applet implements Runnable{
   
   private int width, height;
   private Image dbImage;
   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 sleepRate = 0L;
   private int deltaTime = 0;
   private long lastUpdateTime = 0L;
   private long currentUpdateTime = 0L;
   private long endLoopTime = 0L;
   private Graphics2D g, dbg;
    
   private boolean shiftedX = false, shiftedY = false;
   private int i,j;
   private Thread t;
   Ball 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();
         
         
         this.setSize(900, 600);
         width = getSize().width;
         height = getSize().height;
         setBackground(Color.BLACK);
         
         curTime = System.nanoTime() / 1000000;
         
         g = (Graphics2D)this.getGraphics();
         dbImage = this.createImage(width, height);
         dbg = (Graphics2D)dbImage.getGraphics();
         
         i=0;
         j=0;
         
         ball1 = new Ball(Color.BLUE, width, height, 12, width/2, height/2, .02, .02);
         ball2 = new Ball(Color.GREEN, width, height, 12, width/2, height/2, .12, .12);
         ball3 = new Ball(Color.ORANGE, width, height, 4, width/2, height/2, .5, .5);
         
         running = true;
         t = new Thread(this);
         t.start();
      }
      
      @Override
      public void run() {
           currentUpdateTime = System.nanoTime();
         while (running) {
            
            startTimeOfGameloop = System.nanoTime();

            // Calls paint(dbg) (using the Graphics-object from dbImage as parameter),
            // then calls g.drawImage(dbImage,0,0,null) to draw buffer to the Applet
            update(g);
            
            lastUpdateTime = currentUpdateTime;
              currentUpdateTime = System.nanoTime();
              
            // I've separated the FPS-calculations for readability purposes.
            updateTimersForFPScounter();
            
            updateGameLogic();
            
            // This smoothed out my last game, a full-screen one.
            // It doesn't seem to make much of a difference here.
            Toolkit.getDefaultToolkit().sync();
               
            // 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();
            sleepRate = 16666667L - (endLoopTime - startTimeOfGameloop);
            
            while (System.nanoTime()-endLoopTime < sleepRate) {
               Thread.yield();
               
               // You can try using Thread.sleep(0) instead of Thread.yield() above.
//                 try {
//                     Thread.sleep(0);
//                 }
//                 catch(Exception exc) {}
            }
            
            // Doing this instead of the while-loop above, ends up giving me 63-64 fps,
            // and subsequently yields a LOT more lag!
//            try {
//               if (sleepRate/1000000>0)
//                  Thread.sleep(sleepRate/1000000);
//            } catch (Exception e1) {
//               e1.printStackTrace();
//            }
         }
      }
      
      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 void update(Graphics g){
         paint(dbg);
         g.drawImage(dbImage, 0, 0, null);
      }
      
       public void paint(Graphics g){
         clear();
         g.setColor(Color.red);
         g.drawLine(0, 0, i+2, j+1);
         g.drawString("FPS: "+fps+" - SkeepRate: "+sleepRate+"ns", 400, 100);
         g.drawString("(__)", i, j);
         ball1.draw(g);
         ball2.draw(g);
         ball3.draw(g);
       }
       
       private void clear(){
            dbg.setColor(Color.black);
            dbg.fillRect(0, 0, width, height);
       }
   }





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!