问题
First off, this is for my web design class. I am doing a sort of "promotion website" for the game I made in my Computer Science 2 final for the final in this class. Everything is all good, except that I wanted to add a feature where you could play the game from the browser. The website is all in a local folder, and he will be examining it on his computer, so everything will be local, no servers. How would I go about adding the game to the website?
回答1:
You will need java applets for your java code to run in browser. Here is some intro about applets: http://docs.oracle.com/javase/tutorial/deployment/applet/
Here is a simple example:
Your java code:
import java.applet.*;
import java.awt.*;
public class Main extends Applet{
public void paint(Graphics g){
g.drawString("Welcome in Java Applet.",40,20);
}
}
Compiling the code will generate a .class file. For example: Main.class
Then embed the Main.class file in your browser:
<HTML>
<HEAD>
My game applet
</HEAD>
<BODY>
<div >
<APPLET CODE="Main.class" WIDTH="800" HEIGHT="500"></APPLET>
</div>
</BODY>
</HTML>
Some basic tutorials here: http://www.dreamincode.net/forums/topic/229033-introduction-to-java-applets/
Another way: Java Web Start
Use Java Web Start which allows applications to be launched through browsers or via the Java Network Launching Protocol.
Some valuable resources here: https://stackoverflow.com/tags/java-web-start/info
来源:https://stackoverflow.com/questions/41029236/how-do-i-embed-a-java-program-into-my-website