简体   繁体   中英

How to get the color of a pixel in a Java applet to produce a map?

I am working with a Java to create a small applet. I am interested if there is a way I can "scan" an image to get the color values of a certain pixel. I would prefer to not have to display the image on the screen, but if you find that is the only way, please tell me. I would ideally like to be able to have my applet scan an image file and create an image on the screen according to the image. Please try to keep the answers a little bit simple, as I am still getting used to all of the technical terms.

Thanks, ~Rane

What I have so far:

import java.applet.Applet;

public class LoadGuideImage {

Applet applet;

public LoadGuideImage(Applet applet){
    this.applet = applet;
}

public String getPixelColor(String pathToImage, int Xpix, int Ypix){
    int redC = 0;
    int greenC = 0;
    int blueC = 0;

    //Get Pixel colors here and save to ints

    return redC + " " + greenC + " " + blueC;
}

}

Are you suggesting something like this 'the other guy'?:

        BufferedImage img = (BufferedImage) getImage(pathToImage);

    System.out.println("Color: " + img.getRGB(3, 3));

getImage method:

    public Image getImage(String path) {

    Image img;
    URL url = null;

    try {
        url = applet.getDocumentBase();
    } catch (Exception e){
        // TODO: handle exception
    }
    img = applet.getImage(url, path);

    return img;
}

nice name by the way. So I was in the same position a while ago.

use this for your get image method, just tweak it and use it to benefit you:

public class ImageLoader {

    public BufferedImage load(String path){
        try {
            return ImageIO.read(getClass().getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

I have a been making a game that uses individual pixels to load those pixels and then put tiles where certain pixels are located in the map. If you would like to take my code snippet from there let me know and ill hook you up :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM