Advertisement
Guest User

super(this) in dynamic class

a guest
May 29th, 2015
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.78 KB | None | 0 0
  1. // I want to extend dynamic class "Object" from any other class, the problem is I want the "other class" be dynamic also,
  2. // As construction parameter in Object is (Sprite object) so I want to use "super(this)" but I can't use "this" because it is not
  3. // initialized yet but I need super to initializing so we got a little bad cycle here.
  4. // Any idea how fix this? I bet you got ;)
  5.  
  6.  
  7. // / Sprite.java
  8. ////////////////////////////////////////////
  9. package Agine.Objects;
  10.  
  11. import javax.imageio.ImageIO;
  12. import java.awt.*;
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.lang.*;
  17. import java.net.URL;
  18.  
  19. import Agine.Transform.Vector2;
  20.  
  21. /**
  22.  * Created by Aresak on 5/25/2015.
  23.  */
  24. public class Sprite extends Object {
  25.     BufferedImage spriteImage;
  26.     String fileName;
  27.  
  28.     int width, height;
  29.     public boolean isSet = false;
  30.  
  31.     public Sprite(int width, int height) {
  32.         if(width < 0) throw new IllegalArgumentException("Width (" + width + ") is illegal. Width has positive.");
  33.         if(height < 0) throw new IllegalArgumentException("Height (" + height + ") is illegal. Height has to be positive");
  34.  
  35.         this.width = width;
  36.         this.height = height;
  37.  
  38.         spriteImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  39.  
  40.         fileName = width + "-by-" + height;
  41.         isSet = true;
  42.  
  43.         super(this);
  44.     }
  45.  
  46.     public Sprite(Sprite sprite) {
  47.         width = sprite.width();
  48.         height = sprite.height();
  49.  
  50.         spriteImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  51.         fileName = sprite.getFileName();
  52.  
  53.         for(int col = 0; col < width(); col++) {
  54.             for(int row = 0; row < height(); row++) {
  55.                 spriteImage.setRGB(col, row, sprite.get(col, row).getRGB());
  56.             }
  57.         }
  58.         isSet = true;
  59.     }
  60.  
  61.     public Sprite(String fileName) {
  62.         this.fileName = fileName;
  63.  
  64.         try {
  65.             File file = new File(fileName);
  66.             if(file.isFile()) {
  67.                 spriteImage = ImageIO.read(file);
  68.             }
  69.  
  70.             else {
  71.                 URL url = getClass().getResource(fileName);
  72.                 if(url == null) url = new URL(fileName);
  73.  
  74.                 spriteImage = ImageIO.read(url);
  75.             }
  76.  
  77.             width = spriteImage.getWidth(null);
  78.             width = spriteImage.getHeight(null);
  79.         }
  80.         catch(IOException ioException) {
  81.             throw new RuntimeException("Could not open file: " + fileName);
  82.         }
  83.         catch(Exception exception) {
  84.             exception.printStackTrace();
  85.         }
  86.         isSet = true;
  87.     }
  88.  
  89.     public Sprite(File file) {
  90.         try {
  91.             spriteImage = ImageIO.read(file);
  92.         }
  93.         catch(IOException ioException) {
  94.             ioException.printStackTrace();
  95.             throw new RuntimeException("Could not open file: " + file);
  96.         }
  97.  
  98.         if(spriteImage == null) {
  99.             throw new RuntimeException("Invalid image file: " + file);
  100.         }
  101.  
  102.         width = spriteImage.getWidth();
  103.         height = spriteImage.getHeight();
  104.         fileName = file.getName();
  105.         isSet = true;
  106.     }
  107.  
  108.     public void setGraphics(File file) {
  109.         try {
  110.             spriteImage = ImageIO.read(file);
  111.         }
  112.         catch(IOException ioException) {
  113.             ioException.printStackTrace();
  114.             throw new RuntimeException("Could not open file: " + file);
  115.         }
  116.  
  117.         if(spriteImage == null) {
  118.             throw new RuntimeException("Invalid image file: " + file);
  119.         }
  120.  
  121.         width = spriteImage.getWidth();
  122.         height = spriteImage.getHeight();
  123.         fileName = file.getName();
  124.         isSet = true;
  125.     }
  126.  
  127.     public void setGraphics(String fileName) {
  128.         this.fileName = fileName;
  129.  
  130.         try {
  131.             File file = new File(fileName);
  132.             if(file.isFile()) {
  133.                 spriteImage = ImageIO.read(file);
  134.             }
  135.  
  136.             else {
  137.                 URL url = getClass().getResource(fileName);
  138.                 if(url == null) url = new URL(fileName);
  139.  
  140.                 spriteImage = ImageIO.read(url);
  141.             }
  142.  
  143.             width = spriteImage.getWidth(null);
  144.             width = spriteImage.getHeight(null);
  145.         }
  146.         catch(IOException ioException) {
  147.             throw new RuntimeException("Could not open file: " + fileName);
  148.         }
  149.         catch(Exception exception) {
  150.             exception.printStackTrace();
  151.         }
  152.         isSet = true;
  153.     }
  154.  
  155.     public void setGraphics(Sprite sprite) {
  156.         width = sprite.width();
  157.         height = sprite.height();
  158.  
  159.         spriteImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  160.         fileName = sprite.getFileName();
  161.  
  162.         for(int col = 0; col < width(); col++) {
  163.             for(int row = 0; row < height(); row++) {
  164.                 spriteImage.setRGB(col, row, sprite.get(col, row).getRGB());
  165.             }
  166.         }
  167.         isSet = true;
  168.     }
  169.  
  170.     public void setGraphics(int width, int height) {
  171.         if(width < 0) throw new IllegalArgumentException("Width (" + width + ") is illegal. Width has positive.");
  172.         if(height < 0) throw new IllegalArgumentException("Height (" + height + ") is illegal. Height has to be positive");
  173.  
  174.         this.width = width;
  175.         this.height = height;
  176.  
  177.         spriteImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  178.  
  179.         fileName = width + "-by-" + height;
  180.         isSet = true;
  181.     }
  182.  
  183.     public Graphics getSprite() {
  184.         return spriteImage.getGraphics();
  185.     }
  186.  
  187.  
  188.     public int width() {
  189.         return this.width;
  190.     }
  191.  
  192.     public int height() {
  193.         return this.height;
  194.     }
  195.  
  196.     public String getFileName() {
  197.         return fileName;
  198.     }
  199.  
  200.     public Color get(int col, int row) {
  201.         if (col < 0 || col >= width())  throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
  202.         if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
  203.         else return new Color(spriteImage.getRGB(col, height - row - 1));
  204.     }
  205.  
  206.     public void set(int col, int row, Color color) {
  207.         if (col < 0 || col >= width())  throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
  208.         if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
  209.         if (color == null) throw new NullPointerException("can't set Color to null");
  210.         else spriteImage.setRGB(col, height - row - 1, color.getRGB());
  211.     }
  212.  
  213.     public boolean equals(java.lang.Object obj) {
  214.         if (obj == this) return true;
  215.         if (obj == null) return false;
  216.         if (obj.getClass() != this.getClass()) return false;
  217.         Sprite that = (Sprite) obj;
  218.         if (this.width()  != that.width())  return false;
  219.         if (this.height() != that.height()) return false;
  220.         for (int col = 0; col < width(); col++)
  221.             for (int row = 0; row < height(); row++)
  222.                 if (!this.get(col, row).equals(that.get(col, row))) return false;
  223.         return true;
  224.     }
  225.  
  226.     public BufferedImage getSpriteImage() {
  227.         return spriteImage;
  228.     }
  229.  
  230.     public void save(String name) {
  231.         save(new File(name));
  232.     }
  233.  
  234.     public void save(File file) {
  235.         this.fileName = file.getName();
  236.         String suffix = fileName.substring(fileName.lastIndexOf('.') + 1);
  237.         suffix = suffix.toLowerCase();
  238.         if (suffix.equals("jpg") || suffix.equals("png")) {
  239.             try {
  240.                 ImageIO.write(spriteImage, suffix, file);
  241.             }
  242.             catch (IOException e) {
  243.                 e.printStackTrace();
  244.             }
  245.         }
  246.         else {
  247.             System.out.println("Error: filename must end in .jpg or .png");
  248.         }
  249.     }
  250.  
  251.     public void getGraphics(Graphics g, int x, int y) {
  252.         g.drawImage(this.getSpriteImage(), x, y, null);
  253.     }
  254.  
  255.     public void getGraphics(Graphics g, Vector2 vector) {
  256.         g.drawImage(this.getSpriteImage(), (int) vector.getX(), (int) vector.getY(), null);
  257.     }
  258.  
  259.     @Override
  260.     public void tick() {
  261.  
  262.     }
  263.  
  264.     @Override
  265.     public void render() {
  266.  
  267.     }
  268.  
  269.     @Override
  270.     public void awake() {
  271.  
  272.     }
  273.  
  274.     @Override
  275.     public void onGetBounds() {
  276.  
  277.     }
  278.  
  279. }
  280.  
  281.  
  282.  
  283.  
  284.  
  285. // / Object.java
  286. ////////////////////////////////////////////////
  287.  
  288.  
  289.  
  290.  
  291. package Agine.Objects;
  292.  
  293. import Agine.Debug;
  294.  
  295. import java.awt.*;
  296. import java.util.LinkedList;
  297.  
  298. /**
  299.  * Created by Aresak on 5/18/2015.
  300.  */
  301. public abstract class Object {
  302.     public static LinkedList<Object> objects = new LinkedList<Object>();
  303.  
  304.     public boolean isGameObject = false;
  305.     public boolean isGUIText = false;
  306.     public boolean isGUIButton = false;
  307.     public boolean isGUISprite = false;
  308.     public boolean isGUIAnimatedSprite = false;
  309.     public boolean isAudioClip = false;
  310.  
  311.     public boolean ableToTick = false;
  312.     public boolean ableToAwake = false;
  313.     public boolean ableToRender = false;
  314.     public boolean ableToGetBounds = false;
  315.  
  316.     GameObject gameObject;
  317.     GUIText guiText;
  318.     GUIButton guiButton;
  319.     Sprite guiSprite;
  320.     AnimatedSprite guiAnimatedSprite;
  321.  
  322.     public Object(GameObject object) {
  323.         isGameObject = true;
  324.  
  325.         ableToAwake = true;
  326.         ableToRender = true;
  327.         ableToTick = true;
  328.         ableToGetBounds = true;
  329.  
  330.         gameObject = object;
  331.  
  332.         Object.addObject(this);
  333.     }
  334.  
  335.     public Object(GUIText object) {
  336.         isGUIText = true;
  337.  
  338.         ableToRender = true;
  339.         ableToTick = true;
  340.         ableToGetBounds = true;
  341.  
  342.         guiText = object;
  343.  
  344.         Object.addObject(this);
  345.     }
  346.  
  347.     public Object(GUIButton object) {
  348.         isGUIButton = true;
  349.  
  350.         ableToAwake = true;
  351.         ableToGetBounds = true;
  352.  
  353.         guiButton = object;
  354.  
  355.         Object.addObject(this);
  356.     }
  357.  
  358.     public Object(Sprite object) {
  359.         isGUISprite = true;
  360.  
  361.         ableToGetBounds = true;
  362.  
  363.         guiSprite = object;
  364.  
  365.         Object.addObject(this);
  366.     }
  367.  
  368.     public Object(AnimatedSprite object) {
  369.         isGUIAnimatedSprite = true;
  370.  
  371.         ableToGetBounds = true;
  372.  
  373.         guiAnimatedSprite = object;
  374.  
  375.         Object.addObject(this);
  376.  
  377.     }
  378.  
  379.  
  380.     // Dynamic methods
  381.     public GameObject getAsGameObject() {
  382.         if(isGameObject) {
  383.             return gameObject;
  384.         }
  385.         else {
  386.             Debug.LogError("Object isn't GameObject");
  387.             return null;
  388.         }
  389.     }
  390.  
  391.     public GUIText getAsGUIText() {
  392.         if(isGUIText) {
  393.             return guiText;
  394.         }
  395.         else {
  396.             Debug.LogError("Object isn't GUIText");
  397.             return null;
  398.         }
  399.     }
  400.  
  401.     public GUIButton getAsGUIButton() {
  402.         if(isGUIButton) {
  403.             return guiButton;
  404.         }
  405.         else {
  406.             Debug.LogError("Object isn't GUIButton");
  407.             return null;
  408.         }
  409.     }
  410.  
  411.     public Sprite getAsSprite() {
  412.         if(isGUISprite) {
  413.             return guiSprite;
  414.         }
  415.         else {
  416.             Debug.LogError("Object isn't Sprite");
  417.             return null;
  418.         }
  419.     }
  420.  
  421.     public AnimatedSprite getAsAnimatedSprite() {
  422.         if(isGUIAnimatedSprite) {
  423.             return guiAnimatedSprite;
  424.         }
  425.         else {
  426.             Debug.LogError("Object isn't AnimatedSprite");
  427.             return null;
  428.         }
  429.     }
  430.  
  431.     public String toString() {
  432.         return "[Object]@" + ableToGetBounds + ableToAwake + ableToRender + ableToTick + "<" + hashCode();
  433.     }
  434.  
  435.  
  436.  
  437.     // Static Methods
  438.  
  439.     public static void sysTick() {
  440.         for(int i = 0; i < objects.size(); i ++) {
  441.             Object tempObject = objects.get(i);
  442.  
  443.             if(tempObject.ableToTick) {
  444.                 tempObject.tick();
  445.             }
  446.  
  447.             Debug.Log("Object tick: " + tempObject.toString());
  448.         }
  449.     }
  450.  
  451.     public static void sysRender() {
  452.         for(int i = 0; i < objects.size(); i ++) {
  453.             Object tempObject = objects.get(i);
  454.  
  455.             if(tempObject.ableToRender) {
  456.                 tempObject.render();
  457.             }
  458.         }
  459.     }
  460.  
  461.     public static void sysAwake() {
  462.         for(int i = 0; i < objects.size(); i ++) {
  463.             Object tempObject = objects.get(i);
  464.  
  465.             if(tempObject.ableToAwake) {
  466.                 tempObject.awake();
  467.             }
  468.         }
  469.     }
  470.  
  471.     public static void addObject(Object object) {
  472.         Object.objects.add(object);
  473.         object.awake();
  474.     }
  475.  
  476.  
  477.     public static void removeObject(Object object) {
  478.         Object.objects.remove(object);
  479.     }
  480.  
  481.     public static LinkedList<Object> getAllObjects() {
  482.         return Object.objects;
  483.     }
  484.  
  485.     public static LinkedList<Object> getGameObjects() {
  486.         LinkedList<Object> ret = new LinkedList<Object>();
  487.  
  488.         for(int i = 0; i < objects.size(); i ++) {
  489.             if(objects.get(i).isGameObject == true) {
  490.                 ret.add(objects.get(i));
  491.             }
  492.         }
  493.  
  494.         return ret;
  495.     }
  496.  
  497.     public static LinkedList<Object> getGUITextObjects() {
  498.         LinkedList<Object> ret = new LinkedList<Object>();
  499.  
  500.         for(int i = 0; i < objects.size(); i ++) {
  501.             if(objects.get(i).isGUIText == true) {
  502.                 ret.add(objects.get(i));
  503.             }
  504.         }
  505.  
  506.         return ret;
  507.     }
  508.  
  509.     public static LinkedList<Object> getGUIButtonObjects() {
  510.         LinkedList<Object> ret = new LinkedList<Object>();
  511.  
  512.         for(int i = 0; i < objects.size(); i ++) {
  513.             if(objects.get(i).isGUIButton == true) {
  514.                 ret.add(objects.get(i));
  515.             }
  516.         }
  517.  
  518.         return ret;
  519.     }
  520.  
  521.     public static LinkedList<Object> getGUIRenderable() {
  522.         LinkedList<Object> objects = new LinkedList<Object>();
  523.  
  524.         for(int i = 0; i < objects.size(); i ++) {
  525.             Object tempObject = objects.get(i);
  526.             if(tempObject.ableToRender && (tempObject.isGUIButton || tempObject.isGUIText)) {
  527.                 objects.add(tempObject);
  528.             }
  529.         }
  530.  
  531.         return objects;
  532.     }
  533.  
  534.     public static void renderGameObjects(Graphics g) {
  535.         LinkedList<Object> objects = getGameObjects();
  536.  
  537.         for(int i = 0; i < objects.size(); i ++) {
  538.             if(objects.get(i).ableToRender) {
  539.                 objects.get(i).render();
  540.             }
  541.         }
  542.     }
  543.  
  544.     public static void renderGUI(Graphics g) {
  545.         LinkedList<Object> objects = getGUIRenderable();
  546.  
  547.         for(int i = 0; i < objects.size(); i ++) {
  548.             objects.get(i).render();
  549.         }
  550.     }
  551.  
  552.  
  553.     public abstract void tick();
  554.     public abstract void render();
  555.     public abstract void awake();
  556.     public abstract void onGetBounds();
  557. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement