i am trying to code a game from scratch, I have multiple types of buttons, one of those types has to switch the sprites it uses after it gets clicked. all buttons have one set of sprites, but that specific type has an extra field with the second set of sprites.
I have a solution that works, but was trying to us generics to swap the images instead (trying to learn something new). I think i don't understand generics, because the way i did it does nothing.
public class Button {
protected BufferedImage[] imgs;
}
public class SpecialButton extends Button{
private BufferedImage[] imgs2;
public void swapImage() {
//this works the way it was intended
BufferedImage[] tmp = imgs;
imgs = imgs2;
imgs2 = tmp;
}
public void swapImageDoesNothing() {
// this does nothing
swap(imgs, imgs2);
}
public static <T> void swap(T obj1, T obj2){
T tmp = obj1;
obj1 = obj2;
obj2 = tmp;
}
}
I tried multiple thing, also tried making the swap() method not static, but the goal was to have the static in another Utils class for other classes to use, don't know if i need it anywhere else, but i was trying to learn something about generics
public final class Utilities {
private Utilities(){}
public static <T> void swap(T obj1, T obj2){
T tmp = obj1;
obj1 = obj2;
obj2 = tmp;
}
}