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

2013/1/12

How to detect a color underneath a transparent image.

Minion1 Minion1

2013/1/12

#
Boy this problem is killing me. danpost, thanks again, your last solution has the program moving without stopping, but I've now encountered yet another new problem. And this is a request to anyone who might know a solution. :-/ The cursor object is transparent, the map moves underneath the cursor. Think of the cursor as an anchor for the eye. It's made to look like the cursor is moving, when in reality the map is moving underneath the cursor. Here's the problem: In order for the map to work, the compiler must check the red level of a certain pixel at (16, 11) in each tile that the map is constructed with. My plan was to use the cursor as the "anchor point" to determine where on the map to detect the red pixel. So what I'm trying to do is get the program to use the cursor's location, but detect the color of the pixel that is UNDERNEATH it. So far, so good. But the program is now aiming at the cursor itself instead of the image underneath it, so the red value returned is always 0. It's a shame we can't post pictures here, it would make things easier to see if I could screenshot it. Anyway, here is the pertinent code (Scroller is the superclass, WorldMap and Cursor are underneath it): From Scroller:
public void checkKeyPress()
    {
        timer++;
        if(Greenfoot.isKeyDown("W") && timer >= 10)        
        {
            setLocation(getX(), getY() + MOVEDISTANCE);
            ((WorldMap)getWorld().getObjects(WorldMap.class).get(0)).redCheck();
It calls the redCheck method inside of WorldMap.. From WorldMap:
public void redCheck()
    {
        Cursor c = ((Cursor)getWorld().getObjects(Cursor.class).get(0));
        int cursorX = c.getX();
        int cursorY = c.getY();
        int red = getImage().getColorAt(cursorX, cursorY - 5).getRed();
        System.out.println(red);
I get a reference to the cursor object, and with that get the location of the cursor (remember, the cursor is transparent). Then I attempt to get the color of a pixel at the cursor's location. Where I'm stuck is that it's attempting to get the cursor's color. Is there anyway to get it to see underneath the cursor at the same location? Thank you all. I've been struggling with this simple problem all week.
Minion1 Minion1

2013/1/12

#
Ok, I've done some testing around at it appears that even if I use the cursor's location and with it tell it to check a pixel far away from the cursor it STILL returns 0. It's not seeing the worldMap image. In the World constructor, I have this: setPaintOrder(Cursor.class); Do you think that might be causing it?
danpost danpost

2013/1/12

#
Is it not possible to use:
World world = getWorld();
int cellsize = world.getCellSize();
int x = getX()*cellsize+16;
int y = getY()*cellsize+11;
int red = world.getBackground().getColorAt(x, y).getRed();
from the Cursor class?
Minion1 Minion1

2013/1/12

#
It might be.. I sort of suck at math. I was hoping to make it easy, but if this will help to pinpoint the necessary pixels then it might work. But the problem is that the WorldMap is an Actor object, it's not a background.
danpost danpost

2013/1/12

#
Then, again from the Cursor class:
WorldMap wm = (WorldMap)getWorld().getObjects(WorldMap.class).get(0);
int x = wm.getImage().getWidth()/2+(getX()-wm.getX());
int y = wm.getImage().getHeight()/2+(getY()-wm.getY());
int red = wm.getImage().getColorAt(x, y).getRed();
Minion1 Minion1

2013/1/13

#
Well, looks like that's not working either. :-P It's still only giving me a red value of 0, which means that it's seeing the cursor only, which has a transparent red value of 0. Of course, I tried to move the red detection off the cursor, and it still gave me a value of zero. I'm not certain what's going on. I wish I were better at this. I've been stuck here for so long, I think I'm just going to have to completely start over and re-work the code because I can't move forward without this working. Thanks for the help danpost.
Minion1 Minion1

2013/1/13

#
Holy cow!! You did it danpost. Thank you so much. I didn't implement the code you suggested above properly so I thought it didn't work, but after realizing my mistake I tried it again and fixed my error. It's working like a charm now. Thank the heck out of you!
kiarocks kiarocks

2013/1/16

#
Just a small note, you can upload pictures somewhere and then attach them with (without spaces).
You need to login to post a reply.