Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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 code...  (Read 8160 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Posted 2007-12-11 01: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
« Reply #1 - Posted 2007-12-11 05: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 Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #2 - Posted 2007-12-11 07: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline woogley
« Reply #3 - Posted 2007-12-11 12: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 Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #4 - Posted 2007-12-11 12: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
« Reply #5 - Posted 2007-12-11 13: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 Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #6 - Posted 2007-12-11 13:48:47 »

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

Play Minecraft!
Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #7 - Posted 2007-12-11 15: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

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline DzzD
« Reply #8 - Posted 2007-12-11 19: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();
}




Online kevglass

JGO Kernel


Medals: 165
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2007-12-11 19: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #10 - Posted 2007-12-11 20:03:57 »

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

Online Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2007-12-11 21: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
« Reply #12 - Posted 2007-12-12 03: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
« Reply #13 - Posted 2007-12-12 13: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 Wizard


Medals: 15
Projects: 19


Mojang Specifications


« Reply #14 - Posted 2007-12-12 14:07:50 »

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

Play Minecraft!
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #15 - Posted 2007-12-12 16: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 Coder


Projects: 1


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


« Reply #16 - Posted 2007-12-19 15: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
« Reply #17 - Posted 2007-12-19 19: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (103 views)
2014-09-10 06:39:09

Tekkerue (51 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!