Advertisement
Khadija_Assem

Untitled

Dec 18th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. package sample;
  2.  
  3. import javafx.scene.image.Image;
  4.  
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.lang.reflect.InvocationTargetException;
  9. import java.lang.reflect.Method;
  10. import java.net.URL;
  11. import java.net.URLClassLoader;
  12. import java.util.*;
  13. import java.util.jar.JarEntry;
  14. import java.util.jar.JarFile;
  15.  
  16. public class loadPhotos {
  17.  
  18. Map<String,GameObject> images;
  19. @SuppressWarnings("resource")
  20. public Map<String, GameObject> getClassNamesFromJar(String JarName) {
  21. JarName = "lib\\" + JarName;
  22. List<Class<?>> listofClasses = new ArrayList<Class<?>>();
  23. images=new HashMap<>();
  24. try {
  25. File pathToJar = new File(JarName);
  26. JarFile jarFile;
  27. jarFile = new JarFile(pathToJar);
  28. Enumeration<JarEntry> e = jarFile.entries();
  29. URL[] urls = { new URL("jar:file:" + pathToJar + "!/") };
  30. URLClassLoader cl = URLClassLoader.newInstance(urls);
  31. // while (e.hasMoreElements()) {
  32. JarEntry je = e.nextElement();
  33. // if (je.isDirectory() || !je.getName().endsWith(".class")) {
  34. // continue;
  35. // }
  36. String className = je.getName().substring(0, je.getName().length() - 6);
  37. className = className.replace('/', '.');
  38. String finalName=className.replace("View.Shapes.","");
  39. System.out.println(className);
  40. GameObject iO;
  41. className = "sample.RedBall";
  42. Class exampleClass = Class.forName(className);
  43. try {
  44. iO = (GameObject) exampleClass.getDeclaredConstructor().newInstance();
  45. Method m = exampleClass.getDeclaredMethod("loadImage", null);
  46. System.out.println("Got method: " + m);
  47. iO = (GameObject) m.invoke(iO, null);
  48. images.put(finalName,iO);
  49. System.out.println("Class: "+finalName + " added to the list");
  50. }
  51. catch (Exception E){
  52. // System.out.println("Not Game Object");
  53. }
  54. // }
  55. } catch (IOException | ClassNotFoundException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }
  59.  
  60. return images;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement