Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.mayuan.tiny.img;
- import java.lang.annotation.Annotation;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.HashMap;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- public class Images {
- StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
- StackTraceElement e = stacktrace[2];//coz 0th will be getStackTrace so 1st
- String methodName = e.getMethodName();
- @Image(texture="badlogic.jpg")
- public Sprite badlogic(float width, float height, float x, float y) {
- Texture t = getTexture(methodName);
- Sprite s = new Sprite(t);
- s.setSize(width, height);
- s.setPosition(x, y);
- return s;
- }
- public Texture getTexture(String name) {
- Method[] methods = this.getClass().getDeclaredMethods();
- for(Method m : methods) {
- if(m.isAnnotationPresent(Image.class)) {
- if(m.getName().equalsIgnoreCase(name)) {
- Annotation an = (Annotation)m.getDeclaredAnnotation(Image.class);
- Image img = (Image)an;
- return new Texture(img.texture());
- }
- }
- }
- return null;
- }
- public HashMap<String,Sprite> getSpriteMap() {
- Images images = this;
- Method[] methods = this.getClass().getDeclaredMethods();
- HashMap<String,Sprite> sprites = new HashMap<String,Sprite>();
- for(Method m : methods) {
- if(m.isAnnotationPresent(Image.class)) {
- try {
- sprites.put(m.getName(),(Sprite) m.invoke(images, null));
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- return sprites;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement