This site requires JavaScript, please enable it in your browser!
Greenfoot back
ValeroDeniro
ValeroDeniro wrote ...

2015/2/26

Advanced Tricky Coding; Can anyone add comment to these lines?

ValeroDeniro ValeroDeniro

2015/2/26

#
I've never seen code so complex. Cn anyone explain to me what is happening here? Thank you very much and I would really appreciate it.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Graphics;

/**
 * Displays the number of frames per second.
 * 
 * Tanvero Prod.
 */
public class FPS extends Actor
{
    private static final Color textColor = new Color(255, 0, 50);
    private static final String text = "FPS: ";
    private static final int updateFreq = 10;
    private long countAct;
    private long prevTime;
    private double fps;
    private String value;
    double lowest;

    /**
     * Create a new FPS object
     */
    public FPS()
    {
        value = "0";
        countAct = 0;
        setImage(new GreenfootImage(47, 13));
        GreenfootImage image = getImage();
        image.setColor(textColor);
        updateImage();
    }
    
    /**
     * Set the value of the frames per second
     */
    private void setFPS(String val)
    {
        value = val;
        updateImage();
    }
    
    public void act()
    {
        if(Greenfoot.isKeyDown("r"))
            lowest=1000000;
        countAct++;
        long gap = System.currentTimeMillis() - prevTime;
        if(gap != 0 && countAct % updateFreq==0) {
            fps = ((1.0/gap)*1000.0);
            Double d = fps;
            if(fps<lowest)lowest=fps;
            value = d.toString();
            updateImage();
        }
        prevTime = System.currentTimeMillis();
    }

    /**
     * Make the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 12);
    }
    
    
}
danpost danpost

2015/2/26

#
This is the code explained. A following post will contain a basic class that does the same thing (without extraneous fields and procedures).
import greenfoot.*; // makes classes of the greenfoot package accessible
import java.awt.Color; // makes the Color class accessible
import java.awt.Graphics; // makes the Graphics class accessible (not used)
/** comment with author */ 
/**
 * Displays the number of frames per second.
 * 
 * Tanvero Prod.
 */

/** creates a specific Counter object to display running frame-rate of the scenario */
public class FPS extends Actor
{
    /** class Color constant for color of text */
    private static final Color textColor = new Color(255, 0, 50);
    
    /** class String constant for part (title) of text */
    private static final String text = "FPS: ";
    
    /** class int constant for limiting how often to update the image of this actor */
    private static final int updateFreq = 10;
    
    /** object instance fields (fields given to each instance created from this class) */
    private long countAct; // field to count act cycles
    private long prevTime; // field to hold the start time for this set of act cycles
    private double fps; // can be local to act method (not needed to be tracked between acts)
    private String value; // unsure at this time
    double lowest; // not a factor in the code (all codes using this can be removed)
 
    /**
     * Create a new FPS object
     */
    /** initializes a new FPS object */
    public FPS()
    {
        // initialize instance fields 
        value = "0";
        countAct = 0;
        
        // initialize image for this actor
        setImage(new GreenfootImage(47, 13)); // creates and sets a new image for this actor
        GreenfootImage image = getImage(); // gets a local reference to the new image of this actor
        image.setColor(textColor); // sets the drawing color for future draw commands for the image
        updateImage(); // initializes the text shown on this image
    }
     
    /**
     * Set the text string value of the frames per second
     */
    /**
     * sets a new text fps value to the one given and updates the image
     * @param val the new fps value
     * NOTE:  not used (can be removed from the class)
     */
    private void setFPS(String val)
    {
        value = val; // saves new text string for fps value
        updateImage(); // updates the text on the image of this actor
    }
     
    /**
     * updates the image of this actor with current fps value every so often
     * (depending on the value of the 'updateFreq' field)
     */
    public void act()
    {
        // pressing the 'r' key reset value of 'lowest' field which is not significant
        if(Greenfoot.isKeyDown("r")) // (this 'if' block can be removed)
            lowest=1000000; // not significant
            
        countAct++; // counts act cycles
        long gap = System.currentTimeMillis() - prevTime; // gets time between last cycle and this cycle
        if(gap != 0 && countAct % updateFreq==0) // if time to update (gap should never be zero)
        {
            fps = ((1.0/gap)*1000.0); // compute new fps value
            Double d = fps; // silly re-assignment of fps value to a local variable
            if(fps<lowest)lowest=fps; // not significant (can be removed)
            value = d.toString(); // converts double value to text string
            updateImage(); // updates the text on the image of this actor
        }
        prevTime = System.currentTimeMillis(); // saves new start time for timing to next act cycle
    }
 
    /**
     * Make the image
     */
    /** updates the text shown on the image of this actor */
    private void updateImage()
    {
        GreenfootImage image = getImage(); // gets local reference to image of actor
        image.clear(); // clear the image (makes fully transparent)
        image.drawString(text + value, 1, 12); // draws new text string on image
    }
}
danpost danpost

2015/2/26

#
Here is the revised code (as I might have done it):
import greenfoot.*; // makes classes of the greenfoot package accessible

/** creates a specific Counter object to display running frame-rate of the scenario */
public class FPS extends Actor
{
    /** class int constant for limiting how often to update the image of this actor */
    private static final int updateFreq = 30;
    
    /** object instance fields (fields given to each instance created from this class) */
    private long countAct; // field to count act cycles
    private long prevTime; // field to hold the start time for this set of act cycles
    private long sumTime = 500; // field to accumulate times between acts in current update cycle
 
    /** initializes a new FPS object */
    public FPS()
    {
        setImage(new GreenfootImage("FPS:    ", 18, null, null)); // initializes the text shown on this image
    }
     
    /**
     * updates the image of this actor with current fps value every so often
     * (depending on the value of the 'updateFreq' field)
     */
    public void act()
    {
        long time = System.currentTimeMillis(); // gets current time value (must be done first thing in act)
        if (prevTime != 0) // must have a previous time to determine time between act cycles
        {
            sumTime += time-prevTime; // calculate time between act cycles and add to accumulator
            countAct = (countAct+1)%updateFreq; // counts act cycles between updates
            if (countAct == 0) // if time to update
            {
                double fps = (double)((int)updateFreq*100000/sumTime)/100; // calculates new fps value
                setImage(new GreenfootImage("FPS: "+fps, 18, null, null)); // updates the text on image
                sumTime = 0; // resets the accumulator
            }
        }
        prevTime = System.currentTimeMillis(); // saves new start time value (must be done last thing in act)
    }
}
ValeroDeniro ValeroDeniro

2015/2/27

#
Thanks a lot! it was hard understanding the comments but thank you!
You need to login to post a reply.