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

后端 未结 1 1622
慢半拍i
慢半拍i 2021-01-27 07:54

I\'m trying to make a JFrameable \"Loader\" of one of my favorite Java games, but I don\'t know how to load the actual .Jar into a JFrame panel (I was told this was

相关标签:
1条回答
  • 2021-01-27 08:41

    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);
    
    0 讨论(0)
提交回复
热议问题