我想在一个新的JFrame中在minecraft.jar中运行MinecraftApplet.class。
但是,我问的是如何做到这一点,因为我现有的代码不起作用:
这是加载jar网址的StartScript,然后更新类路径,最后在GameFrame中启动一个线程并添加createApplet。
public class StartScript implements Runnable{
public ClassLoader classLoader;
public boolean natives_loaded;
protected URL[] urlList;
public void run()
{
try
{
String path = (String)AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws Exception {
return InfiniLauncherUtils.getWorkingDirectory() + File.separator + "bin" + File.separator;
}});
File dir = new File(path);
if (!dir.exists())
{
dir.mkdirs();
}
loadJarURLs();
updateClassPath(dir);
Thread start = new Thread(new InfiniLauncherGameFrame(createApplet()));
start.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Applet createApplet() throws InstantiationException, IllegalAccessException, ClassNotFoundException
{
Class appletClass = classLoader.loadClass("net.minecraft.client.MinecraftApplet");
return (Applet)appletClass.newInstance();
}
protected void updateClassPath(File dir)throws Exception
{
URL[] urls = new URL[this.urlList.length];
for (int i = 0; i < this.urlList.length; i++) {
urls[i] = new File(dir, getJarName(this.urlList[i])).toURI().toURL();
System.out.println(urlList[i]);
}
if (classLoader == null) {
classLoader = new URLClassLoader(urls) {
protected PermissionCollection getPermissions(CodeSource codesource)
{
PermissionCollection perms = null;
try
{
Method method = SecureClassLoader.class.getDeclaredMethod("getPermissions", new Class[] { CodeSource.class });
method.setAccessible(true);
perms = (PermissionCollection)method.invoke(getClass().getClassLoader(), new Object[] {
codesource });
String host = "www.minecraft.net";
if ((host != null) && (host.length() > 0))
{
perms.add(new SocketPermission(host, "connect,accept"));
} else codesource.getLocation().getProtocol().equals("file");
perms.add(new FilePermission("<<ALL FILES>>", "read"));
}
catch (Exception e)
{
e.printStackTrace();
}
return perms;
}
};
}
String path = dir.getAbsolutePath();
if (!path.endsWith(File.separator)) path = path + File.separator;
unloadNatives(path);
System.setProperty("org.lwjgl.librarypath", path + "natives");
System.setProperty("net.java.games.input.librarypath", path + "natives");
natives_loaded = true;
}
protected void loadJarURLs() throws Exception {
String jarList[] = {"lwjgl.jar", "jinput.jar", "lwjgl_util.jar", LoginScript.mainGameURL};
//StringTokenizer jar = new StringTokenizer(jarList, ", ");
int jarCount = jarList.length+1;
this.urlList = new URL[jarCount];
URL path = new URL("http://s3.amazonaws.com/MinecraftDownload/");
for (int i = 0; i < jarCount - 1; i++) {
this.urlList[i] = new URL(path, jarList[i]);
System.out.println(urlList[i]);
}
String osName = System.getProperty("os.name");
String nativeJar = null;
if (osName.startsWith("Win"))
nativeJar = "windows_natives.jar.lzma";
else if (osName.startsWith("Linux"))
nativeJar = "linux_natives.jar.lzma";
else if (osName.startsWith("Mac"))
nativeJar = "macosx_natives.jar.lzma";
else if ((osName.startsWith("Solaris")) || (osName.startsWith("SunOS")))
nativeJar = "solaris_natives.jar.lzma";
else {
}
if (nativeJar == null) {
} else {
this.urlList[(jarCount - 1)] = new URL(path, nativeJar);
}
}
protected String getJarName(URL url)
{
System.out.println(url);
String fileName = url.toString();
if (fileName.contains("?"))
{
fileName = fileName.substring(0, fileName.indexOf("?"));
}
if (fileName.endsWith(".pack.lzma"))
{
fileName = fileName.replaceAll(".pack.lzma", "");
}
else if (fileName.endsWith(".pack"))
{
fileName = fileName.replaceAll(".pack", "");
}
else if (fileName.endsWith(".lzma"))
{
fileName = fileName.replaceAll(".lzma", "");
}
return fileName.substring(fileName.lastIndexOf('/') + 1);
}
private void unloadNatives(String nativePath)
{
if (!natives_loaded) {
return;
}
try
{
Field field = ClassLoader.class.getDeclaredField("loadedLibraryNames");
field.setAccessible(true);
Vector libs = (Vector)field.get(getClass().getClassLoader());
String path = new File(nativePath).getCanonicalPath();
for (int i = 0; i < libs.size(); i++) {
String s = (String)libs.get(i);
if (s.startsWith(path)) {
libs.remove(i);
i--;
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
GameFrame类
public InfiniLauncherGameFrame(Applet aapplet)
{
applet = aapplet;
System.out.println("ran");
}
public void run()
{
System.out.println("few");
removeAll();
add(applet);
validate();
setVisible(true);
setSize(300, 300);
}
我运行时没有出现任何错误,但是新框架为黑色,左上方有一个小白框。
我可能不会收到错误,因为我抛弃了一些例外。
非常感谢任何帮助。
答案 0 :(得分:0)
也许是一个愚蠢的建议,但可能是因为你没有明确调用repaint()方法?这件事发生在我身上很多