java - How to read colors from an image -


this called program read spritesheet, in class.

private spritesheet spritesheet = new spritesheet("/sprite_sheet.png"); 

this tried read pixel colors sprite sheet colored black, dark grey, light grey, , white. meant print out color details first row of pixels.

public spritesheet(string path) {     bufferedimage image = null;      try {         image = imageio.read(spritesheet.class.getresourceasstream(path));     } catch (ioexception e) {         e.printstacktrace();     }     if(image==null){         return;     }      this.path = path;     this.width = image.getwidth();     this.height = image.getheight();      pixels = image.getrgb(0, 0, width, height, null, 0, width);      for(int = 0; i<pixels.length;i++){         pixels[i] = (pixels[i] & 0xff)/64;     }     for(int i=0; i<8;i++){         system.out.println(pixels[i]);     } 

when run not print numbers coded. how fix this? , how reading of colors work?

if aren't getting output either println lines aren't getting reached or (far less likely) there's wrong console configuration hiding output. we assume, correctly, methods called here either return or throw (i.e. nothing hanging). presuming former, opportunity see happen if image fails load , return.

since print stack trace of exceptions not report seeing stack trace, means imageio.read() must returning null. happen, according the documentation:

the inputstream wrapped in imageinputstream. if no registered imagereader claims able read resulting stream, null returned.

i know png supported generally, perhaps specific flavor of png not (e.g. compressed, 4-color palette or something; don't know limitations of imageio here). suggest try saving image in different format, or testing more baseline png format (e.g. uncompressed 24-bit, no transparency, no interlacing).

if analysis here correct, moral of story is: improve error handling. don't silently fail; when performing tasks critical correct functioning of program.

if not correct, suggest either drop debugging printouts in method or step through in debugger see why println lines aren't getting executed.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -