Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem with Slick 2d Image / Graphics  (Read 4743 times)
0 Members and 1 Guest are viewing this topic.
Offline SmokeNWrite

Junior Member


Projects: 1



« Posted 2012-02-09 22:40:06 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
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.
Offline aazimon
« Reply #1 - Posted 2012-02-09 22:53:40 »

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

Junior Member


Projects: 1



« Reply #2 - Posted 2012-02-09 23:02:55 »

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.



Peace
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline yuma

Junior Member


Medals: 2
Projects: 1


Monkeys are listening


« Reply #3 - Posted 2012-02-09 23:09:38 »

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

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

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #4 - Posted 2012-02-09 23:16:32 »

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.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-02-10 06:04:37 »

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

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #6 - Posted 2012-02-10 11:45:29 »

I thought I was I'll download new LWJGL
Thanks mate.
Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #7 - Posted 2012-02-10 11:59:52 »

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




Help?
Offline yuma

Junior Member


Medals: 2
Projects: 1


Monkeys are listening


« Reply #8 - Posted 2012-02-10 12:09:32 »

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:
1  
import org.lwjgl.opengl.GL11.glEnable(GL11.GL_TEXTURE_2D);

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #9 - Posted 2012-02-10 12:18:14 »

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:
1  
import org.lwjgl.opengl.GL11.glEnable(GL11.GL_TEXTURE_2D);


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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline yuma

Junior Member


Medals: 2
Projects: 1


Monkeys are listening


« Reply #10 - Posted 2012-02-10 12:28:11 »

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?

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #11 - Posted 2012-02-10 12:37:36 »

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 ¬.¬
Offline H3rnst

Senior Newbie


Medals: 1



« Reply #12 - Posted 2012-02-10 13:47:54 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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?


Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #13 - Posted 2012-02-10 13:50:55 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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.
Offline H3rnst

Senior Newbie


Medals: 1



« Reply #14 - Posted 2012-02-10 13:58:09 »

You tried this:

1  
2  
3  
4  
5  
6  
7  
public void init(){
    Image image;
}

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


Or this (this is what I meant)
1  
2  
3  
4  
5  
6  
7  
8  
9  
Image image;

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

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

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #15 - Posted 2012-02-10 14:18:52 »

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



Window is crashed..
Offline sproingie

JGO Kernel


Medals: 202



« Reply #16 - Posted 2012-02-10 17:25:24 »

It would help if you pasted your actual code instead of screenshots.
Offline GabrielBailey74
« Reply #17 - Posted 2012-02-10 21:01:49 »

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.
1  
2  
3  
4  
5  
6  
7  
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 Smiley

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2012-02-10 22:20:44 »

Yeah just post an SSCCE please Smiley

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #19 - Posted 2012-02-11 12:11:20 »

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.
1  
2  
3  
4  
5  
6  
7  
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 Smiley
Its slick not java 2d mate.

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

edit; updated.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #20 - Posted 2012-02-11 15:04:08 »

This is line 40 in GraphicsFactory:
1  
fbo = GLContext.getCapabilities().GL_EXT_framebuffer_object;


I have no idea how a NullPointer can be thrown there, unless this is an LWJGL bug where GLContext.getCapabilities() returns null.

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #21 - Posted 2012-02-11 15:55:58 »

This is line 40 in GraphicsFactory:
1  
fbo = GLContext.getCapabilities().GL_EXT_framebuffer_object;


I have no idea how a NullPointer can be thrown there, unless this is an LWJGL bug where GLContext.getCapabilities() returns null.

Exactly what I mean, the error is totally confusing and shouldn't happen its a pretty simple procedure I'm trying and I have tried several ways around which have also failed.

I can't paste anymore code neither cause I ain't at home.
Would really appreciate it if somebody could figure this out. Smiley
Peacee
Offline Matthias

Senior Member


Medals: 3
Projects: 1


TWL - Themable Widget Library


« Reply #22 - Posted 2012-02-12 18:13:18 »

GLContext.getCapabilities() return null when the current thread has no GL context active.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #23 - Posted 2012-02-12 19:31:09 »

*FACEPALM* Of course!

@OP
You're loading that image from another thread! You should do everything on the same thread as the rendering thread and after the GL Context has been initialized Tongue

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #24 - Posted 2012-02-12 21:07:01 »

*FACEPALM* Of course!

@OP
You're loading that image from another thread! You should do everything on the same thread as the rendering thread and after the GL Context has been initialized Tongue

Surprisingly I already thought of it but never did try it, I'll just make a trigger or something that I can set in that thread then activate in the other thread.

Nice one mate (:

EDIT:
Wait don't know if that'll work because I tried getting graphics of a Image in render() and it makes the game freeze.
So strange... :/
Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #25 - Posted 2012-02-20 12:57:50 »

Stilll not fixed!

Peace..
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #26 - Posted 2012-02-20 19:29:27 »

Any more info you can give us about this?

Offline SmokeNWrite

Junior Member


Projects: 1



« Reply #27 - Posted 2012-03-02 11:37:18 »

Not really sure what else there is to say everything else works and this would be simple in java2d.

There is a converter but then I would have to convert back lol

Peace
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!