Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I want to extend dynamic class "Object" from any other class, the problem is I want the "other class" be dynamic also,
- // 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
- // initialized yet but I need super to initializing so we got a little bad cycle here.
- // Any idea how fix this? I bet you got ;)
- // / Sprite.java
- ////////////////////////////////////////////
- package Agine.Objects;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.lang.*;
- import java.net.URL;
- import Agine.Transform.Vector2;
- /**
- * Created by Aresak on 5/25/2015.
- */
- public class Sprite extends Object {
- BufferedImage spriteImage;
- String fileName;
- int width, height;
- public boolean isSet = false;
- public Sprite(int width, int height) {
- if(width < 0) throw new IllegalArgumentException("Width (" + width + ") is illegal. Width has positive.");
- if(height < 0) throw new IllegalArgumentException("Height (" + height + ") is illegal. Height has to be positive");
- this.width = width;
- this.height = height;
- spriteImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- fileName = width + "-by-" + height;
- isSet = true;
- super(this);
- }
- public Sprite(Sprite sprite) {
- width = sprite.width();
- height = sprite.height();
- spriteImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- fileName = sprite.getFileName();
- for(int col = 0; col < width(); col++) {
- for(int row = 0; row < height(); row++) {
- spriteImage.setRGB(col, row, sprite.get(col, row).getRGB());
- }
- }
- isSet = true;
- }
- public Sprite(String fileName) {
- this.fileName = fileName;
- try {
- File file = new File(fileName);
- if(file.isFile()) {
- spriteImage = ImageIO.read(file);
- }
- else {
- URL url = getClass().getResource(fileName);
- if(url == null) url = new URL(fileName);
- spriteImage = ImageIO.read(url);
- }
- width = spriteImage.getWidth(null);
- width = spriteImage.getHeight(null);
- }
- catch(IOException ioException) {
- throw new RuntimeException("Could not open file: " + fileName);
- }
- catch(Exception exception) {
- exception.printStackTrace();
- }
- isSet = true;
- }
- public Sprite(File file) {
- try {
- spriteImage = ImageIO.read(file);
- }
- catch(IOException ioException) {
- ioException.printStackTrace();
- throw new RuntimeException("Could not open file: " + file);
- }
- if(spriteImage == null) {
- throw new RuntimeException("Invalid image file: " + file);
- }
- width = spriteImage.getWidth();
- height = spriteImage.getHeight();
- fileName = file.getName();
- isSet = true;
- }
- public void setGraphics(File file) {
- try {
- spriteImage = ImageIO.read(file);
- }
- catch(IOException ioException) {
- ioException.printStackTrace();
- throw new RuntimeException("Could not open file: " + file);
- }
- if(spriteImage == null) {
- throw new RuntimeException("Invalid image file: " + file);
- }
- width = spriteImage.getWidth();
- height = spriteImage.getHeight();
- fileName = file.getName();
- isSet = true;
- }
- public void setGraphics(String fileName) {
- this.fileName = fileName;
- try {
- File file = new File(fileName);
- if(file.isFile()) {
- spriteImage = ImageIO.read(file);
- }
- else {
- URL url = getClass().getResource(fileName);
- if(url == null) url = new URL(fileName);
- spriteImage = ImageIO.read(url);
- }
- width = spriteImage.getWidth(null);
- width = spriteImage.getHeight(null);
- }
- catch(IOException ioException) {
- throw new RuntimeException("Could not open file: " + fileName);
- }
- catch(Exception exception) {
- exception.printStackTrace();
- }
- isSet = true;
- }
- public void setGraphics(Sprite sprite) {
- width = sprite.width();
- height = sprite.height();
- spriteImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- fileName = sprite.getFileName();
- for(int col = 0; col < width(); col++) {
- for(int row = 0; row < height(); row++) {
- spriteImage.setRGB(col, row, sprite.get(col, row).getRGB());
- }
- }
- isSet = true;
- }
- public void setGraphics(int width, int height) {
- if(width < 0) throw new IllegalArgumentException("Width (" + width + ") is illegal. Width has positive.");
- if(height < 0) throw new IllegalArgumentException("Height (" + height + ") is illegal. Height has to be positive");
- this.width = width;
- this.height = height;
- spriteImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
- fileName = width + "-by-" + height;
- isSet = true;
- }
- public Graphics getSprite() {
- return spriteImage.getGraphics();
- }
- public int width() {
- return this.width;
- }
- public int height() {
- return this.height;
- }
- public String getFileName() {
- return fileName;
- }
- public Color get(int col, int row) {
- if (col < 0 || col >= width()) throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
- if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
- else return new Color(spriteImage.getRGB(col, height - row - 1));
- }
- public void set(int col, int row, Color color) {
- if (col < 0 || col >= width()) throw new IndexOutOfBoundsException("col must be between 0 and " + (width()-1));
- if (row < 0 || row >= height()) throw new IndexOutOfBoundsException("row must be between 0 and " + (height()-1));
- if (color == null) throw new NullPointerException("can't set Color to null");
- else spriteImage.setRGB(col, height - row - 1, color.getRGB());
- }
- public boolean equals(java.lang.Object obj) {
- if (obj == this) return true;
- if (obj == null) return false;
- if (obj.getClass() != this.getClass()) return false;
- Sprite that = (Sprite) obj;
- if (this.width() != that.width()) return false;
- if (this.height() != that.height()) return false;
- for (int col = 0; col < width(); col++)
- for (int row = 0; row < height(); row++)
- if (!this.get(col, row).equals(that.get(col, row))) return false;
- return true;
- }
- public BufferedImage getSpriteImage() {
- return spriteImage;
- }
- public void save(String name) {
- save(new File(name));
- }
- public void save(File file) {
- this.fileName = file.getName();
- String suffix = fileName.substring(fileName.lastIndexOf('.') + 1);
- suffix = suffix.toLowerCase();
- if (suffix.equals("jpg") || suffix.equals("png")) {
- try {
- ImageIO.write(spriteImage, suffix, file);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- else {
- System.out.println("Error: filename must end in .jpg or .png");
- }
- }
- public void getGraphics(Graphics g, int x, int y) {
- g.drawImage(this.getSpriteImage(), x, y, null);
- }
- public void getGraphics(Graphics g, Vector2 vector) {
- g.drawImage(this.getSpriteImage(), (int) vector.getX(), (int) vector.getY(), null);
- }
- @Override
- public void tick() {
- }
- @Override
- public void render() {
- }
- @Override
- public void awake() {
- }
- @Override
- public void onGetBounds() {
- }
- }
- // / Object.java
- ////////////////////////////////////////////////
- package Agine.Objects;
- import Agine.Debug;
- import java.awt.*;
- import java.util.LinkedList;
- /**
- * Created by Aresak on 5/18/2015.
- */
- public abstract class Object {
- public static LinkedList<Object> objects = new LinkedList<Object>();
- public boolean isGameObject = false;
- public boolean isGUIText = false;
- public boolean isGUIButton = false;
- public boolean isGUISprite = false;
- public boolean isGUIAnimatedSprite = false;
- public boolean isAudioClip = false;
- public boolean ableToTick = false;
- public boolean ableToAwake = false;
- public boolean ableToRender = false;
- public boolean ableToGetBounds = false;
- GameObject gameObject;
- GUIText guiText;
- GUIButton guiButton;
- Sprite guiSprite;
- AnimatedSprite guiAnimatedSprite;
- public Object(GameObject object) {
- isGameObject = true;
- ableToAwake = true;
- ableToRender = true;
- ableToTick = true;
- ableToGetBounds = true;
- gameObject = object;
- Object.addObject(this);
- }
- public Object(GUIText object) {
- isGUIText = true;
- ableToRender = true;
- ableToTick = true;
- ableToGetBounds = true;
- guiText = object;
- Object.addObject(this);
- }
- public Object(GUIButton object) {
- isGUIButton = true;
- ableToAwake = true;
- ableToGetBounds = true;
- guiButton = object;
- Object.addObject(this);
- }
- public Object(Sprite object) {
- isGUISprite = true;
- ableToGetBounds = true;
- guiSprite = object;
- Object.addObject(this);
- }
- public Object(AnimatedSprite object) {
- isGUIAnimatedSprite = true;
- ableToGetBounds = true;
- guiAnimatedSprite = object;
- Object.addObject(this);
- }
- // Dynamic methods
- public GameObject getAsGameObject() {
- if(isGameObject) {
- return gameObject;
- }
- else {
- Debug.LogError("Object isn't GameObject");
- return null;
- }
- }
- public GUIText getAsGUIText() {
- if(isGUIText) {
- return guiText;
- }
- else {
- Debug.LogError("Object isn't GUIText");
- return null;
- }
- }
- public GUIButton getAsGUIButton() {
- if(isGUIButton) {
- return guiButton;
- }
- else {
- Debug.LogError("Object isn't GUIButton");
- return null;
- }
- }
- public Sprite getAsSprite() {
- if(isGUISprite) {
- return guiSprite;
- }
- else {
- Debug.LogError("Object isn't Sprite");
- return null;
- }
- }
- public AnimatedSprite getAsAnimatedSprite() {
- if(isGUIAnimatedSprite) {
- return guiAnimatedSprite;
- }
- else {
- Debug.LogError("Object isn't AnimatedSprite");
- return null;
- }
- }
- public String toString() {
- return "[Object]@" + ableToGetBounds + ableToAwake + ableToRender + ableToTick + "<" + hashCode();
- }
- // Static Methods
- public static void sysTick() {
- for(int i = 0; i < objects.size(); i ++) {
- Object tempObject = objects.get(i);
- if(tempObject.ableToTick) {
- tempObject.tick();
- }
- Debug.Log("Object tick: " + tempObject.toString());
- }
- }
- public static void sysRender() {
- for(int i = 0; i < objects.size(); i ++) {
- Object tempObject = objects.get(i);
- if(tempObject.ableToRender) {
- tempObject.render();
- }
- }
- }
- public static void sysAwake() {
- for(int i = 0; i < objects.size(); i ++) {
- Object tempObject = objects.get(i);
- if(tempObject.ableToAwake) {
- tempObject.awake();
- }
- }
- }
- public static void addObject(Object object) {
- Object.objects.add(object);
- object.awake();
- }
- public static void removeObject(Object object) {
- Object.objects.remove(object);
- }
- public static LinkedList<Object> getAllObjects() {
- return Object.objects;
- }
- public static LinkedList<Object> getGameObjects() {
- LinkedList<Object> ret = new LinkedList<Object>();
- for(int i = 0; i < objects.size(); i ++) {
- if(objects.get(i).isGameObject == true) {
- ret.add(objects.get(i));
- }
- }
- return ret;
- }
- public static LinkedList<Object> getGUITextObjects() {
- LinkedList<Object> ret = new LinkedList<Object>();
- for(int i = 0; i < objects.size(); i ++) {
- if(objects.get(i).isGUIText == true) {
- ret.add(objects.get(i));
- }
- }
- return ret;
- }
- public static LinkedList<Object> getGUIButtonObjects() {
- LinkedList<Object> ret = new LinkedList<Object>();
- for(int i = 0; i < objects.size(); i ++) {
- if(objects.get(i).isGUIButton == true) {
- ret.add(objects.get(i));
- }
- }
- return ret;
- }
- public static LinkedList<Object> getGUIRenderable() {
- LinkedList<Object> objects = new LinkedList<Object>();
- for(int i = 0; i < objects.size(); i ++) {
- Object tempObject = objects.get(i);
- if(tempObject.ableToRender && (tempObject.isGUIButton || tempObject.isGUIText)) {
- objects.add(tempObject);
- }
- }
- return objects;
- }
- public static void renderGameObjects(Graphics g) {
- LinkedList<Object> objects = getGameObjects();
- for(int i = 0; i < objects.size(); i ++) {
- if(objects.get(i).ableToRender) {
- objects.get(i).render();
- }
- }
- }
- public static void renderGUI(Graphics g) {
- LinkedList<Object> objects = getGUIRenderable();
- for(int i = 0; i < objects.size(); i ++) {
- objects.get(i).render();
- }
- }
- public abstract void tick();
- public abstract void render();
- public abstract void awake();
- public abstract void onGetBounds();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement