Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Applet code...  (Read 6108 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard
****

Posts: 1477
Medals: 23


I always win!


« on: 2007-12-10 20:09:39 »

Anyone got a source code for a 4k game as an applet?

I'm wondering what you really need, and how you set up the double buffering. Going to put it in some sort of "getting started" page.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline woogley

JGO Neuromancer
****

Posts: 1098
Medals: 5



« Reply #1 on: 2007-12-11 00:42:41 »

bare-bones 4K applet with double buffering (untested, uncompiled):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
import java.applet.*;
import java.awt.*;
public class Game extends Applet implements Runnable {
   Image buffer; // faster than BufferedImage, I've experienced
  public void init() {
      buffer = createImage(getWidth(),getHeight());
      new Thread(this).start();
   }
   public void run() {
      while (true) {
         // game logic here
        paint(getGraphics());
         try {
            Thread.sleep(10);
         }
         catch (InterruptedException e) {}
      }
   }
   public void paint(Graphics g) {
      Graphics gfx = buffer.getGraphics();
      // work your drawing magic here
     g.drawImage(buffer,0,0,null);
   }
}
Offline Markus_Persson

JGO Kernel
*****

Posts: 2092
Medals: 10


Mojang Specifications


« Reply #2 on: 2007-12-11 02:25:39 »

bare-bones 4K applet with double buffering (untested, uncompiled):
1  
2  
3  
[/quote]

You'll really really want to implement and support stop() as well to avoid hanging browsers, which is not very popular.

Play Minecraft!
Games published by our own members! Go get 'em!
Offline woogley

JGO Neuromancer
****

Posts: 1098
Medals: 5



« Reply #3 on: 2007-12-11 07:51:14 »

lack of stop() has never been an issue for me in the past, but if you have the extra bytes, you might as well use it.

however, a cheaper way to avoid any potential hanging would be to replace:

1  
while (true) {


with

1  
while (me.isAlive()) { // where 'me' is your Thread


so, revision:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
import java.applet.*;
import java.awt.*;
public class Game extends Applet implements Runnable {
   Image buffer; // faster than BufferedImage, I've experienced
  Thread me;
   public void init() {
      buffer = createImage(getWidth(),getHeight());
      me = new Thread(this);
      me.start();
   }
   public void run() {
      while (me.isAlive()) {
         // game logic here
        if (getGraphics() != null) paint(getGraphics());
         try {
            Thread.sleep(10);
         }
         catch (InterruptedException e) {}
      }
   }
   public void paint(Graphics g) {
      Graphics gfx = buffer.getGraphics();
      // work your drawing magic here
     g.drawImage(buffer,0,0,null);
   }
}

this clocks in at 915b/708b (uncompressed/compressed)

adding a stop() function clocks it to 995b uncompressed, which isn't bad, but if you're looking for 90 extra bytes towards the end of your game, this is one area to slash.
this is the stop() function i used:
1  
2  
3  
4  
5  
6  
   public void stop() {
      try {
         me.join(1000);
      }
      catch (InterruptedException e) {}
   }


enable your applet console, and try this applet (which doesn't have stop()) to see if it works for you: http://woogley.net/misc/Applet4K/ (source). you should notice in the console it will stop printing "still running" after you navigate away from the page, with no ugly exceptions
Offline Markus_Persson

JGO Kernel
*****

Posts: 2092
Medals: 10


Mojang Specifications


« Reply #4 on: 2007-12-11 07:55:45 »

1  
while (me.isAlive()) { // where 'me' is your Thread

Thread.currentThread().isAlive() will always return true, so that's a noop.

I guess this means I know where to shave off another bunch of bytes from t4kns =D

Play Minecraft!
Offline woogley

JGO Neuromancer
****

Posts: 1098
Medals: 5



« Reply #5 on: 2007-12-11 08:16:46 »

Thread.currentThread().isAlive() will always return true, so that's a noop.

hmm, after testing in the console, it seems you're right o_O. mystery of life solved. at this rate, does it even need to implement Runnable? can it just run on the Applet's pre-existing currentThread() I wonder?

edit: nevermind, that was a bad idea. I successfully murdered firefox when trying that Smiley
Offline Markus_Persson

JGO Kernel
*****

Posts: 2092
Medals: 10


Mojang Specifications


« Reply #6 on: 2007-12-11 08:48:47 »

Yeah, not returning from init()/the contructor is a really bad idea. Wink

Play Minecraft!
Offline Abuse

JGO Kernel
*****

Posts: 1866
Medals: 5


falling into the abyss of reality


« Reply #7 on: 2007-12-11 10:08:55 »


edit: nevermind, that was a bad idea. I successfully murdered firefox when trying that Smiley

There-in lies the reason Applets never took off  Wink
Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #8 on: 2007-12-11 14:55:48 »

a short way may be as the following , no ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void run()
{
   try
   {
     while(true)
     {
      gamelogic();
      gamedraw();
      Thread.sleep(10);
    }
   }
   catch(InterruptedException ie)
   {
    //End of game
  }
}


or

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void run()
{
     while(me!=null)
     {
      gamelogic();
      gamedraw();
    }
}

public void stop()
{
 Thread t=me;
 me=null;
 t.join();
}




Offline kevglass
« League of Dukes »

JGO Kernel
*****

Posts: 5214
Medals: 49


Mentally unstable, best avoided.


« Reply #9 on: 2007-12-11 14:57:04 »

1  
2  
3  
4  
5  
public void stop()
{
 me=null;
 me.join();
}


You know I think that might blow up.  Grin

Kev

Games published by our own members! Go get 'em!
Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #10 on: 2007-12-11 15:03:57 »

hehe i saw and edit my post but you was faster than me Wink

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5866
Medals: 255


Hand over your head.


« Reply #11 on: 2007-12-11 16:24:48 »

I always wondered why everybody was null-ing their Thread-references... it certainly doesn't make them stop.

Any special applet-context magic? Embarrassed

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #12 on: 2007-12-11 22:08:58 »

it remove reference on thread, and exiting the run method kill thread , so no reference and thread not running (run method ended) anymore implied that it is freed.

Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #13 on: 2007-12-12 08:55:36 »

some infomrmations on applet methods: init/start/stop/destroy

correct me if I said something wrong

start/stop method in an applet doesn't mean starting en stopping the applet as start and stop method can be called more than once.

init method means: nothing visible yet, applet panel not connected to screen, but you can initialise some stuff.
start method means: start the applet (the first time) or resume from pause(for next call)
stop method means: pause the applet
destroy method means: destroy the applet (the code to free and exit thread should be there)

so
init/destroy methods works together as constructor & destructor of applet
start/stop methods works together as resume/pause applet

usually/normally way is: stop method is called when browser is minimised and start when browser is restored or after applet init for first call

Offline Markus_Persson

JGO Kernel
*****

Posts: 2092
Medals: 10


Mojang Specifications


« Reply #14 on: 2007-12-12 09:07:50 »

Oh. That sounds likely. And explains a lot. Wink

Play Minecraft!
Offline CaptainJester

JGO Neuromancer
****

Posts: 1138
Medals: 8


Make it work; make it better.


« Reply #15 on: 2007-12-12 11:24:32 »

some infomrmations on applet methods: init/start/stop/destroy

correct me if I said something wrong

start/stop method in an applet doesn't mean starting en stopping the applet as start and stop method can be called more than once.

init method means: nothing visible yet, applet panel not connected to screen, but you can initialise some stuff.
start method means: start the applet (the first time) or resume from pause(for next call)
stop method means: pause the applet
destroy method means: destroy the applet (the code to free and exit thread should be there)

so
init/destroy methods works together as constructor & destructor of applet
start/stop methods works together as resume/pause applet

usually/normally way is: stop method is called when browser is minimised and start when browser is restored or after applet init for first call


stop is also called if you navigate away from the page running the applet. 
start is also called after navigation away, then returning to the page. 

note:  If the browser is not destroyed, the init method will not be called again.  This where you can have trouble when testing an applet since the new jar will not be reloaded unless you close your browser or clear the classloader cache in the console.

Offline jbanes

JGO Neuromancer
****

Posts: 1178


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #16 on: 2007-12-19 10:21:05 »

it remove reference on thread, and exiting the run method kill thread , so no reference and thread not running (run method ended) anymore implied that it is freed.

Threads are freed after they terminate. You don't need to worry about having a circular reference. The JVM will automatically detect that both the dead thread and the Applet are ready for garbage collection.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #17 on: 2007-12-19 14:36:47 »

I am just working on a bug I have with Firefox JRE 1.6.0.2... using severals Applets without closing browser, and believe me,  unfortunatly Applets are "not freed" on FireFox....  releasing all reference in destroy method make the heap grow slower but doesn't resolve all problemes on FF, IE just works fine...

this is bunch of the code (maybe there is some stupids things in it as it is experimental, I am still working on it) I am working on to make applet released on FF:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
public void destroy()
{
      /*  
      Container c=this.getParent();
      Container f=this;
      while(c!=null)
      {
         c.remove(f);
         f=c;
         c=c.getParent();        
      }
      */

     
      ThreadGroup tg=Thread.currentThread().getThreadGroup();
      while(tg!=null)
      {
         System.out.println(" threadgroug " + tg);
         Thread[] list=new Thread[100];
         int nb=tg.enumerate(list);
         for(int n=0;n<nb;n++)
         {
            System.out.println(" stop thread " + n);
            list[n].stop();
         }
         tg.destroy();
         tg.list();
         
         tg=tg.getParent();
      }
}


Using the above code I am able to open 5/6 times more applets, but there is still sometime an OutOfMemory exception that happen...

Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.137 seconds with 22 queries.