简体   繁体   English

如何获取Java小程序中像素的颜色以生成图?

[英]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. 我正在使用Java创建一个小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. 理想情况下,我希望能够使applet扫描图像文件并根据图像在屏幕上创建图像。 Please try to keep the answers a little bit simple, as I am still getting used to all of the technical terms. 请尽量使答案简单一点,因为我仍然习惯所有技术术语。

Thanks, ~Rane 谢谢,〜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: getImage方法:

    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 如果您想从那里拿走我的代码段,请告诉我,让您不满意:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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