Hi again. I am trying to make code that set all black pixels to white. But my code does nothing. Why is that?

package net.viped;

import java.awt.image.BufferedImage;

public class ImageManipulation {
    public ImageManipulation() {

    }

    public BufferedImage setTransparent(BufferedImage image) {

        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                if (image.getRGB(x, y) == (0x000000)) {
                    System.out.println("This never shows up");
                    image.setRGB(x, y, 0xffffff);
                }
            }
        } return image;
    }
}

Oh, print getRGB returns -16777216 where it should return 000000 thats the problem but shouldnt java be supposed to use hex colors?

Okay, now it works better but how about changing color to transparent, it should work by modifying alpha composition? Well my code doesnt.

package net.viped;

import java.awt.Color;
import java.awt.image.BufferedImage;

public class ImageManipulation {
    public ImageManipulation() {

    }

    public BufferedImage setTransparent(BufferedImage image) {
        image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                System.out.println(image.getRGB(x, y));
                int pixel = image.getRGB(x, y);
                if (image.getRGB(x, y) == (0)) {
                    System.out.println("Now this shows up");
                    Color newColor = new Color(00, 00, 00, 00);
                    image.setRGB(x, y, 0x00000000);
                }
            }
        } return image;
    }
}

And one more update. All debug info I can get shows the code works right but nothing happening to the pic. Heres the code:

package net.viped;

import java.awt.Color;
import java.awt.image.BufferedImage;

public class ImageManipulation {
    public ImageManipulation() {

    }

    public BufferedImage setTransparent(BufferedImage image) {
        image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                System.out.println(image.getRGB(x, y));
                int pixel = image.getRGB(x, y);
                printPixelARGB(pixel);
                if (image.getRGB(x, y) == (0x000000)) {
                    System.out.println("Now this shows up");
                    int newRGB = 0xFF00FFFF;
                    image.setRGB(x, y, newRGB);
                    pixel = image.getRGB(x, y);
                    printPixelARGB(pixel);
                }
            }
        } return image;
    }

    public void printPixelARGB(int pixel) {
        int alpha = (pixel >> 24) & 0xff;
        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;
        System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
      }
}

Okay, I am officially idiot. There was problem with from where I used this class. ImageManipulation class changes color almoust right. Only that it changes all the colors not just those where color is 0x000000

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.