Problem with Slick 2d Image / Graphics

Hey, trying to create a simple new Image, blank, then add the graphics altered from another image and replace it with the image just made.

Sounds simple but when I’ve been trying to create a new image or even access any Images graphics its not been working, throwing a null error every time, real confused at this been wrecking my brain for past hour or two.

EDIT:
this is shader class with the problem in it:



package org.background.worker;

import java.util.Iterator;

import javax.swing.SwingWorker;

import org.background.LightProcessor;
import org.model.World;
import org.model.emitters.Emitter;
import org.model.emitters.Light;
import org.model.player.Player;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
/**
 * Shader, for lighting.
 * @author Senior
 */
public class Shader extends SwingWorker<World, Void> {

	private LightProcessor processor = null;
	
	public LightProcessor getProcessor() {
		return processor;
	}

	public void setProcessor(LightProcessor processor) {
		this.processor = processor;
	}

	public Shader(LightProcessor processor) {
		this.setProcessor(processor);
	}

	@Override
	protected World doInBackground() throws Exception {
		if(processor.getWorld() == null) 
    		return null;
		World world = this.getProcessor().getWorld();
		Player player = world.getPlayer();
		if(player == null)
			return null;
		if(world.getLightEmitters().size() > 0) {
			for(Iterator<Emitter> it$ = world.getLightEmitters().descendingIterator(); it$.hasNext();) {
				Light l = (Light) it$.next();
				if(l == null)
					continue;
				if(!l.getPosition().isWithin(player.getPosition(), (int)l.getRange()))
					continue;
				if(player.getIcon() == null)
					continue;
				
				try {
					Image image = null;
					image = world.getBlank();
					if(image == null)
						continue;
					image = image.getScaledCopy(player.getIcon().getWidth(), player.getIcon().getHeight());
					if(image == null)
						continue;
					//Next line is problem line.
					Graphics g = image.getGraphics();
					if(g == null)
						continue;
					Image pixel = null;
					
					for(int h = 0; h < player.getIcon().getHeight(); h++) {
						for(int w = 0; w < player.getIcon().getWidth(); w++) {
							pixel = player.getIcon().getSubImage(w, h, 1, 1);
							g.drawImage(pixel, w, h, Color.red);
						}
					}
					
					player.setIcon(image);
				} catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		return world;
	}
	
}

In theory it should work but can’t find the graphics here is the error:


java.lang.NullPointerException
	at org.newdawn.slick.opengl.pbuffer.GraphicsFactory.init(GraphicsFactory.java:40)
	at org.newdawn.slick.opengl.pbuffer.GraphicsFactory.createGraphics(GraphicsFactory.java:120)
	at org.newdawn.slick.opengl.pbuffer.GraphicsFactory.getGraphicsForImage(GraphicsFactory.java:91)
	at org.newdawn.slick.Image.getGraphics(Image.java:397)
	at org.background.worker.Shader.doInBackground(Shader.java:62)
	at org.background.worker.Shader.doInBackground(Shader.java:1)
	at javax.swing.SwingWorker$1.call(Unknown Source)
	at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at javax.swing.SwingWorker.run(Unknown Source)
	at org.background.LightProcessor.run(LightProcessor.java:37)
	at java.util.TimerThread.mainLoop(Unknown Source)
	at java.util.TimerThread.run(Unknown Source)

And here is where the Image ‘blank’ is set:


public World(Main main) {
		this.setMain(main);
		//Set up workers.
		this.setProcessor(new Processor(this));
		this.setLightProcessor(new LightProcessor(this));
		
		try {
			this.setBlank(new Image("./data/blank.png", false, Image.FILTER_LINEAR));
....
		} catch(SlickException e) {
			e.printStackTrace();
		}
	}

The world is initialized in the init() method of Main extends BasicGame.

Any ideas?

Peace.

Is the image object null? If so it may not be finding the image. How are you loading the image?

Was just trying to create a blank Image eg new Image(256, 256) to draw graphics on and create a image.
The image is throwing a null at the moment but the graphics also does.
I don’t get what’s happening the principle is simple.
I’ve tried numerous different ways, ImageBuffer, loading a actual Image it all seems to be obsolete.

http://img252.imageshack.us/img252/5395/codeerror.png

Peace

I think the problem is in the constructor of Image() (in the backtrace you see org.newdawn.slick.Image.)

Are you sure player.getIcon().getWidth() and player.getIcon().getHeight() return the right size?

Pretty sure and I’ve tried different constructors of Image and even images that I know work.
When I try get Graphics from the players icon that would throw a null too.

Even the graphics I use to paint the images if I try setting that and using it there it still doesn’t work.
Rather confusing me.

This looks like an old LWJGL bug I’ve seen a while ago. Are you using the latest version?

I thought I was I’ll download new LWJGL
Thanks mate.

Well I updated my LWJGL from 2.8.3 - 2.8.4 and downloaded (newer?) version of Slick.
Still doesn’t work :confused:

http://img803.imageshack.us/img803/7501/errorreprt.png

Help?

Is your OpenGL context created? I think with LWJGL you have to call Display.create(), but it is some time since I last used it.

Other thing I could think of is enabling textures in OpenGL:


import org.lwjgl.opengl.GL11.glEnable(GL11.GL_TEXTURE_2D);

Tried both don’t seem to work.
This my initialize method;

http://img690.imageshack.us/img690/4665/initj.png

Ooops, I forgot you are using Slick. I have never used it (except for the utils to load PNG textures).

Some random things that probably wont work (sorry :-():

  • Do you try to create the image AFTER this.getApp().start() is called?
  • The init() method is of a class inherited from org.newdawn.slick.BasicGame? Although I don’t think it’s relevant for this error, you could append the @Override tag to it, so you get a warning if it doesn’t override for some reason.
  • If your class is inherited from *.BasicGame, do you call the super() constructor on your constructor?

Yeah I create after this.getApp().start() and the init method of that is not thats in Main which has the superclass BasicGame and it is calling Super(String).
Really not sure, if I was in java2d this would be easy.
But slick is awesome ¬.¬

First of all I’d like to say that I’m a complete newbie. Perhaps that’s why I find something odd in the next piece of code:


try {
    Image image = null; 
    try {
        image = new Image(256,256);
    } catch (SlickException se) {se.printStackTrace();
}

//This is the line I'm talking about. What's the purpose of this?
image.getScaledCopy(player.getIcon.getWidth(), player.getIcon.getHeight());

Graphics g = image.getGraphics();
Image pixel = null;

You’re not setting another Image with the return of getScaledCopy(). Does this method sets ‘image’ to it’s scaled copy?

Anyway I don’t think it has something to do with your problem. But I do think that it might be a variable scope problem. Have you tried creating the ‘image’ outside the method and initializing it inside?

Oh no that was an accident I forgot to put image = image.getScaledCopy().
Yeah the image I have tried that the image in first post is made in init().
Yet again I’m just not sure what’s going on, should be simple.

You tried this:


public void init(){
    Image image;
}

public void render(Graphics g){
    image = new Image(256,256);
}

Or this (this is what I meant)


Image image;

public void init(){
    image = new Image(256,256);
}

public void render(Graphics g){
    image.resizeOrDrawThisImage();
}

This is in render and the image is created in init()

http://img717.imageshack.us/img717/246/inrender.png

Window is crashed…

It would help if you pasted your actual code instead of screenshots.

Just rolled out of bed so I could be wrong/late.

Define the variable you’d like to use to connect a image to “Sprite”.
Define the image/path, than link it to that Sprite variable.
Than render that Sprite variable after linking the Image to it.

public BufferedImage Sprite;
public ImageIcon player = new ImageIcon("path/to/file.png");

	public void paint(Graphics g) {
		Sprite = player.getImage();
		g.drawIage(Sprite, 20, 20, null);
	}

Hope it helps some :slight_smile:

Yeah just post an SSCCE please :slight_smile:

Its slick not java 2d mate.

And i’ll edit my thread with sscce in a minute.

edit; updated.