Converting Java desktop game to applet?

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:

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?

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.

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?

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:

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!

Applets don’t start from the conventional:

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:

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).

This could be of help.

Game.java
GWindow.java