Calculation of FPS in Java graphics operation

Source: Internet
Author: User
Tags final time interval

FPS: The abbreviation for frame/sec (frames per second), also known as the frame rate. Refers to the number of frames that are refreshed in 1 seconds, and can be understood as a graphics processor can refresh several times per second. If specific to mobile phone is the ability to play (or record) the number of frames per second. At the same time, the higher the frame rate can be more fluent, more realistic animation. The more frames per second (fps), the smoother the actions displayed.

In most graphics programs (typical of game classes), the execution efficiency is based on FPS as the evaluation criterion.

Because of the current lack of Java related use cases, the completion of the function of the following figure (in the native test, the maximum FPS set to 500, the actual FPS efficiency in the IDE 280 or so, run 380, by system configuration and other factors):

The code is as follows:

FPS part of the relevant source code:

 package org.test; 


import Java.text.DecimalFormat;


/**


* <p>Title:LoonFramework</p>


* <p>Description:</p>


* <p>copyright:copyright (c) 2007</p>


* <p>Company:LoonFramework</p>


* @author Chenpeng


* @email: ceponline@yahoo.com.cn


* @version 0.1


*/


public class Fpslisten {


//Set the number of FPS frames for the animation, the higher the value, the faster the animation speed.


public static final int FPS = 500;


//conversion to run cycle


public static final Long PERIOD = (long) (1.0/fps * 1000000000); Unit: NS (nanosecond)


/FPS Maximum time interval, converted to 1s = 10^9ns


public static long fps_max_interval = 1000000000L; Unit: NS


    


//actual FPS value


private Double nowfps = 0.0;


    


/FPS Cumulative spacing time


private long interval = 0L; In NS


Private long time;


//Run frame cumulative


private Long framecount = 0;


    


//Format decimal places


private DecimalFormat df = new DecimalFormat ("0.0");


//Open OpenGL


public void OpenGL () {


system.setproperty ("Sun.java2d.opengl", "True");


system.setproperty ("Sun.java2d.translaccel", "True");


    }





    /** *//**


* Manufacturing FPS data


     *


     */


public void Makefps () {


framecount++;


interval + = PERIOD;


//When the actual interval matches the time.


if (interval >= fps_max_interval) {


//nanotime () returns the current value of the most accurate available system timer, in nanoseconds


Long TimeNow = System.nanotime ();


//Get the time distance so far


Long realtime = Timenow-time; Unit: NS


//conversion to actual FPS value


Nowfps = ((double) framecount/realtime) * FPS_MAX_INTERVAL;


//Change value


framecount = 0L;


interval = 0L;


time = TimeNow;


        }


    }


public Long Getframecount () {


return framecount;


}


public void Setframecount (long framecount) {


this.framecount = Framecount;


    }


public Long Getinterval () {


return interval;


    }


public void SetInterval (long interval) {


this.interval = interval;


    }


public Double Getnowfps () {


return nowfps;


    }


public void Setnowfps (double nowfps) {


this.nowfps = Nowfps;


    }


public Long GetTime () {


return time;


    }


public void SetTime (long time) {


this.time = time;


    }


public String Getfps () {


return Df.format (Nowfps);


    }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.