Jump to content
  • Advertisement
Sign in to follow this  

[java] Graphics and Timing

This topic is 4788 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, as I am learning Java, I have decided to venture further into GUI development and have found the drawing capabilites. Here is what I have so far: Image hosted by Photobucket.com I have been reading up on timers and animation but from what I can tell so far, the timers are utilized to simply stop the redrawing process until a specific amount of time has passes. Coming from a background of Opengl, I would rather take the time it takes to draw and utilize that as a variable to modify how fast some component can "move" in relation to redrawing as fast as possible. Does Java have this sort of thing? Thank you.

Share this post

Link to post
Share on other sites
Is this what you're talking about?

long a = System.currentTimeMillis();
long b = System.currentTimeMillis();
System.out.printf("It took %d milliseconds to draw one frame.", a - b);

If you need more precision, I believe Java has support for getting the current time in nanoseconds (System.nanoTime()).

Share this post

Link to post
Share on other sites
Thank you for the reply.

I implemented a timing scheme into my program, but during the time the program is running, sometimes the animation stops for a moment and then resumes. Or, sporadically stops and then starts up again.

The following is how I am obtaining Delta Time

//Start Timing
lStartTime = System.currentTimeMillis();
//End Timing and obtain Delta
lDeltaTime = System.currentTimeMillis() - lStartTime;

The below is how I am applying the modified Delta Time

//Draw Arcs for Blades at Stipulated Angles
g.fillArc(175, 45, 290, 290, iArcSet, 30);
g.fillArc(175, 45, 290, 290, iArcSet + 90, 30);
g.fillArc(175, 45, 290, 290, iArcSet + 180, 30);
g.fillArc(175, 45, 290, 290, iArcSet + 275, 30);

//Modify ArcSet for incrementation
iArcSet += (10 * (int)(lDeltaTime * .1));

I have put out the resulting delta time whenever I run the program to a console and sometimes the lDeltaTime shows up as being 0. Am I applying this wrong?

One more question: Since I am sometimes recieving that annoying flashing, near the fan blades, my guess is to go look at either double-buffering, or erasing a particular section of the window and repainting there. Would double-buffering be a more viable solution?

Thank you.

Share this post

Link to post
Share on other sites
If you erase and repaint you will still be drawing each blade at a different time and double buffering is probably easier anyways. Just for the heck of it here is some basic double buffering code:

Image offscreen;
Graphics screen;
public void update(Graphics g)
Container pane = getContentPane();
offscreen = createImage(pane.getWidth(), pane.getHeight());
screen = offscreen.getGraphics();
g.drawImage(offscreen, 0, 0, this);

If you are calling repaint() then the update method should be called automatically before paint. Note: the code provided above does not resize the buffer when the window is resized

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!