This site requires JavaScript, please enable it in your browser!
Greenfoot back
Vellyxenya
Vellyxenya wrote ...

2015/10/10

Reading an image

Vellyxenya Vellyxenya

2015/10/10

#
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);"
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);
        }
    }
}
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?
davmac davmac

2015/10/10

#
You need to handle the possible exception:
1
2
3
4
5
6
7
8
try {
    reader();
    // more code - if  execution gets here, reader() succeeded
}
catch (IOException ioe) {
    // call to reader() failed
    ioe.printStackTrace();  // just an example of what you can do
}
ElNo ElNo

2015/10/10

#
Hi! If you declare, that a method throws an exception it has to throw it. An IOException shows that sometihing has gone wrong with the loading of your file. Here is a quite good overview: http://examples.javacodegeeks.com/core-java/io/ioexception/java-io-ioexception-how-to-solve-ioexception/ If you make sure your image always axists you can delete the "Throws"- part until you have more experience with exceptions. Can't you use GreenfootImage instead of these extern solution?
Vellyxenya Vellyxenya

2015/10/10

#
So I tried this:
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
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);
 
    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++)
                {
                    try{
                        File file = new File("Rift.png");
                        BufferedImage image = ImageIO.read(file);
                        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]);
                        }
                    } catch(IOException ioe){
                        ioe.printStackTrace();
                    }
                    setImage(rift);
                }
            }
        }
    }
 
    public void movements()
    {
        if(this.getX()>70)
        {
            move(-1);
        }
    }
}
..but the terminal window keeps telling me : "java.imageio.IIOException: can't read input file" In fact I have an image (the file to read) that is black and transparent and in my code, i'm trying to replace the black color by the background color.. is there a better method than reading the file pixel by pixel?
davmac davmac

2015/10/10

#
i'm trying to replace the black color by the background color.. is there a better method than reading the file pixel by pixel?
Not that I can think of.
..but the terminal window keeps telling me : "java.imageio.IIOException: can't read input file"
Probably the path to the file is wrong. You're not using the Greenfoot API, so you would need to specify the full path to the image (i.e. including the "images" folder if the file is in that folder), and if you want it to work when you upload it as an applet you would need to figure out how to deal with it as a non-file. You can read a pixel from a GreenfootImage very easily, so it's probably better that you just do that. Use the getColorAt(x,y) method to get a Color instance, and use getRed()/getGreen()/getBlue() on the color.
Vellyxenya Vellyxenya

2015/10/10

#
Thank you very much, it finally works! :)
You need to login to post a reply.