简体   繁体   English

Java Applet中特定像素位置的颜色?

[英]Color at Certain Pixel location inside Java Applet?

I am currently making a paint like program with Java Applets. 我目前正在使用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. 我想通过使用递归并检查给定点周围的每个像素来模拟存储桶工具,但是我在获取给定像素的RGB值时遇到了麻烦。

Is there a way to do something such as 有没有办法做这样的事情

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

Or something? 或者其他的东西?

Graphics is virtual concept and does not support what you are trying to do Graphics是虚拟概念,不支持您尝试做的事情

What you need to do is paint to a surface that you can interact with, something like a BufferedImage . 您需要做的是在可以交互的表面上绘制,例如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. 基本思想是将所有效果绘制到此缓冲的图像上,并使用Graphihcs#drawImage将图像实际呈现到屏幕上。

From there you can us BufferedImage#getRaster which will provide you with a WritableRaster object which has get/setPixel methods. 从那里您可以使用BufferedImage#getRaster ,它将为您提供一个具有get/setPixel方法的WritableRaster对象。

You could use Applet.getLocationOnScreen() and java.awt.Robot.createScreenCapture() to find and capture the part of the applet you want. 您可以使用Applet.getLocationOnScreen()和java.awt.Robot.createScreenCapture()来查找和捕获所需的applet部分。

This question poses a similar problem. 这个问题提出了类似的问题。

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

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