66import org .w3c .dom .css .Rect ;
77
88import javax .imageio .ImageIO ;
9+ import javax .xml .bind .helpers .DefaultValidationEventHandler ;
910import java .awt .*;
1011import java .awt .image .BufferedImage ;
12+ import java .io .File ;
1113import java .io .IOException ;
1214import java .io .InputStream ;
15+ import java .net .URL ;
16+ import java .util .ArrayList ;
1317import java .util .LinkedList ;
1418
1519public class Sprite extends Component {
1620
17- LinkedList < LinkedList <Rectangle >> animations1 = new LinkedList <>();
18- LinkedList < LinkedList < BufferedImage >> animations = new LinkedList <>();
21+ ArrayList < ArrayList <Rectangle >> animations1 = new ArrayList <>();
22+ ArrayList < BufferedImage []> animations = new ArrayList <>();
1923 BufferedImage spriteSheet ;
2024 private int animationIndex = 0 ;
2125 private int spriteCounter ;
2226 float timer = 10 ;
2327 int currentSprite = 0 ;
2428
25-
2629 public float getTimer () {
2730 return timer ;
2831 }
29-
3032 public void setTimer (float timer ) {
3133 this .timer = timer ;
3234 }
@@ -37,7 +39,6 @@ public void setTimer(float timer) {
3739 */
3840 public void loadAnimation (Rectangle [] tiles ,String spriteSheetPath ){
3941 BufferedImage sprite = null ;
40-
4142 try {
4243 InputStream in = this .getClass ().getResourceAsStream (spriteSheetPath );
4344 sprite = ImageIO .read (in );
@@ -48,42 +49,66 @@ public void loadAnimation(Rectangle[] tiles,String spriteSheetPath){
4849 if (spriteSheet == null ) {
4950 spriteSheet = sprite ;
5051 }
51- LinkedList <BufferedImage > animation = new LinkedList <>();
52+ BufferedImage [] animation = new BufferedImage [tiles .length ];
53+ int i = 0 ;
5254 for (Rectangle r : tiles ){
5355 BufferedImage animationSprite = (spriteSheet .getSubimage ((int ) (r .getX ()), (int ) (r .getY ()), (int ) r .getWidth (), (int ) r .getHeight ()));
54- animation .add (animationSprite );
56+ animation [i ]=(animationSprite );
57+ i ++;
5558 }
5659 animations .add (animation );
57-
5860 }
5961 /**
6062 * This function loads in sprites from images
6163 * @param paths array of paths to images to be loaded
6264 */
6365 public void loadAnimation (String [] paths ){
64-
66+ BufferedImage [] images = new BufferedImage [paths .length ];
67+ for (int i = 0 ;i < paths .length ;i ++){
68+ String path = paths [i ];
69+ //read image of file
70+ BufferedImage sprite = null ;
71+ try {
72+ InputStream in = this .getClass ().getResourceAsStream (path );
73+ sprite = ImageIO .read (in );
74+ } catch (IOException e ) {
75+ e .printStackTrace ();
76+ }
77+ //add image to temp animation
78+ images [i ] = sprite ;
79+ }
80+ animations .add (images );
6581 }
6682 /**
6783 * This function loads in all images in side a folder
6884 * @param folder path to folder where to load in all sprites
69- */
85+ * * /
7086 public void loadAnimation (String folder ){
71-
87+ /*
88+ how with jar?
89+ */
7290 }
7391
7492 public BufferedImage getAnimation (){
75- int spritesLen = animations .get (animationIndex ).size () ;
93+ int spritesLen = animations .get (animationIndex ).length ;
7694 //Adds until timer then 0
7795 spriteCounter = (spriteCounter +1 >=timer ) ? 0 :spriteCounter +1 ;
78- //Sets the current sprite index
96+ //Sets the current sprite index (the image that will be displayed)
7997 currentSprite = (((currentSprite + 1 ) >= spritesLen ) && (spriteCounter >= (timer - 1 ))) ? 0 :
8098 ((spriteCounter >= (timer - 1 )) ? (currentSprite + 1 ) :
8199 currentSprite );
82100
83- BufferedImage sprite = animations .get (animationIndex ). get (currentSprite );
101+ BufferedImage sprite = animations .get (animationIndex )[ (currentSprite )] ;
84102 float angle = getRotation ().getAngle ();
103+ //Sprite.resize(rotate(angle,sprite),getScale())
104+ return (sprite );
105+ }
106+
107+ @ Override
108+ public void draw (Graphics g ) {
109+ super .draw (g );
110+ g .drawImage (getAnimation (),(int )getPosition ().getX (),(int )getPosition ().getY (),(int )getScale ().getX (),(int )getScale ().getY (),null );
85111
86- return (Sprite .resize (rotate (angle ,sprite ),getScale ()));
87112 }
88113
89114 public static BufferedImage resize (BufferedImage img , Vector2 scale ) {
@@ -96,13 +121,10 @@ public static BufferedImage resize(BufferedImage img, Vector2 scale) {
96121
97122 return dimg ;
98123 }
99- public BufferedImage rotate (double angle ,BufferedImage image )
100- {
101-
124+ public BufferedImage rotate (double angle ,BufferedImage image ) {
102125 // Getting Dimensions of image
103126 int width = image .getWidth ();
104127 int height = image .getHeight ();
105-
106128 // Creating a new buffered image
107129 BufferedImage newImage = new BufferedImage (
108130 image .getWidth (), image .getWidth (), image .getType ());
0 commit comments