Hi, I was trying to write a program, that converts the sprites from a sprite sheet into an actual animation. The problem I stumbled upon, is that I now need to convert a buffered image into a greenfoot image, but my mental capacity clearly isn't as big enough co comprehend such an action, so help would be much appreciated :)
the error itself is in the animationShower() method, but i'll post the whole class, just for clarity
the code:
import greenfoot.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class SpriteSheetExtractor extends Actor {
MyWorld w;
private int numRows;
private int numCols = 3; // number of columns
private int spriteWidth = 32; // width of each sprite in pixels
private int spriteHeight = 32; // Height of each sprite in pixels
private boolean added = false;
private int frame = 0; // Declare frame as an instance variable
BufferedImage[][] sprites; // Declare the sprites array without initialization
AnimationModels model[] = new AnimationModels[numRows]; // Declare the models array without initialization
public SpriteSheetExtractor(MyWorld w1) {
w = w1;
numRows = 4; // Set the correct number of rows
sprites = new BufferedImage[numRows][numCols]; // Initialize the sprites array with the correct size
main(); // Call main after initializing the sprites array
animationShower(); // Then call animationShower
}
public void act() {
animationShower();
}
public void main() {
// Load the sprite image
BufferedImage spriteSheet = null;
try {
spriteSheet = ImageIO.read(new File("C:/Users/sebwr/Documents/Greenfoot/tests/CharacterDesignTest-Ediding-01/images/spriteSheets/Male16-4.png"));
} catch (IOException e) {
e.printStackTrace();
return;
}
// create a 2D array to store individual sprite images
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
// calculate the position of the current sprite
int x = col * spriteHeight;
int y = row * spriteWidth;
// extract the current image as a subimage
BufferedImage spriteImage = spriteSheet.getSubimage(x, y, spriteWidth, spriteHeight);
// store the image in the sprites array
sprites[row][col] = spriteImage;
}
}
}
public void animationShower() {
if (!added) {
int yPos = 0;
MouseInfo mouse = Greenfoot.getMouseInfo();
added = true;
for (int i = 0; i < numRows; i++) {
// Create a new AnimationModels object for each row
model[i] = new AnimationModels(w);
w.addObject(model[i], w.getWidth() / 2, yPos + w.getHeight() / (numRows + 9));
yPos += w.getHeight() / numRows + 1;
yPos += model[i].getImage().getHeight(); // Adjust yPos based on the height of the added object
System.out.println("Animation " + (i + 1) + " wurde hinzugefügt " + added);
}
} else {
frame++;
if (frame == 20) frame = 0;
if (frame == 10) {
for (int i = 0; i < numRows; i++) {
GreenfootImage greenfootImage = new GreenfootImage(sprites[i][0]); // Convert BufferedImage to GreenfootImage
model[i + 1].setImage(greenfootImage);
}
}else if (frame == 20) {
for (int i = 0; i < numRows; i++) {
GreenfootImage greenfootImage = new GreenfootImage(sprites[i][2]); // Convert BufferedImage to GreenfootImage
model[i + 1].setImage(greenfootImage);
}
}
}
}
}