Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (754)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Applet main loop  (Read 6500 times)
0 Members and 1 Guest are viewing this topic.
Offline stefanha

Senior Newbie

I love YaBB 1G - SP1!

« Posted 2002-10-18 11:08:54 »

I know this must have been covered in various places in the past.  However, I would be interested in your opinions and alternatives for the "Applet main loop" problem; How to structure the main loop of a real-time applet?

I think we all more or less agree on:
public class RealTimeApplet extends Applet implements Runnable
Thread thread; // Runner thread

public void start () {
thread = new Thread ( this );

public void stop () {
thread = null;

public void run ()
while ( thread != null )
// run main loop

public void update ( Graphics g )
{ // override because origional cleared screen
paint ( g );

Now comes the interesting part.  How do we update logic and then repaint in the main loop.  These are some ideas/approaches I've seen:

1. Just grab the Applet's Graphics context and use it however you like (bypassing the paint() function).  This is not very friendly, and I'm not sure have supported it is by various AWT implementations.
2. In the main loop, wait() after calling repaint().  The paint() function then calls notifyAll() when it is done.  When running on a fast computer, the applet can eat up too many resources (ie. freeze the system by taking almost all time).  This means some performance manager must be implemented to avoid this :(.
3. Just use Thread.sleep (1L) at the end of the main loop.  This is kind of a hack :(.  It avoids eating too many resources though.

I would like to know what other approaches there are, because these all have some drawback(s) which I don't like.

Also, who uses ToolKit.vsync () and where should it be used.  Who uses Thread.yield () and where should it be used.

You see I have many questions ;).  Sorry guys, but I'm looking to sort this out once and for all.

from, Stefan
Offline Jeff

JGO Coder

Got any cats?

« Reply #1 - Posted 2002-10-18 19:37:44 »

Applet animation is kind of a black art.  The shrot answer is in the limited 1.1.x world of Applets there really isn't a 'right' way to do it, those who write applet games have found all sorts of hacks to make the wrong ways function all right.

In applciations, use JDk1.4 BufferStratagey  to get the graphics to paint do and do all your processing and painting in a bgi loop in a single thread (totally bypassing AWT paint() ), thats the way "real' games have done it for a long long time in other languages.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Offline Chaoslab

Junior Newbie


« Reply #2 - Posted 2002-10-19 06:51:07 »

Well regarding 1.1 stuff....

I always set the thread priority to min else the applet will hog to much os time when the applet is not focus on WinNT / Win2K boxs.

And then just make empty update / paint methods and grab store a Graphics ( g_ref = this.getGraphics )reference in the applet init() method to use for drawing to the applet canvas, and when image builder store use an MemoryImageSource and newPixels the image (and if you are not using the alpha channel which is done by dither and looks ugly use a color mode that is only 24 bit ( DirectColorModel  cmr_img_cm = new DirectColorModel(32,0xff0000,0xff00,0xff); ) and use that when you create the MemoryImageSource and the image gets built about 30% faster!

Smiley hope that helps....

 Cam Mckechnie
Pages: [1]
  ignore  |  Print  

DesertCoockie (33 views)
2018-05-13 18:23:11

nelsongames (75 views)
2018-04-24 18:15:36

nelsongames (70 views)
2018-04-24 18:14:32

ivj94 (752 views)
2018-03-24 14:47:39

ivj94 (82 views)
2018-03-24 14:46:31

ivj94 (622 views)
2018-03-24 14:43:53

Solater (98 views)
2018-03-17 05:04:08

nelsongames (179 views)
2018-03-05 17:56:34

Gornova (405 views)
2018-03-02 22:15:33

buddyBro (1065 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!