[SOLVED] .gif image becomes black and white
My program is put together from a few online tutorials because I'm new to working with images. It displays the image I expected it to, but it's black and white (took me a while to recognize it!)
Since the code is big, I'm only posting what I think is relevant.
Code Java:
public class Board extends JPanel implements ActionListener {
public MovableObject[] movables;
public Board(){
movables = new MovableObject[MAX_MOVABLE_OBJECTS];
movables[0] = new MovableObject(0); // (0) - player character;
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(movables[0].getImage(), movables[0].getX(), movables[0].getY(), null);
}
public class MovableObject {
BufferedImage still;
AnimatedSprite sprite;
public MovableObject(int spriteRefNum) {
sprite = sprites[spriteRefNum];
still = sprite.getCurrentImage();
}
public Image getImage() {
return still;
}
}
static public AnimatedSprite[] sprites = new AnimatedSprite[MAX_SPRITES_GLOBAL];
public class AnimatedSprite {
private double x; // upper left coordinates of the sprite
private double y;
private double lastX; // the coordinates the sprite was at the previous frame
private double lastY;
private HashMap<String, int[]> frames;
private BufferedImage[] images; // all the images of this AnimatedSprite
private String currentAnim; // the current 'state' or animation that the sprite is in
private int currentFrame;
private int[] currentFrameSet; // current set of frames being used
public AnimatedSprite(BufferedImage[] images) { // class constructor
this.images = images;
frames = new HashMap<String, int[]>();
}
public void addNewAnimation(String name, int[] set) {
frames.put(name, set);
setAnimation(name);
}
public void setAnimation(String name) { // Sets the current animation of the Sprite
if(frames.containsKey(name)) {
currentAnim = name;
currentFrameSet = frames.get(currentAnim);
currentFrame = 0;
}
}
public void setLocation(double x, double y) { // upper left corner
this.x = x;
this.y = y;
}
// added ************************
public BufferedImage getCurrentImage() {
return images[0];
}
// *****************************
public int getWidth() { // Gets the width of one frame
return images[0].getWidth();
}
public int getHeight() { // Gets the height of one frame
return images[0].getHeight();
}
}
public class ImageUtil {
public static BufferedImage loadImage(String ref) {
BufferedImage bimg = null;
try {
bimg = ImageIO.read(new File(ref));
} catch (Exception e) {
e.printStackTrace();
}
return bimg;
}
public static BufferedImage[] splitImage(BufferedImage img, int cols, int rows) {
int w = img.getWidth()/cols;
int h = img.getHeight()/rows;
int num = 0;
BufferedImage imgs[] = new BufferedImage[w*h];
for(int y = 0; y < rows; y++) {
for(int x = 0; x < cols; x++) {
imgs[num] = new BufferedImage(w, h, img.getType());
// Tell the graphics to draw only one block of the image
Graphics2D g = imgs[num].createGraphics();
g.drawImage(img, 0, 0, w, h, w*x, h*y, w*x+w, h*y+h, null);
g.dispose();
num++;
}
}
return imgs;
}
}
Re: .gif image becomes black and white (against my wishes)
It seems that the code works with some pictures, although I can't figure out the rule (which format or color depth), but it's kind of solved.