I don't get where to insert the code and such; I could really use some help with this. :)

Here's my java file: (I removed irrelevant stuff)

package projectprojectile;
import java.applet.*;
import java.awt.*;
import java.io.File;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;

public class Main extends Applet implements Runnable
{
    // Variables...


    public void init() {
        // Variable initialization

    }

    public void start(){
        Thread th = new Thread(this);
        th.start();
    }

    public void run () {
        
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            while (true){
                repaint();
                try{
                      Thread.sleep (10);
                }
                catch (InterruptedException ex){
                      // Do nothing
                }
                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            }
      }


// Lots of mouse events here such as "public boolean mouseDrag"

// This is a double buffering code
    public void update (Graphics g){
          if (dbImage == null){
                dbImage = createImage (this.getSize().width, this.getSize().height);
                dbg = dbImage.getGraphics ();
          }
          dbg.setColor (getBackground ());
          dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
          dbg.setColor (getForeground());
          paint (dbg);
          g.drawImage (dbImage, 0, 0, this);
    }
    public void paint (Graphics g) {
        // Draw methods
    }

    // My own functions
}

Where and how would I insert the FPS counter and FPS limiter code?

Recommended Answers

All 8 Replies

Easiest limiter is to set your Thread.sleep(...) to 1000/fpsMax

But that can't possibly be the most effective one, can it?
It doesn't keep itself on a regular time pattern, it's relative to the amount of time the loading and rendering took.

Yes, true, but it is the easiest!
You can get the time, as nearly as the machine's clock will tell via:
(new Date()).getTime(); which returns the time in milliseconds.
You can use this either to see how many mS have expired between two paints, or to count how many paints you get in a second.
I prefer the first option: just before doing each sleep use the timer to see how long it was since the previous paint, then set the sleep to enough mS to give the desired time between frames, or skip it if the time between frames is already above the desired minimum

Yes, true, but it is the easiest!
You can get the time, as nearly as the machine's clock will tell via:
(new Date()).getTime(); which returns the time in milliseconds.
You can use this either to see how many mS have expired between two paints, or to count how many paints you get in a second.
I prefer the first option: just before doing each sleep use the timer to see how long it was since the previous paint, then set the sleep to enough mS to give the desired time between frames, or skip it if the time between frames is already above the desired minimum

Do you think you could show me where I would insert that code?

The forum rules are: "We only give homework help to those who show effort", so I can't just give you the code. But here's a quick pseudocode outline of the logic to get you started.

int maxFPS = 30
long nextRepaintDue = 0 // earliest time for next repaint in mSec
While  (true) {
   if (nextRepaintDue > now()) {
      // too soon to repaint, wait...
      sleep(nextRepaintDue - now())
   }
   nextRepaintDue = now() + 1000/maxFPS
   repaint()
}

The forum rules are: "We only give homework help to those who show effort", so I can't just give you the code. But here's a quick pseudocode outline of the logic to get you started.
<code>

Thanks!
It isn't actually a homework assignment, it's a personal project :)
Anyway, I believe I got this working, thanks!
I have a few other things I could use some help with though, and it'd be unnecessary to create a new thread for each problem I run into.
Do you have MSN messenger or Steam Friends where I can chat with you?

Do you have MSN messenger or Steam Friends where I can chat with you?

Sorry, no, I don't do chat.
Also consider that people come here to learn and to contribute. By taking a topic offline we deny then that chance.
Finally, I would encourage you to start a new thread for each separate topic and give it a good descriptive name so people can find it and refer to it later. Be proud to be part of the making of a repository of problems & solutions.
regards
James
ps: glad you got the FPS thing working OK.

commented: Wise words indeed. +29

Sorry, no, I don't do chat.
Also consider that people come here to learn and to contribute. By taking a topic offline we deny then that chance.
Finally, I would encourage you to start a new thread for each separate topic and give it a good descriptive name so people can find it and refer to it later. Be proud to be part of the making of a repository of problems & solutions.
regards
James
ps: glad you got the FPS thing working OK.

Okay, I will!
Thanks again for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.