Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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 to javascript woes..  (Read 3151 times)
0 Members and 1 Guest are viewing this topic.
Offline Drakkheim

Senior Newbie





« Posted 2011-04-06 20:56:38 »

So I'm working on making a game using Slick as an applet, but seem to have gotten myself stumped. :-/

I've got the basic applet up and running in a webpage  however when it comes to having the applet call a Javascript function and vice versa, all I can manage to do is to hang the java plugin and lock up the browser.   Eventually (in Chrome at least) the browser detects that the Java plugin is not responding and asks me if I want to kill it.  I do so and then the webpage runs the function.

I'm using a statebased game and in my main gamestate  GameBoard's init I do this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public void init(GameContainer gc, StateBasedGame sbg) 
         throws SlickException {
      Log.info("Init Board");
      if (gc instanceof AppletGameContainer.Container) {
         Applet applet = ((AppletGameContainer.Container) gc).getApplet();
         jso = JSObject.getWindow(applet);
         if(jso != null )
            try {
               jso.call("Update", new String[] {"Hello from Applet"});
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
         Log.info("APPLET");
         Log.info(applet.getParameter("name"));
         //applet.getParameterInfo()
      }
... ... more init junk ....


The culprit is the jso.call line..
I've tried it in the Update portion of the class,  Signing Jars, making sure the Applet Tag has the MAYSCRIPT in it..

At this point I've read every piece of documentation I can find and am pretty much at my wits end.   Any suggestions would be more than welcome.
Offline DzzD
« Reply #1 - Posted 2011-04-06 21:07:57 »

in chrome if you call a java method it often lock/hang (showing dialog) until you wait that applet is fully loaded before calling java from JS

even doing in JS :
var test=document.getElementById('myApplet').anything ; //may lock/hang

Offline Drakkheim

Senior Newbie





« Reply #2 - Posted 2011-04-06 21:30:26 »

I tossed in a counter so it would try the call out to javascript on the 1,000th update..  which lets me play around in the game and have the applet working a bit to make sure it's finished loading it's images etc.. but no luck it still seizes up Sad
I was able to add a bunch of Log messages in there and its definatly the:
 jso.call("Update", new String[] {"Hello from Applet"});
line that's causing the hang.
I tried
JSObject response = (JSObject) jso.eval("Update('hullo');");
and in gives the same response.


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #3 - Posted 2011-04-06 21:35:57 »

strange but not an expert in slick does this one work well ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
import java.awt.*;
import java.applet.*;
import java.net.*;

public class JS extends Applet
{
   

   public void paint(Graphics g)
   {
      try
      {
       this.getAppletContext().showDocument(new URL("Javascript:alert('Hello from Applet')"));  
      }
      catch(Throwable t)
      {
         t.printStackTrace();
      }
   }
}

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2011-04-06 21:42:15 »

strange but not an expert in slick does this one work well ?

1  
this.getAppletContext().showDocument(new URL("Javascript:alert('Hello from Applet')"));   


Not in the Opera browser.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #5 - Posted 2011-04-06 21:43:09 »

strange but not an expert in slick does this one work well ?

1  
this.getAppletContext().showDocument(new URL("Javascript:alert('Hello from Applet')"));   


Not in the Opera browser.
yup but I notice that OP use chrome ^^, just to ensure there is no prob with liveconnect  

Offline Drakkheim

Senior Newbie





« Reply #6 - Posted 2011-04-06 21:51:54 »

Yeah that worked.
I made that JS the al_main parameter and got a nice little javascript alert.

:-/ so that appears to be working.
Offline Drakkheim

Senior Newbie





« Reply #7 - Posted 2011-04-06 21:55:40 »

The applet code I'm running is virtually identical to the one on the Slick Wiki tutorial
here: http://slick.cokeandcode.com/wiki/doku.php?id=03_-_setting_up_the_web-page
With the exception of having added MAYSCRIPT to the applet tag  using my class in the <game> parameter.
Offline DzzD
« Reply #8 - Posted 2011-04-06 21:55:59 »

you got free time ? me too Smiley maybe try ...
1  
jso = JSObject.getWindow(applet); 

replaced by
1  
jso = JSObject.getWindow(this); 


EDIT:
MAYSCRIPT  was ignored in some browsers for a while ( it seems to be requiered now in some case...WTF....), and it should not change anything (but this is only conditional)


EDIT2:
mabe a new plugin different classloader issue ?

Offline Drakkheim

Senior Newbie





« Reply #9 - Posted 2011-04-06 22:04:28 »

Unfortunately that won't compile. 
The method getWindow(Applet) in the type JSObject is not applicable for the arguments (GameBoard)

If I understand the way things work (and I may be horribly horribly wrong) is that
When you call the slick.AppletGameContainer from the Applet tag it wraps itself around the standalone game class (StateBasedGame in this case) and runs the main function. 
The StateBasedGame then makes the canvas and graphics and then passes them to the individual game states...(GameBoard in this case)  which is where I'm trying to run the call.

exactly how any of that works .. well.. is kinda fuzzy to me.  Undecided
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #10 - Posted 2011-04-06 22:07:10 »

MAYSCRIPT  was ignored in some browsers for a while ( it seems to be requiered now in some case...WTF....), and it should not change anything (but this is only conditional)

As of java plugin2 MAYSCRIPT no longer does anything and javascript communication is enabled by default.
I am pretty sure I have experienced problem without mayscript recently, will look if I can remember how (not sure but probably on FF, will let you know)

EDIT: or maybe safari...

Online kappa
« League of Dukes »

JGO Kernel


Medals: 78
Projects: 15


★★★★★


« Reply #11 - Posted 2011-04-06 22:07:44 »

MAYSCRIPT  was ignored in some browsers for a while ( it seems to be requiered now in some case...WTF....), and it should not change anything (but this is only conditional)

As of java plugin2, MAYSCRIPT no longer does anything and javascript communication is enabled by default (as far as I know there is no way to disable it anymore).


In Slick2D you can get the applet object from the container using the following:
1  
2  
3  
4  
// you can check if container is an applet 
if (container instanceof AppletGameContainer.Container) {
    // do foo
}

1  
2  
3  
// cast the container to get the applet instance 
Applet applet = ((AppletGameContainer.Container) container).getApplet();
jso = JSObject.getWindow(applet);


You could maybe try calling the javascript from another thread to make sure its not blocking the main thread.
Offline DzzD
« Reply #12 - Posted 2011-04-06 22:25:20 »

another try plz...

Java :

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  
import java.awt.*;
import java.applet.*;
import java.net.*;
import netscape.javascript.*;
public class JS extends Applet
{
   private int n;
   public String test;
   public void paint(Graphics g)
   {
   
      try
      {
         if(n++==0)
         {
          //this.getAppletContext().showDocument(new URL("Javascript:alert('Hello from Applet')"));
          JSObject.getWindow(this).eval("alert('Hello from Applet 2')");  
         }
         g.setColor(Color.black);
         g.drawString(this.test,50,50);
      }
      catch(Throwable t)
      {
         t.printStackTrace();
      }
   }
}



HTML :

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  
<html>
   <head>
   <script language=Javascript>
   
      function testLiveConnect()
      {
         try
         {
            var applet=document.getElementById('appletTest');
            applet.test="from javascript";
            applet.repaint();
         }
         catch(exception)
         {
            alert(exception);
         }
     
      }
   </script>
   </head>
   <body onload="testLiveConnect()">
         <applet
            id="appletTest"
            code   = "JS.class"
            width   = "500"
            height   = "300"
            >
         </applet>
   </body>
</html>

Offline DzzD
« Reply #13 - Posted 2011-04-06 22:35:01 »

MAYSCRIPT  was ignored in some browsers for a while ( it seems to be requiered now in some case...WTF....), and it should not change anything (but this is only conditional)

As of java plugin2, MAYSCRIPT no longer does anything and javascript communication is enabled by default (as far as I know there is no way to disable it anymore).

"hum... that's embarrasing..."

can't reproduce the problem in any browser I got


Notice: for the last sample I posted opera & safari draw the "from javascript" string but do not show any JS popup ( throw a malformed URLException, seems they need a "javascript:" proto or such while other chrome/IE/FF doesn't requiere it )

Offline DzzD
« Reply #14 - Posted 2011-04-06 23:01:28 »

(just an idea, maybe try to do your work in another method than the init)

Offline Drakkheim

Senior Newbie





« Reply #15 - Posted 2011-04-07 02:26:05 »

Woo!
1. Slick Applet calling Javascript functions [Done!]

Thanks for the excellent help guys. 
After reading up a bit on how threading basically works  I was able to start a new thread and in the run() get the Applet and run the jso.call  and it worked like a champ!!!

For posterity http://www.realapplets.com/tutorial/ThreadExample.html is the example that I used to show me how to get the new thread working.

2. .. But going the other direction..

I've found this thread:  http://lwjgl.org/forum/index.php?topic=3266.0 that basically says
Quote
AppletLoader.getApplet() method, can't you use that to get your main class and call methods from it?

...which... i Almost understand... but I don't get how to implement that... Anyone able to clarify a bit?

Thanks ever so much.

Online kappa
« League of Dukes »

JGO Kernel


Medals: 78
Projects: 15


★★★★★


« Reply #16 - Posted 2011-04-07 09:34:51 »

Ok, so you'd like to know how javascript to java communication works.

Give your applet tag an id.

<applet code="org.lwjgl.util.applet.AppletLoader"
        id = "myApplet"
        archive="lwjgl_util_applet.jar"
        codebase="."
        width="640" height="480">

The main applet in this case is the appletloader and you'll need to get the applet loaded by it (the Slick2D applet), this is done as follows.

var appletloader = document.getElementById('myApplet');
var applet = appletloader.getApplet();

That is it, you have the main applet object (which is slicks applet instance) and can call any public method therein using applet.x()

As you want to call methods in your main slick Game class you'll need to get this from slicks applet instance. Unfortunately Slicks API doesn't expose getting the Game object (although you could raise a RFE).

One immediate solution is that you can use a custom AppletGameContainer which exposes this for you.
Online kappa
« League of Dukes »

JGO Kernel


Medals: 78
Projects: 15


★★★★★


« Reply #17 - Posted 2011-04-07 10:39:21 »

just a quick mock up at a custom AppletGameContainer, just put it with your own code and point al_main to it in the html (instead of at the standard AppletGameContainer).

You should then be able to do applet.getGame() which will return your main class and allowing you to call any method therein (e.g. applet.getGame().x() ).


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  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
314  
315  
316  
317  
318  
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339  
340  
341  
342  
343  
344  
345  
346  
347  
348  
349  
350  
351  
352  
353  
354  
355  
356  
357  
358  
359  
360  
361  
362  
363  
364  
365  
366  
367  
368  
369  
370  
371  
372  
373  
374  
375  
376  
377  
378  
379  
380  
381  
382  
383  
384  
385  
386  
387  
388  
389  
390  
391  
392  
393  
394  
395  
396  
397  
398  
399  
400  
401  
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412  
413  
414  
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425  
426  
427  
428  
429  
430  
431  
432  
433  
434  
435  
436  
437  
438  
439  
440  
441  
442  
443  
444  
445  
446  
447  
448  
449  
450  
451  
452  
453  
454  
455  
456  
457  
458  
459  
460  
461  
462  
463  
464  
465  
466  
467  
468  
469  
470  
471  
472  
473  
474  
475  
476  
477  
478  
479  
480  
481  
482  
483  
484  
485  
486  
487  
488  
489  
490  
491  
492  
493  
494  
495  
496  
497  
498  
499  
500  
501  
502  
503  
504  
505  
506  
507  
508  
509  
510  
511  
512  
513  
514  
515  
516  
517  
518  
519  
520  
521  
522  
523  
524  
525  
526  
527  
528  
529  
530  
531  
532  
533  
534  
535  
536  
537  
538  
539  
540  
541  
542  
543  
544  
545  
546  
547  
548  
549  
550  
551  
552  
553  
554  
555  
556  
557  
558  
559  
560  
561  
562  
563  
564  
565  
566  
567  
568  
569  
570  
571  
572  
573  
574  
575  
576  
577  
578  
579  
580  
581  
582  
583  
584  
585  
586  
587  
588  
589  
590  
591  
592  
593  
594  
595  
596  
597  
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Cursor;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
import org.newdawn.slick.Game;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.opengl.CursorLoader;
import org.newdawn.slick.opengl.ImageData;
import org.newdawn.slick.opengl.InternalTextureLoader;
import org.newdawn.slick.util.Log;

/**
 * A game container that displays the game as an applet. Note however that the
 * actual game container implementation is an internal class which can be
 * obtained with the getContainer() method - this is due to the Applet being a
 * class wrap than an interface.
 *
 * @author kevin
 */

public class AppletGameContainer extends Applet {
   /** The GL Canvas used for this container */
   protected ContainerPanel canvas;
   /** The actual container implementation */
   protected Container container;
   /** The parent of the display */
   protected Canvas displayParent;
   /** The thread that is looping for the game */
   protected Thread gameThread;
   /** Alpha background supported */
   protected boolean alphaSupport = true;
   
   /**
    * @see java.applet.Applet#destroy()
    */

   public void destroy() {
      if (displayParent != null) {
         remove(displayParent);
      }
      super.destroy();
     
      Log.info("Clear up");
   }

   /**
    * Clean up the LWJGL resources
    */

   private void destroyLWJGL() {
      container.stopApplet();
     
      try {
         gameThread.join();
      } catch (InterruptedException e) {
         Log.error(e);
      }
   }

   /**
    * @see java.applet.Applet#start()
    */

   public void start() {
     
   }
   
   /**
    * Start a thread to run LWJGL in
    */

   public void startLWJGL() {
      if (gameThread != null) {
         return;
      }
     
      gameThread = new Thread() {
         public void run() {
            try {
               canvas.start();
            }
            catch (Exception e) {
               e.printStackTrace();
               if (Display.isCreated()) {
                  Display.destroy();
               }
               displayParent.setVisible(false);//removeAll();
               add(new ConsolePanel(e));
               validate();
            }
         }
      };
     
      gameThread.start();
   }

   /**
    * @see java.applet.Applet#stop()
    */

   public void stop() {
   }

   /**
    * @see java.applet.Applet#init()
    */

   public void init() {
      removeAll();
      setLayout(new BorderLayout());
      setIgnoreRepaint(true);

      try {
         Game game = (Game) Class.forName(getParameter("game")).newInstance();
         
         container = new Container(game);
         canvas = new ContainerPanel(container);
         displayParent = new Canvas() {
            public final void addNotify() {
               super.addNotify();
               startLWJGL();
            }
            public final void removeNotify() {
               destroyLWJGL();
               super.removeNotify();
            }

         };

         displayParent.setSize(getWidth(), getHeight());
         add(displayParent);
         displayParent.setFocusable(true);
         displayParent.requestFocus();
         displayParent.setIgnoreRepaint(true);
         setVisible(true);
      } catch (Exception e) {
         Log.error(e);
         throw new RuntimeException("Unable to create game container");
      }
   }

   /**
    * Get the GameContainer providing this applet
    *
    * @return The game container providing this applet
    */

   public GameContainer getContainer() {
      return container;
   }
   
   /**
    * Get the Game object for this applet
    *
    * @return The game object in the GameContainer
    */

   public Game getGame() {
      return container.getGame();
   }

   /**
    * Create a new panel to display the GL context
    *
    * @author kevin
    */

   public class ContainerPanel {
      /** The container being displayed on this canvas */
      private Container container;

      /**
       * Create a new panel
       *
       * @param container The container we're running
       */

      public ContainerPanel(Container container) {
         this.container = container;
      }

      /**
       * Create the LWJGL display
       *
       * @throws Exception Failure to create display
       */

      private void createDisplay() throws Exception {
         try {
            // create display with alpha
            Display.create();//new PixelFormat(8,8,GameContainer.stencil ? 8 : 0));
            alphaSupport = true;
         } catch (Exception e) {
            // if we couldn't get alpha, let us know
            alphaSupport = false;
             Display.destroy();
             // create display without alpha
            Display.create();
         }
      }
     
      /**
       * Start the game container
       *
       * @throws Exception Failure to create display
       */

      public void start() throws Exception {
         Display.setParent(displayParent);
         Display.setVSyncEnabled(true);
         
         try {
            createDisplay();
         } catch (LWJGLException e) {
            e.printStackTrace();
            // failed to create Display, apply workaround (sleep for 1 second) and try again
            Thread.sleep(1000);
            createDisplay();
         }
         
         initGL();
         displayParent.requestFocus();
         container.runloop();
      }

      /**
       * Initialise GL state
       */

      protected void initGL() {
         try {
            InternalTextureLoader.get().clear();
            SoundStore.get().clear();

            container.initApplet();
         } catch (Exception e) {
            Log.error(e);
            container.stopApplet();
         }
      }
   }

   /**
    * A game container to provide the applet context
    *
    * @author kevin
    */

   public class Container extends GameContainer {
     
     
      /**
       * Returns the game instance held by the container
       */

      public Game getGame() {
         return game;
      }
     
      /**
       * Create a new container wrapped round the game
       *
       * @param game The game to be held in this container
       */

      public Container(Game game) {
         super(game);

         width = AppletGameContainer.this.getWidth();
         height = AppletGameContainer.this.getHeight();
      }

      /**
       * Initiliase based on Applet init
       *
       * @throws SlickException Indicates a failure to inialise the basic framework
       */

      public void initApplet() throws SlickException {
         initSystem();
         enterOrtho();

         try {
            getInput().initControllers();
         } catch (SlickException e) {
            Log.info("Controllers not available");
         } catch (Throwable e) {
            Log.info("Controllers not available");
         }

         game.init(this);
         getDelta();
      }

      /**
       * Check if the applet is currently running
       *
       * @return True if the applet is running
       */

      public boolean isRunning() {
         return running;
      }

      /**
       * Stop the applet play back
       */

      public void stopApplet() {
         running = false;
      }

      /**
       * @see org.newdawn.slick.GameContainer#getScreenHeight()
       */

      public int getScreenHeight() {
         return 0;
      }

      /**
       * @see org.newdawn.slick.GameContainer#getScreenWidth()
       */

      public int getScreenWidth() {
         return 0;
      }
     
      /**
       * Check if the display created supported alpha in the back buffer
       *
       * @return True if the back buffer supported alpha
       */

      public boolean supportsAlphaInBackBuffer() {
         return alphaSupport;
      }
     
      /**
       * @see org.newdawn.slick.GameContainer#hasFocus()
       */

      public boolean hasFocus() {
         return true;
      }
     
      /**
       * Returns the Applet Object
       * @return Applet Object
       */

      public Applet getApplet() {
         return AppletGameContainer.this;
      }
     
      /**
       * @see org.newdawn.slick.GameContainer#setIcon(java.lang.String)
       */

      public void setIcon(String ref) throws SlickException {
         // unsupported in an applet
      }

      /**
       * @see org.newdawn.slick.GameContainer#setMouseGrabbed(boolean)
       */

      public void setMouseGrabbed(boolean grabbed) {
         Mouse.setGrabbed(grabbed);
      }

     /**
      * @see org.newdawn.slick.GameContainer#isMouseGrabbed()
      */

      public boolean isMouseGrabbed() {
         return Mouse.isGrabbed();
     }
     
      /**
       * @see org.newdawn.slick.GameContainer#setMouseCursor(java.lang.String,
       *      int, int)
       */

      public void setMouseCursor(String ref, int hotSpotX, int hotSpotY) throws SlickException {
         try {
            Cursor cursor = CursorLoader.get().getCursor(ref, hotSpotX, hotSpotY);
            Mouse.setNativeCursor(cursor);
         } catch (Throwable e) {
            Log.error("Failed to load and apply cursor.", e);
         throw new SlickException("Failed to set mouse cursor", e);
         }
      }

      /**
       * Get the closest greater power of 2 to the fold number
       *
       * @param fold The target number
       * @return The power of 2
       */

      private int get2Fold(int fold) {
          int ret = 2;
          while (ret < fold) {
              ret *= 2;
          }
          return ret;
      }
     
      /**
       * {@inheritDoc}
       */

      public void setMouseCursor(Image image, int hotSpotX, int hotSpotY) throws SlickException {
          try {
             Image temp = new Image(get2Fold(image.getWidth()), get2Fold(image.getHeight()));
             Graphics g = temp.getGraphics();
             
             ByteBuffer buffer = BufferUtils.createByteBuffer(temp.getWidth() * temp.getHeight() * 4);
             g.drawImage(image.getFlippedCopy(false, true), 0, 0);
             g.flush();
             g.getArea(0,0,temp.getWidth(),temp.getHeight(),buffer);
             
             Cursor cursor = CursorLoader.get().getCursor(buffer, hotSpotX, hotSpotY,temp.getWidth(),temp.getHeight());
             Mouse.setNativeCursor(cursor);
          } catch (Throwable e) {
             Log.error("Failed to load and apply cursor.", e);
           throw new SlickException("Failed to set mouse cursor", e);
          }
       }
     
      /**
       * @see org.newdawn.slick.GameContainer#setIcons(java.lang.String[])
       */

      public void setIcons(String[] refs) throws SlickException {
         // unsupported in an applet
      }

      /**
       * @see org.newdawn.slick.GameContainer#setMouseCursor(org.newdawn.slick.opengl.ImageData, int, int)
       */

      public void setMouseCursor(ImageData data, int hotSpotX, int hotSpotY) throws SlickException {
         try {
            Cursor cursor = CursorLoader.get().getCursor(data, hotSpotX, hotSpotY);
            Mouse.setNativeCursor(cursor);
         } catch (Throwable e) {
            Log.error("Failed to load and apply cursor.", e);
         throw new SlickException("Failed to set mouse cursor", e);
         }
      }

      /**
       * @see org.newdawn.slick.GameContainer#setMouseCursor(org.lwjgl.input.Cursor, int, int)
       */

      public void setMouseCursor(Cursor cursor, int hotSpotX, int hotSpotY) throws SlickException {
         try {
            Mouse.setNativeCursor(cursor);
         } catch (Throwable e) {
            Log.error("Failed to load and apply cursor.", e);
         throw new SlickException("Failed to set mouse cursor", e);
         }
      }

      /**
       * @see org.newdawn.slick.GameContainer#setDefaultMouseCursor()
       */

      public void setDefaultMouseCursor() {
      }

      public boolean isFullscreen() {
         return Display.isFullscreen();
      }

      public void setFullscreen(boolean fullscreen) throws SlickException {
         if (fullscreen == isFullscreen()) {
            return;
         }

         try {
            if (fullscreen) {
               // get current screen resolution
               int screenWidth = Display.getDisplayMode().getWidth();
               int screenHeight = Display.getDisplayMode().getHeight();

               // calculate aspect ratio
               float gameAspectRatio = (float) width / height;
               float screenAspectRatio = (float) screenWidth
                     / screenHeight;

               int newWidth;
               int newHeight;

               // get new screen resolution to match aspect ratio

               if (gameAspectRatio >= screenAspectRatio) {
                  newWidth = screenWidth;
                  newHeight = (int) (height / ((float) width / screenWidth));
               } else {
                  newWidth = (int) (width / ((float) height / screenHeight));
                  newHeight = screenHeight;
               }

               // center new screen
               int xoffset = (screenWidth - newWidth) / 2;
               int yoffset = (screenHeight - newHeight) / 2;

               // scale game to match new resolution
               GL11.glViewport(xoffset, yoffset, newWidth, newHeight);

               enterOrtho();

               // fix input to match new resolution
               this.getInput().setOffset(
                     -xoffset * (float) width / newWidth,
                     -yoffset * (float) height / newHeight);

               this.getInput().setScale((float) width / newWidth,
                     (float) height / newHeight);

               width = screenWidth;
               height = screenHeight;
               Display.setFullscreen(true);
            } else {
               // restore input
               this.getInput().setOffset(0, 0);
               this.getInput().setScale(1, 1);
               width = AppletGameContainer.this.getWidth();
               height = AppletGameContainer.this.getHeight();
               GL11.glViewport(0, 0, width, height);

               enterOrtho();

               Display.setFullscreen(false);
            }
         } catch (LWJGLException e) {
            Log.error(e);
         }

      }

      /**
       * The running game loop
       *
       * @throws Exception Indicates a failure within the game's loop rather than the framework
       */

      public void runloop() throws Exception {
         while (running) {
            int delta = getDelta();

            updateAndRender(delta);

            updateFPS();
            Display.update();
         }

         Display.destroy();
      }
   }
   
   /**
    * A basic console to display an error message if the applet crashes.
    * This will prevent the applet from just freezing in the browser
    * and give the end user an a nice gui where the error message can easily
    * be viewed and copied.
    */

   public class ConsolePanel extends Panel {
      /** The area display the console output */
      TextArea textArea = new TextArea();
     
      /**
       * Create a new panel to display the console output
       *
       * @param e The exception causing the console to be displayed
       */

      public ConsolePanel(Exception e) {
         setLayout(new BorderLayout());
         setBackground(Color.black);
         setForeground(Color.white);
         
         Font consoleFont = new Font("Arial", Font.BOLD, 14);
         
         Label slickLabel = new Label("SLICK CONSOLE", Label.CENTER);
         slickLabel.setFont(consoleFont);
         add(slickLabel, BorderLayout.PAGE_START);
         
         StringWriter sw = new StringWriter();
         e.printStackTrace(new PrintWriter(sw));
         
         textArea.setText(sw.toString());
         textArea.setEditable(false);
         add(textArea, BorderLayout.CENTER);
         
         // add a border on both sides of the console
         add(new Panel(), BorderLayout.LINE_START);
         add(new Panel(), BorderLayout.LINE_END);
         
         Panel bottomPanel = new Panel();
         bottomPanel.setLayout(new GridLayout(0, 1));
         Label infoLabel1 = new Label("An error occured while running the applet.", Label.CENTER);
         Label infoLabel2 = new Label("Plese contact support to resolve this issue.", Label.CENTER);
         infoLabel1.setFont(consoleFont);
         infoLabel2.setFont(consoleFont);
         bottomPanel.add(infoLabel1);
         bottomPanel.add(infoLabel2);
         add(bottomPanel, BorderLayout.PAGE_END);
      }
   }
}


Offline Drakkheim

Senior Newbie





« Reply #18 - Posted 2011-04-07 14:26:58 »

Thank you so much!

The one little thing I'd missed was that appletloader.getApplet(); is called in the javascript... which totally makes sense in hindsight. Smiley 
My brain had skipped the point apparently.

And thank you so much for custom AppletGameContainer mockup.

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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!