[java] Very simple rendering loop?

Started by
5 comments, last by d000hg 17 years, 3 months ago
I'm new to Java and while I have some resources, I don't at the moment have time to investigate properly what all these Applets and JFrames and stuff do. All I want to do is get a quick & dirty application which runs a classic render-loop like this:
while(notOver())
{
 Update();
 Repaint();
}
I am happy to mangle my way through painting stuff to the screen, but it's getting the skeleton app together where I'm confused. If it can be done in just a few lines I'd welcome an example, otherwise a link to a very simple tutorial - I just want a window which gets repainted in a loop. Later I'll want to do it on a timer but for now just a flat-out while() loop is fine. Thankyou for any suggestions.
Advertisement
The Java tutorials has a section on this in the part about Full-Screen Exclusive Mode. It's here, http://java.sun.com/docs/books/tutorial ( sorry, don't know how to make it link )

Otherwise, I would avoid using repaint(), if by that you mean you're forcing a paint message into the event queue, to eventually call the Applet/Frame's paint method, especially if you eventually set your loop up on a timer. If you're using it that way, you don't get a consistent framerate, because repaint() doesn't automatically call paint.

Of course, if you have a function Repaint() [ with capital R ], then you would have to have setIgnoreRepaint( true ) somewhere in your constructor or initialization code, because the Repaint() function would have something like this

Graphics g = getGraphics();
// drawing code
g.dispose()

and if your window decides it should be repainted at the same time your calling your Repaint() function, you'd have thread-lock
Eric Richards
I think I found something:
import java.awt.*;import java.awt.event.*;import javax.swing.*; public class simplegraphics extends JFrame{  public simplegraphics()  {    setVisible(true);    setSize(300,200);  }  public void paint(Graphics g)  {  }  public static void main(String args[])  {    simplegraphics SG = new simplegraphics();     while(true)    {    	if(step%500000==0)//the number 500000 doesn't matter but a smaller number will make the                          //animation, not the ball, speed up.    	{    		SG.repaint();    	}    }  }}


[Edited by - d000hg on January 27, 2007 3:32:10 AM]
Yes and this loop will also kill all input events.
I think you're describing the problem I have where closing the window leaves java.exe running, taking 100% of the CPU? How can I fix this most easily - this is a very simple app just to let me view a simulation, not anything ever being used elsewhere, so any solution that works is great.
Since you're using a JFrame, you can use setDefaultCloseOperation( EXIT_ON_CLOSE ) , in the constructor, and then when you click on the X in the title bar the program will close.
Eric Richards
rating++ for you!

This topic is closed to new replies.

Advertisement