I have created a Swing game in java that works correctly when executed on the desktop. However, due to unforeseen events, my boss is now requiring that the game be turned into an applet so that it can be embedded within a website (No, JWS is not an option).
Here are the steps that I took to convert the game into an applet:
1) Changed my main class to have public void init() rather than public static void main(), and extended it from Applet
2) Changed my myJFrame.java class to now extend from Applet, rather than from JFrame.
My applet code is as follows:
import java.applet.Applet;
public class app extends Applet{
@Override
public void init() {
myJFrame mjf = new myJFrame();
}
}
import java.applet.Applet;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class myJFrame extends Applet{
private final MainMenu main;
private SidePanel side;
public myJFrame() {
try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );}
catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException e) {}
main = new MainMenu(this);
add(main, "Center");
setSize(1080,720);
setVisible(true);
}
...
}
Now, my index.php code for embedding the applet
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<object type="application/x-java-applet" name="psych-game" width="1080"height="720">
<param name="code" value="app.java" />
<param name="archive" value="PsychGame.jar" />
</object>
I am not receiving an error message, just simply a blank page.
This is my first time working with an applet, so my apologies if I am missing a simple step. Thanks for your help.
In the case of an applet you can only extend the class once and from there after you have to pass it to the others, e.g.:
import java.applet.JApplet;
public class app extends JApplet{
@Override
public void init() {
myJFrame mjf = new myJFrame(this);
}
}
And then for subsequent classes note the constructor arg:
import java.applet.JApplet;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class myJFrame {
private final MainMenu main;
private SidePanel side;
private JApplet parent;
public myJFrame(JApplet ja) {
parent = ja;
...
}
...
}
All uses in the class are then prepended with parent.whatever