0

So I'm continuing with my app to pick colors out of a picture taken by the user and returning the RGB values. The problem is that when I attempt to get the green value for the color, I get an error saying "cannot invoke getGreen() on the Primitive Type int". Here's the code I wrote:

Bitmap bitmap = ((BitmapDrawable)iv.getDrawable()).getBitmap(); 
int touchedRGB = bitmap.getPixel(x,y);          
rgbvals.setText("Color Value" + "#" + Integer.toHexString(touchedRGB));
rgbvals.setTextColor(touchedRGB);
int gval = touchedRGB.getgreen();

I also attempted to write the final line as

    String gval = Integer.toString(touchedRGB).getGreen();

But of course getGreen() can only be used on type int. Thanks in advance for the help guys!

Johnathan
  • 15
  • 1
  • 7
  • 3
    The last time I checked primitives didn't have methods. – Kevin Bowersox Oct 31 '13 at 09:17
  • Could you explain this a little further? I've read that before but it really doesn't make sense to me (i'm transitioning from C to Java so some of the finer nuances are a little weird) – Johnathan Oct 31 '13 at 09:27

2 Answers2

1

You can use the static method green of the Color class :

Return the green component of a color int. This is the same as saying (color >> 8) & 0xFF

int gval = Color.green(touchedRGB);

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • Yes you did. Either test your code, or go read the Javadocs. – Dawood ibn Kareem Oct 31 '13 at 09:20
  • @DavidWallace http://developer.android.com/reference/android/graphics/Color.html Go read the android doc. – Alexis C. Oct 31 '13 at 09:22
  • My apologies. I had no idea that the android people had added a completely different class called `Color`. You should probably note in your answer that this has to be `android.graphics.Color`, not `java.awt.Color`. I will edit MY answer similarly. Thank you for educating me. – Dawood ibn Kareem Oct 31 '13 at 09:25
  • OK, thanks @ZouZou. My answer is gone. I've upvoted yours. I guess I should stay away from Android questions - it's not really Java, in a way. – Dawood ibn Kareem Oct 31 '13 at 09:31
0

Error is here:

touchedRGB.getgreen();

Java compiler try to say that :

Since int touchedRGB = bitmap.getPixel(x,y); touchedRGB is primitive data type(it is integer) you can't call a method on primitive data types, they are not objects.

ozanonurtek
  • 306
  • 5
  • 18