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

2014/11/5

getImage complications....

Alwin_Gerrits Alwin_Gerrits

2014/11/5

#
I'm trying to get an if-statement working and i've tried various things, but it doesn't seem to return true... ever.... any ideas? I'm using :
1
if(getImage().equals(new GreenfootImage("feather.png")))
and i tried:
1
2
private GreenfootImage feather = new GreenfootImage("feather.png")))
if(getImage().equals(feather));
And even some other things I found in previous discussions about this subject but they don't seem to work. I know it gets into the method itself because ... well it's an act method and the object is in the world and does respond, but just this if(getImage()) statement doesn't work. Again, if anybody has a solution please help ^^
danpost danpost

2014/11/5

#
You should probably show the entire class code as how you set the image may be important also.
Alwin_Gerrits Alwin_Gerrits

2014/11/5

#
Actually, i think i will just change it a bit and forget about the getImage. I've tried using it in a lot of ways and i think making a variable with a value declaring weither it is or isn't that image is a lot easier.. actually i allready tried and it is indeed a hell of a lot easier. Thanks anyway :)
Super_Hippo Super_Hippo

2014/11/5

#
I don't know if it is possible at all with the 'equals' method. I only use it with strings. In some of my scenarios I just use 'getImage == /* or != */ ... ' and it works.
danpost danpost

2014/11/5

#
Super_Hippo wrote...
I don't know if it is possible at all with the 'equals' method. I only use it with strings. In some of my scenarios I just use 'getImage == /* or != */ ... ' and it works.
I am sure it is possible with other Object types -- for example 'java.awt.Color' or 'java.awt.Font' objects. However, you should make sure that the method is well defined for what you are using it for. I believe the method is inherited from the Object class as an empty method and must be overridden to give it meaning for the type of objects being compared (like for 'java.awt.Color', all four color values, 'r', 'g', 'b', and 'a', must be equal, '==', for 'equals' to return a true value). The most recent subclass that it is defined would determine whether the return value would be appropriate for the objects being compared (you could also override it yourself in the class you want to compare objects of, if you want). Anyway, I believe that if you compare two images, the size of the images and the color values of each pixel must all be the same for a return value of true.
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
Uuh... then this should have worked if the 2 images were exactly the same? Because they were and it didn't return true.... I used a setImage command somewhere and a getImage command here. After that i used the exact same image file and it still didn't return true... bug then? Be aware that i'm not interested in using it as a working method anymore, but I can use code like this and it doesn't return true:
1
2
3
setImage("feather.png");
if(getImage().equals("feather.png"))
{System.out.println("it does work though");}
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
This also doesn't seem to respond with true:
1
2
3
4
String feather = "feather.png";
setImage(feather);
if(getImage().equals(feather))
{System.out.println("it does work though");}
danpost danpost

2014/11/6

#
In both cases above, you are trying to compare a String with a GreenfootImage, which will always return false. In the first case, you should try the following for line 2:
1
if (getImage().equals(new GreenfootImage("feather.png")))
and the same for line 3 in the second.
Super_Hippo Super_Hippo

2014/11/6

#
Does this work for you?
1
2
3
4
GreenfootImage img = new GreenfootImage("feather.png");
setImage("feather.png"); //or setImage(img);
if(getImage() == img))
{System.out.println("it does work though");}
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
Danpost, your idea was nice, but it still doesn't return true. I tried it then tried if it was even getting into the overall method, but it still doesn't work and gets into the method. I used it like this:
1
2
3
setImage("feather.png");
if(getImage().equals(new GreenfootImage("feather.png"))) 
{System.out.println("it does work though");}
Hippo, nice job mate. Your idea actually worked perfectly. Problem one is I would need more then one img if I were to use it in my reall code and problem two is I would have to make all of them static (or at least public) seen I was setting the image in one class and getting it in the other. But again, I allready found a way to do it without getImage, but it's still great to know there's actually a way to do it. Thanks for the effort danpost and ty for a possible solution for next time hippo :).
Super_Hippo Super_Hippo

2014/11/6

#
It is not bad to save images public static. In my newer projects, I am also saving all images in a world class. I started that after a game where I created all images with the 'setColorAt' method and I didn't want to do that for every new object ^^
Alwin_Gerrits Alwin_Gerrits

2014/11/6

#
Well, I have been told to watch out using public static because it isn't considered good to use to many while programming (I use them often enough though and they fix a lot of problems). By the way, can I ask what kind of game you're working on right now? Just curious ^^.
Super_Hippo Super_Hippo

2014/11/6

#
It is often not good to fix these 'non-static variable from static context' errors with making it static. But there are useful moments when to use it. :) Updated Insect Wars and the Bomberman game lately. Right now, I am not working on anything. Sometimes I have an idea and then, I try to add it. ;)
You need to login to post a reply.