Loading a .Jar Applet inside of a JFrame(Panel)

£可爱£侵袭症+ 提交于 2019-12-02 07:41:43

Since you haven't provided the site which is being used I'll only explain some parts.

Firstly you'll need to download the games .JAR file or have it accessible.

Secondly you'll need to have knowledge of the main class typically it's called "main.class".

Thirdly you'll need code similar to the below to load the .jar files main file. Once again changing the "main.class" variable to the actually ".class".

URL url[] = { 
    new File(directory.concat("/gamepack.jar")).toURI().toURL() 
};

URLClassLoader classLoader = new URLClassLoader(url);
applet = (Applet)classLoader.loadClass("main").newInstance(); 
applet.setBounds(0, 0, width, height);

applet.setBackground(Color.BLACK);
applet.setStub(stub);

applet.init();
applet.start();

mainFrame.getContentPane().add(applet);

You'll notice the method "applet.setStub(stub)". You'll need to create an AppletStub class. Something similar to do the below will suffice.

package com;

import java.util.Map;
import java.util.HashMap;
import java.net.URL;
import java.applet.AppletStub;
import java.applet.AppletContext;

public class AppletEnvironment implements AppletStub
{
private final Map<String, String> PARAMETERS = new HashMap<String, String>();
private final URL URLBASE;

public AppletEnvironment(final URL URLBASE)
{
    this.URLBASE = URLBASE;
}

public boolean put(String key, String param)
{
    if (PARAMETERS.containsKey(key))
        return false;
    PARAMETERS.put(key, param);
    return true;
}

@Override
public String getParameter(String name) 
{
    return PARAMETERS.get(name);
}

@Override
public URL getCodeBase() 
{
    return URLBASE;
}

@Override
public URL getDocumentBase() 
{
    return URLBASE;
}

@Override
public boolean isActive() 
{
    return true;
}

@Override
public AppletContext getAppletContext()
{
    return null;
}

@Override
public void appletResize(int width, int height) 
{

}
}

Now to make that class work you'll need something like the below. So you can create the "stub" variable.

AppletEnvironment stub = new AppletEnvironment(url);

You'll notice the AppletEnvironment class has a method "put(String key, String param)". This must be done correctly. When you parse the website you'll find there are client parameters which are used to generate the client. If none are found then you can ignore this. If you'd like to see what ones are needed.

Then add the following code in the "getParameter(String name);" method.

System.out.println(name);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!