0

So, I am making a small Java Game as a school project and want to return an image, which should be in a HashMap. In the method I call, i want to give a java.awt.Color as a parameter and make a switch statement on it.

(Note: All colors going INTO the function are default Colors like "Color.red" or "Color.green"!)

I already tried to do it with the RGB value of the Color, but that doesn't work.

Heres the NOT WORKING code:

   private static HashMap<Color, Image> blocks_hash;

    public static void setBlock(Color color, Image image) { blocks_hash.put(color, image); }

    public static Image getBlock(Color color) {
        if(blocks_hash.containsKey(color))
            return blocks_hash.get(color);
        else
            switch (color.getRGB()) {
                case Color.red.getRGB():
                    return getBlocks().getSubimage(0, 0, 60, 60);
            }
        return null;
    }

PlayWolfYT
  • 99
  • 1
  • 3
  • 8
  • btw . what is not working here? – Onkar Musale Apr 03 '19 at 13:23
  • The problem is the switch statement, thats atleast what IntelliJ says, requires a constant expression. "Color.red" and "Color.red.getRGB()" are not accepted for a strange reason – PlayWolfYT Apr 03 '19 at 13:25
  • The Color class and the getRGB() method are provided by java itself. I'm using java.awt.Color and its methods... – PlayWolfYT Apr 03 '19 at 13:29
  • yeah i get it..try printing values of color.getRGB() and Color.red.getRGB() and compare them both.. debug it – Onkar Musale Apr 03 '19 at 13:30
  • Your idea is good and it would really make sense, but even if I compare them, I can't use a switch statement because it doesn't accept Color.red.getRGB() and it also doesn't want me to compare to a variable with the value (int) of Color.red.getRGB() because "Constant expression required" (errormessage)... – PlayWolfYT Apr 03 '19 at 13:38

1 Answers1

0

You can not make a switch case because it constant already, see here

You can change you colors to enum fields

class newClass {
 enum Colors {RED,GREEN,BLACK}

 public static Image getBlock(Colors colors) {
    switch (colors){
        case BLACK:
            System.out.println("BLACK");;
            break;
        case GREEN:
            System.out.println("GREEN");;
            break;
        case RED:
            System.out.println("RED");
            break;
            default:
                System.out.println("non color");
    }
}

P.S Without switch-case

class newClass {
 enum Colors {RED,GREEN,BLACK}

 public static Image getBlock(Colors colors) {

    if(colors==BLACK){
      System.out.println("BLACK");
}else if(colors==GREEN){
      System.out.println("GREEN");
}else if(colors==RED){
      System.out.println("RED");
}else
      System.out.println("non color");
    }
}

And apply to your code

public static Image getBlock(Color color) {
    if (blocks_hash.containsKey(color)) {
        if (color == Color.BLACK) {
            System.out.println("Black");
        } else if (color == Color.CYAN) {
            System.out.println("CYAN");
        }
    } else {
        System.out.println("non color");
    }
    return null;
}
Dred
  • 1,076
  • 8
  • 24
  • Thanks for the solution but i still have one more question: Could I also use an ongoing _else if_ method? Something like that: ```if(color == Color.red) { }``` – PlayWolfYT Apr 03 '19 at 13:46
  • Where do you want make it? In some case of switch-case construction? – Dred Apr 03 '19 at 13:51
  • No, I would just put it instead of the whole switch method: ```public static Image getBlock(Color color) { if(blocks_hash.containsKey(color)) return blocks_hash.get(color); else if(color == Color.red) return getBlocks().getSubimage(0, 0, 60, 60); else if(color == Color.green) return getBlocks().getSubimage(60, 0, 60, 60); ...``` – PlayWolfYT Apr 03 '19 at 13:54
  • I am confused %) Do you want swicth-case or if-else. it both equals solution. If you want use only if-else, do it, you are programmer. If you want switch-case do that. Maybe [that](https://www.geeksforgeeks.org/switch-statement-in-java) article will be helpful for you. Look at 'Nested Switch Case statements' paragraph – Dred Apr 03 '19 at 13:58
  • I would like to do a switch-case but, because it does not work the way I wanted it to, I might do a if-else, is that clear? xD – PlayWolfYT Apr 03 '19 at 14:03
  • If I understood correct, see P.S block in my answer – Dred Apr 03 '19 at 14:13