Search code examples
javacolorsappletpixel

Color at Certain Pixel location inside Java Applet?


I am currently making a paint like program with Java Applets. I want to simulate a bucket tool by using recursion and checking each pixel around a given point, however I am having trouble getting the RGB value at a given pixel.

Is there a way to do something such as

public void paint(Graphics g) {
    g.getPixelAt(X, Y);
}

Or something?


Solution

  • Graphics is virtual concept and does not support what you are trying to do

    What you need to do is paint to a surface that you can interact with, something like a BufferedImage.

    The basic idea would be painting all effects to this buffered image and using Graphihcs#drawImage to actually render the image to the screen.

    From there you can us BufferedImage#getRaster which will provide you with a WritableRaster object which has get/setPixel methods.