Hi, i found on a forum a method to read an image's pixel at a given position but they use the "throws IOException" and all this mess, I have no idea what it is... I have been trying the following code but It tells me nullPointException when it's created and it shows me the line with : "int clr = image.getRGB(i, j);"
I tried to put the "reader();" method in the act() method but it tells me: "unreported exception java.io.IOException; must be caught or declared to be thrown"
Could you help me please?
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 | import greenfoot.*; import java.awt.Color; import java.io.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; public class Rift1 extends Rift { Color[][] pixels = new Color[ 140 ][ 50 ]; final GreenfootImage baseImage = getImage(); GreenfootImage rift = new GreenfootImage( 140 , 50 ); File file = new File( "Rift.png" ); BufferedImage image; public void reader() throws IOException { image = ImageIO.read(file); } public Rift1() { } public void act() { if (isPaused() == false ) { movements(); for ( int i = 0 ; i< 140 ; i++) { for ( int j = 0 ; j< 50 ; j++) { pixels [i][j] = getWorld().getColorAt( this .getX()- 70 +i, this .getY()- 25 +j); } } for ( int i = 0 ; i< 140 ; i++) { for ( int j = 0 ; j< 50 ; j++) { int clr = image.getRGB(i, j); int red = (clr & 0x00ff0000 ) >> 16 ; int green = (clr & 0x0000ff00 ) >> 8 ; int blue = clr & 0x000000ff ; Color color = new Color(red, green, blue); if (color == Color.BLACK) { rift.setColorAt(i, j, pixels[i][j]); } setImage(rift); } } } } public void movements() { if ( this .getX()> 70 ) { move(- 1 ); } } } |