My error is still there, and i still have no idea where it is coming from... Just to troll with me, the test code in ImageBufferTest.java is working perfectly, even when i try messing around with setRGBA inside the key listener method...
Im at the point where im positive there is a problem with my code but i cannot possibly find it =/ Here is the full class im using (notice a lot of the setRGBA parts of the code in the AddImage() function is commented out and still the problem persists with the img.getImage() call)
Code:
import StickGame.LevelEditor.LevelData.LevelDataManager;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Color;
import org.lwjgl.util.Rectangle;
import org.newdawn.slick.*;
import org.newdawn.slick.imageout.ImageIOWriter;
import org.newdawn.slick.opengl.ImageDataFactory;
import org.newdawn.slick.opengl.LoadableImageData;
import org.newdawn.slick.opengl.TextureLoader;
/**
*
* @author Devin LR
*/
public class BGImageManager{
private ImageBuffer img = null;
private ImageBuffer oldImg = null;
int numImagesWide, numImagesHigh;
static final int imgBlockSize = 1000;
boolean needToLoadLvl = false;
boolean lvlLoaded = false;
static int iLocX, iLocY;
Image drawImg;
Image greyOverlay;
static Rectangle overlayRect = null;
private Image[][] lvlImages;
static int centerBlockX, centerBlockY;
int drawnCenterBlockX, drawnCenterBlockY;
boolean setCenterBlock = true;
BGImageManager(int width, int height, boolean loadLevel){
numImagesWide = (width+(imgBlockSize/2))/imgBlockSize;
numImagesHigh = (height+(imgBlockSize/2))/imgBlockSize;
needToLoadLvl = loadLevel;
lvlImages = new Image[numImagesWide][numImagesHigh];
iLocX = 0;
iLocY = 0;
if(loadLevel)
LoadImagesInit();
else
BlankImageInit();
}
private void BlankImageInit(){
img = new ImageBuffer(imgBlockSize, imgBlockSize);
Arrays.fill(img.getRGBA(), (byte)255);
ImageIOWriter writer = new ImageIOWriter();
int i = 0;
FileOutputStream fos;
drawImg = img.getImage();
for(int w=0; w<numImagesWide; w++){
for(int h=0; h<numImagesHigh; h++){
try{
fos = new FileOutputStream(EditorStarter.PATH + "/" + LevelDataManager.levelName + "/" + LevelDataManager.levelName + i + ".png");
System.out.println(i);
writer.saveImage(drawImg, "PNG", fos, true);
fos.flush();
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
lvlImages[w][h] = drawImg;
i++;
}
}
ImageBuffer tempImgData = new ImageBuffer(imgBlockSize, imgBlockSize);
Arrays.fill(tempImgData.getRGBA(), (byte)123);
greyOverlay = tempImgData.getImage();
}
void LoadImagesInit(){
if(img == null){
img = new ImageBuffer(imgBlockSize, imgBlockSize);
}
centerBlockX = 0; centerBlockY = 0;
drawnCenterBlockX = 0; drawnCenterBlockY = 0;
String tempPath = EditorStarter.PATH
+ "/" + LevelDataManager.levelName + "/" + LevelDataManager.levelName + 0 + ".png";
System.out.println(tempPath);
LoadableImageData tempImg = ImageDataFactory.getImageDataFor(tempPath);
try{
FileInputStream fin = new FileInputStream(tempPath);
ByteBuffer tempBuffer = tempImg.loadImage(fin);
int i=0;
for(int x=0; x<imgBlockSize; x++){
for(int y=0; y<imgBlockSize; y++){
img.setRGBA(x, y, tempBuffer.getInt(i), tempBuffer.getInt(i+1),
tempBuffer.getInt(i+2), tempBuffer.getInt(i+3));
i+=4;
}
}
int j=0;
for(int w=0; w<numImagesWide; w++){
for(int h=0; h<numImagesHigh; h++){
tempPath = EditorStarter.PATH
+ "/" + LevelDataManager.levelName + "/" + LevelDataManager.levelName + j + ".png";
fin = new FileInputStream(tempPath);
lvlImages[w][h] = new Image(TextureLoader.getTexture("PNG", fin));
j++;
}
}
ImageBuffer tempImgData = new ImageBuffer(imgBlockSize, imgBlockSize);
Arrays.fill(tempImgData.getRGBA(), (byte)200);
greyOverlay = tempImgData.getImage();
}catch(Exception e){
e.printStackTrace();
}
}
void Draw(GameContainer gc, Graphics g){
int tempx, tempy;
for(int w=0; w<numImagesWide; w++){
for(int h=0; h<numImagesHigh; h++){
tempx = iLocX+w*1000;
tempy = iLocY+h*1000;
g.drawImage(lvlImages[w][h], tempx, tempy);//lvlImages[w][h].draw(tempx, tempy);
if(setCenterBlock){
overlayRect = new Rectangle(tempx, tempy, imgBlockSize, imgBlockSize);
if(overlayRect.contains(gc.getWidth()/2, gc.getHeight()/2)){
centerBlockX = w; centerBlockY = h;
setCenterBlock = false;
}
}
}
}
setCenterBlock = true;
g.drawImage(greyOverlay, iLocX+centerBlockX*1000, iLocY+centerBlockY*1000);//greyOverlay.draw(iLocX+centerBlockX*1000, iLocY+centerBlockY*1000);
}
void AddImage(Image imgToAdd, int imgx, int imgy){
if(overlayRect.contains(imgx, imgy)){
oldImg = img;
int imgAddX = imgx - iLocX%imgBlockSize;
int imgAddY = imgy - iLocY%imgBlockSize;
int tempHeight = imgToAdd.getHeight();
int tempWidth = imgToAdd.getWidth();
LevelDataManager.AddBrick(imgx, imgy);
/*ByteBuffer bb = img.getImageBufferData();
try {
Graphics g = img.getImage().getGraphics();
g.drawImage(imgToAdd, imgAddX, imgAddY);
} catch (SlickException ex) {
Logger.getLogger(BGImageManager.class.getName()).log(Level.SEVERE, null, ex);
}
/*ImageBuffer tempImgData = img;
int i=0;
for(int x=0; x<imgBlockSize; x++){
for(int y=0; y<imgBlockSize; y++){
tempImgData.setRGBA(x, y, bb.get(i), bb.get(i+1),
bb.get(i+2), bb.get(i+3));
}
}
img = tempImgData;//*/
//Color tempC = Color.blue;
//Arrays.fill(img.getRGBA(), (byte)255);
/*for(int h=0; h<tempHeight; h++){
for(int w=0; w<tempWidth; w++){
//tempC = imgToAdd.getColor(w, h);
img.setRGBA((imgAddX+w)<imgBlockSize ? (imgAddX+w):imgBlockSize,
(imgAddY+h)<imgBlockSize ? (imgAddY+h):imgBlockSize, tempC.getRed(),
tempC.getGreen(), tempC.getBlue(), tempC.getAlpha());
}
}//*/
lvlImages[centerBlockX][centerBlockY] = img.getImage();
/*try{
FileOutputStream fos = new FileOutputStream(EditorStarter.PATH + "/" + LevelDataManager.levelName
+ "/" + LevelDataManager.levelName + centerBlockX*centerBlockY + ".png");
ImageIOWriter writer = new ImageIOWriter();
writer.saveImage(lvlImages[centerBlockX][centerBlockY], "PNG", fos, true);
fos.flush();
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}//*/
}else
System.out.println("Cannot add an image onto the non-center background block...");
}
void Update(int locx, int locy){
iLocX = locx;
iLocY = locy;
if(drawnCenterBlockX != centerBlockX || drawnCenterBlockY != centerBlockY){
drawnCenterBlockX = centerBlockX;
drawnCenterBlockY = centerBlockY;
String tempPath = EditorStarter.PATH
+ "/" + LevelDataManager.levelName + "/" + LevelDataManager.levelName + drawnCenterBlockX*drawnCenterBlockY + ".png";
LoadableImageData tempImg = ImageDataFactory.getImageDataFor(tempPath);
try{
FileInputStream fin = new FileInputStream(tempPath);
ByteBuffer tempBuffer = tempImg.loadImage(fin);
int i=0;
for(int x=0; x<imgBlockSize; x++){
for(int y=0; y<imgBlockSize; y++){
img.setRGBA(x, y, tempBuffer.getInt(i), tempBuffer.getInt(i+1),
tempBuffer.getInt(i+2), tempBuffer.getInt(i+3));
i+=4;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
Also, as a comparison to working code that is doing similar things, here is the ImageBufferTest.java file that i messed with trying to reproduce my problem...
Code:
import java.util.Arrays;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.ImageBuffer;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
/**
* A test for image buffer maniupulation rendering
*
* @author kevin
*/
public class ImageBufferTest extends BasicGame {
/** The image we're currently displaying */
private Image image;
ImageBuffer buffer;
int colorAdd = 10;
/**
* Create a new image buffer rendering test
*/
public ImageBufferTest() {
super("Image Buffer Test");
}
/**
* @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
*/
public void init(GameContainer container) throws SlickException {
buffer = new ImageBuffer(1000,1000);
Arrays.fill(buffer.getRGBA(), (byte)255);
image = buffer.getImage();
}
/**
* @see org.newdawn.slick.BasicGame#render(org.newdawn.slick.GameContainer, org.newdawn.slick.Graphics)
*/
public void render(GameContainer container, Graphics g) {
image.draw(50,50);
}
/**
* @see org.newdawn.slick.BasicGame#update(org.newdawn.slick.GameContainer, int)
*/
public void update(GameContainer container, int delta) {
}
/**
* @see org.newdawn.slick.BasicGame#keyPressed(int, char)
*/
public void keyPressed(int key, char c) {
if (key == Input.KEY_ESCAPE) {
System.out.println("escape pressed");
System.exit(0);
}else if(true){
System.out.println("space pressed");
for (int x=100;x<800;x++) {
for (int y=50;y<350;y++) {
buffer.setRGBA(x, y, x+colorAdd,y+colorAdd,0+colorAdd,255);
}
}
image = buffer.getImage();
colorAdd += 10;
}
}
/**
* Entry point to our test
*
* @param argv The arguments to pass into the test
*/
public static void main(String[] argv) {
try {
AppGameContainer container = new AppGameContainer(new ImageBufferTest());
container.setDisplayMode(800,600,false);
container.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}
Any possible suggestions you guys can give me would be greatly appreciated! But remember that i dont just need to be able to display the image nicely when i am finished adding the image, but i must also be able to save the image to a .png file (which means that with my current knowledge i cannot use the image.getGraphics to do any of this, but if you know of a way to save an image file by using the getGraphics method PLEASE let me know =])