Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: .gif image becomes black and white (against my wishes)

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [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.

    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;
    		}
    }
    Last edited by Jolanda; April 4th, 2011 at 03:28 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. (URGENT) Plz help me with the black jack program.
    By diogo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 05:08 AM
  2. Replies: 2
    Last Post: February 14th, 2011, 05:36 PM
  3. Black Jack Statistics game? lost.
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2010, 08:20 AM
  4. Add white spices before the String element
    By bookface in forum Java Theory & Questions
    Replies: 1
    Last Post: March 23rd, 2010, 09:50 PM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 10:50 AM