Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample;
- import javafx.scene.image.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.util.*;
- import java.util.jar.JarEntry;
- import java.util.jar.JarFile;
- public class loadPhotos {
- Map<String,GameObject> images;
- @SuppressWarnings("resource")
- public Map<String, GameObject> getClassNamesFromJar(String JarName) {
- JarName = "lib\\" + JarName;
- List<Class<?>> listofClasses = new ArrayList<Class<?>>();
- images=new HashMap<>();
- try {
- File pathToJar = new File(JarName);
- JarFile jarFile;
- jarFile = new JarFile(pathToJar);
- Enumeration<JarEntry> e = jarFile.entries();
- URL[] urls = { new URL("jar:file:" + pathToJar + "!/") };
- URLClassLoader cl = URLClassLoader.newInstance(urls);
- // while (e.hasMoreElements()) {
- JarEntry je = e.nextElement();
- // if (je.isDirectory() || !je.getName().endsWith(".class")) {
- // continue;
- // }
- String className = je.getName().substring(0, je.getName().length() - 6);
- className = className.replace('/', '.');
- String finalName=className.replace("View.Shapes.","");
- System.out.println(className);
- GameObject iO;
- className = "sample.RedBall";
- Class exampleClass = Class.forName(className);
- try {
- iO = (GameObject) exampleClass.getDeclaredConstructor().newInstance();
- Method m = exampleClass.getDeclaredMethod("loadImage", null);
- System.out.println("Got method: " + m);
- iO = (GameObject) m.invoke(iO, null);
- images.put(finalName,iO);
- System.out.println("Class: "+finalName + " added to the list");
- }
- catch (Exception E){
- // System.out.println("Not Game Object");
- }
- // }
- } catch (IOException | ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return images;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement