Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package me.mayuan.tiny.img;
  2.  
  3. import java.lang.annotation.Annotation;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.HashMap;
  7.  
  8. import com.badlogic.gdx.graphics.Texture;
  9. import com.badlogic.gdx.graphics.g2d.Sprite;
  10.  
  11. public class Images {
  12.    
  13.     StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
  14.     StackTraceElement e = stacktrace[2];//coz 0th will be getStackTrace so 1st
  15.     String methodName = e.getMethodName();
  16.    
  17.     @Image(texture="badlogic.jpg")
  18.     public Sprite badlogic(float width, float height, float x, float y) {
  19.         Texture t = getTexture(methodName);
  20.         Sprite s = new Sprite(t);
  21.         s.setSize(width, height);
  22.         s.setPosition(x, y);
  23.         return s;
  24.     }
  25.    
  26.     public Texture getTexture(String name) {
  27.         Method[] methods = this.getClass().getDeclaredMethods();
  28.         for(Method m : methods) {
  29.             if(m.isAnnotationPresent(Image.class)) {
  30.                 if(m.getName().equalsIgnoreCase(name)) {
  31.                     Annotation an = (Annotation)m.getDeclaredAnnotation(Image.class);
  32.                     Image img = (Image)an;
  33.                     return new Texture(img.texture());
  34.             }
  35.         }
  36.         }
  37.         return null;
  38.     }
  39.    
  40.     public HashMap<String,Sprite> getSpriteMap() {
  41.         Images images = this;
  42.         Method[] methods = this.getClass().getDeclaredMethods();
  43.         HashMap<String,Sprite> sprites = new HashMap<String,Sprite>();
  44.         for(Method m : methods) {
  45.             if(m.isAnnotationPresent(Image.class)) {
  46.                 try {
  47.                     sprites.put(m.getName(),(Sprite) m.invoke(images, null));
  48.                 } catch (IllegalAccessException e) {
  49.                     // TODO Auto-generated catch block
  50.                     e.printStackTrace();
  51.                 } catch (IllegalArgumentException e) {
  52.                     // TODO Auto-generated catch block
  53.                     e.printStackTrace();
  54.                 } catch (InvocationTargetException e) {
  55.                     // TODO Auto-generated catch block
  56.                     e.printStackTrace();
  57.                 }
  58.                
  59.             }
  60.         }
  61.         return sprites;
  62.        
  63.     }
  64.    
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement