Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Converting Java desktop game to applet?  (Read 366 times)
0 Members and 1 Guest are viewing this topic.
Offline VaPS

Junior Member


Medals: 1



« Posted 2013-11-12 00:11:10 »

I have a desktop Java game I'd like to run on a browser. Some people say I need to use Applet or JApplet for this but I haven't caught a straight answer.

I'm using JFrame, BufferStrategy & Thread if that's relevant in any way and here's the code of interest:
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  
public class Game extends Canvas implements Runnable {
   
   public static void main(String[] args) {
     
      Game g = new Game();
      g.addKeyListener(new Input());
      g.setFocusable(true);
     
      JFrame f = new JFrame("OMG A TITLE");
      f.add(g);
      f.pack();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setSize(W, H);
      f.setResizable(false);
      f.setLocationRelativeTo(null);
      f.setIconImage(icon);
      f.setVisible(true);
     
      g.start();
     
   }
   
   public void start() {
      new Thread(this).start();
   }
   
   public void run() {
      // updatin' 'n' stuff..
  }
   
   public void render() {
     
      BufferStrategy bs = getBufferStrategy();
      if (bs == null) {
         createBufferStrategy(2);
         return;
      }
      g = bs.getDrawGraphics();
     
      Room.render(); // rendering the graphics with the use of g
     
      g.dispose();
      bs.show();
     
   }
   
}

(Let me know if the code is not so relevant.)

I also have a class for handling key inputs by implementing KeyListener.

The question, what should I change in the code in order for it to be runnable in a browser?
Offline jonjava
« Reply #1 - Posted 2013-11-12 00:15:41 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Apple extends JApplet {
   public void start() {
      Game g = new Game();
      // add listeners etc
     
      this.add( g );
      // request focus etc, possibly set size

      g.start();
   }
 
   // play(), stop() etc
}


Should pretty much do the trick I think.

Offline VaPS

Junior Member


Medals: 1



« Reply #2 - Posted 2013-11-12 00:18:50 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Apple extends JApplet {
   public void start() {
      Game g = new Game();
      // add listeners etc
     
      this.add( g );
      // request focus etc, possibly set size

      g.start();
   }
 
   // play(), stop() etc
}


Should pretty much do the trick I think.
1  
public class Game extends Canvas extends JApplet implements Runnable {

Syntax error on token "extends", . expected

I don't think you can extend a class twice, so any idea how I could work around this?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2013-11-12 00:19:51 »

Well yea, you have to make a new class, Apple, for example.

Since your Game class extends the Canvas class - you can simply use the JApplets "add()" method (which basically works exactly like JFrame add() method) to add your game (which is a Canvas).

Offline VaPS

Junior Member


Medals: 1



« Reply #4 - Posted 2013-11-12 00:33:22 »

Well yea, you have to make a new class, Apple, for example.

Since your Game class extends the Canvas class - you can simply use the JApplets "add()" method (which basically works exactly like JFrame add() method) to add your game (which is a Canvas).
Didn't catch that, my bad!
I typed this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
import javax.swing.JApplet;

public class GGame extends JApplet {

   private static final long serialVersionUID = 1L;

   public static void main(String[] args) {
      new GGame().init();
   }

   public void init() {
      Game g = new Game();
      this.add(g);
      Game.init();
   }

}

(Game.init() is the same code in the OP that creates the JFrame and whatnot.)

I don't have the time to test it online but when I compiled it Eclipse suggested running it as an application and rendering, input & everything worked perfectly.
Thank you for your help!
Offline jonjava
« Reply #5 - Posted 2013-11-12 01:05:25 »

Applets don't start from the conventional:

1  
public static void main(String[] args) {}


Instead, applets have special methods that are called by the browser automatically when certain things happen. Mainly these methods are:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
void init()
//Called by the browser or applet viewer to inform this applet that it has been loaded into the system.

void start()
//Called by the browser or applet viewer to inform this applet that it should start its execution.

void stop()
//Called by the browser or applet viewer to inform this applet that it should stop its execution

void destroy()
//Called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated.


source: http://docs.oracle.com/javase/7/docs/api/java/applet/Applet.html

In other words, in the code that you posted the "public static void main()" method is never called - but it still works because you are Overriding the applets own "init()" method (which gets called by the browser/applet viewer when the applet has been loaded).

Online SHC
« Reply #6 - Posted 2013-11-12 14:07:54 »

This could be of help.

Game.java
GWindow.java

Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (28 views)
2014-04-15 18:08:23

BurntPizza (25 views)
2014-04-15 03:46:01

UprightPath (40 views)
2014-04-14 17:39:50

UprightPath (22 views)
2014-04-14 17:35:47

Porlus (38 views)
2014-04-14 15:48:38

tom_mai78101 (62 views)
2014-04-10 04:04:31

BurntPizza (121 views)
2014-04-08 23:06:04

tom_mai78101 (221 views)
2014-04-05 13:34:39

trollwarrior1 (188 views)
2014-04-04 12:06:45

CJLetsGame (195 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!