Sign in to follow this  
Xiachunyi

[java] Graphics and Timing

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();
updateWorld();
renderWorld();
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)
{
if(screen==null){
Container pane = getContentPane();
offscreen = createImage(pane.getWidth(), pane.getHeight());
screen = offscreen.getGraphics();
}
paint(screen);
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this