Java 坐标出界!在爪哇,洪水泛滥
我试图找出在尝试实施洪水填充时为什么会出现“坐标超出范围!”。这是一个黑白图像,当洪水泛滥时,没有任何东西会发生,所以这是好的。所有的黑色区域都会被红色填满,有些区域会变成红色,直到我得到坐标超出边界的错误信息。以下是源代码:Java 坐标出界!在爪哇,洪水泛滥,java,bounds,coordinate,flood-fill,Java,Bounds,Coordinate,Flood Fill,我试图找出在尝试实施洪水填充时为什么会出现“坐标超出范围!”。这是一个黑白图像,当洪水泛滥时,没有任何东西会发生,所以这是好的。所有的黑色区域都会被红色填满,有些区域会变成红色,直到我得到坐标超出边界的错误信息。以下是源代码: public class FloodFiller extends JPanel implements MouseListener { private BufferedImage img; public void turnBlacknWhite() {
public class FloodFiller extends JPanel implements MouseListener {
private BufferedImage img;
public void turnBlacknWhite() {
int x, y;
int w = img.getWidth();
int h = img.getHeight();
// first compute the mean intensity
int totintensity = 0;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
int rgb = img.getRGB(x, y);
totintensity += (rgb >> 16) & 0xFF + (rgb >> 8) & 0xFF + rgb
& 0xFF;
}
}
int meanintensity = totintensity / (w * h);
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
int rgb = img.getRGB(x, y);
int intensity = (rgb >> 16) & 0xFF + (rgb >> 8) & 0xFF + rgb
& 0xFF;
if (intensity < meanintensity) { // it's darker than mean
// intensity
img.setRGB(x, y, 0); // turn black
} else { // it's lighter
img.setRGB(x, y, 0xFFFFFF); // turn white
}
}
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
floodFill(e.getX(), e.getY(), 0xFF0000);
this.repaint();
}
/**
* Fill the black area including and around (x,y) with color. If (x,y) is
* not black, do nothing.
*/
public void floodFill(int x, int y, int color) {
// TODO!
int rgb = img.getRGB(x, y);
int black = Color.black.getRGB();
if (rgb == black) {
if (x < img.getWidth() && y < img.getHeight() && x > 0 && y > 0) {
img.setRGB(x, y, color);
floodFill(x, y + 1, color);
}
if (x < img.getWidth() && y < img.getHeight() && x > 0 && y > 0) {
img.setRGB(x, y, color);
floodFill(x, y - 1, color);
}
if (x < img.getWidth() && y < img.getHeight() && x > 0 && y > 0) {
img.setRGB(x, y, color);
floodFill(x + 1, y, color);
}
if (x < img.getWidth() && y < img.getHeight() && x > 0 && y > 0) {
img.setRGB(x, y, color);
floodFill(x - 1, y, color);
}
}
}
// public void isValid (int width, int height) {
// int coordinate = 0;
// int width = img.getWidth();
// int height = img.getHeight();
// if (width && height => coordinate) {
//
// }
// }
public FloodFiller(String fileName) {
URL imgUrl = getClass().getClassLoader().getResource(fileName);
if (imgUrl == null) {
System.err.println("Couldn't find file: " + fileName);
} else {
try {
img = ImageIO.read(imgUrl);
} catch (IOException ex) {
ex.printStackTrace();
}
}
turnBlacknWhite();
setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
this.addMouseListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
g.drawImage(img, 0, 0, null);
}
public static void main(String[] args) {
final String fileName = args[0];
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Flood Filler");
frame.setContentPane(new FloodFiller(fileName));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
公共类FloodFiller扩展JPanel实现MouseListener{
专用缓冲图像img;
公共空间变黑变白(){
int x,y;
int w=img.getWidth();
inth=img.getHeight();
//首先计算平均强度
int=0;
对于(y=0;y>16)&0xFF+(rgb>>8)&0xFF+rgb
&0xFF;
}
}
int平均强度=总强度/(w*h);
对于(y=0;y>16)&0xFF+(rgb>>8)&0xFF+rgb
&0xFF;
如果(强度<平均强度){//它比平均值暗
//强度
img.setRGB(x,y,0);//变黑
}否则{//它会更轻
img.setRGB(x,y,0xFFFFFF);//变白
}
}
}
}
公共无效鼠标按下(MouseEvent e){
}
公共无效MouseEvent(MouseEvent e){
}
公共无效鼠标事件(鼠标事件e){
}
公共无效mouseExited(MouseEvent e){
}
公共无效mouseClicked(MouseEvent e){
洪水填充(e.getX(),e.getY(),0xFF0000);
这个。重新绘制();
}
/**
*用颜色填充包括(x,y)及其周围的黑色区域。如果(x,y)为
*不黑,什么也不做。
*/
公共空间泛洪填充(整数x、整数y、整数颜色){
//托多!
int rgb=img.getRGB(x,y);
int black=Color.black.getRGB();
如果(rgb==黑色){
如果(x0&&y>0){
图像设置RGB(x,y,颜色);
漫灌(x,y+1,颜色);
}
如果(x0&&y>0){
图像设置RGB(x,y,颜色);
漫灌(x,y-1,颜色);
}
如果(x0&&y>0){
图像设置RGB(x,y,颜色);
泛洪填充(x+1,y,颜色);
}
如果(x0&&y>0){
图像设置RGB(x,y,颜色);
泛洪填充(x-1,y,颜色);
}
}
}
//公共空白有效(整数宽度、整数高度){
//int坐标=0;
//int width=img.getWidth();
//int height=img.getHeight();
//如果(宽度和高度=>坐标){
//
// }
// }
公共泛洪填充(字符串文件名){
URL imgUrl=getClass().getClassLoader().getResource(文件名);
if(imgUrl==null){
System.err.println(“找不到文件:“+fileName”);
}否则{
试一试{
img=图像读取(imgUrl);
}捕获(IOEX异常){
例如printStackTrace();
}
}
turnBlacknWhite();
setPreferredSize(新维度(img.getWidth(),img.getHeight());
这个。addMouseListener(这个);
}
@凌驾
公共组件(图形g){
超级组件(g);
挫折地面(颜色:白色);
g、 drawImage(img,0,0,null);
}
公共静态void main(字符串[]args){
最终字符串文件名=args[0];
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame(“洪水填充物”);
frame.setContentPane(新的FloodFiller(文件名));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
任何可能知道为什么会发生这种情况的人?:)谢谢 未正确检查调用
floodFill()
的参数边界
请看以下几行:
if (x < img.getWidth() && y < img.getHeight() && x > 0 && y > 0) {
img.setRGB(x, y, color);
floodFill(x, y + 1, color);
}
if(x0&&y>0){
图像设置RGB(x,y,颜色);
漫灌(x,y+1,颜色);
}
假设你的图像是100像素高和宽。每个轴上的像素编号为0-99。Sayfloodfill()。if语句被签出,因此进入下两行,现在调用floodFill(30100,[color])
。该调用将尝试以int rgb=img.getRGB(30100)
的形式运行floodFill()
中的第一行,现在y
已超出范围,因为您的y像素仅增加到99
在floodFill()
的其余部分的其他图像边缘上也会发生类似的情况。调用floodFill()
的参数边界未正确检查
请看以下几行:
if (x < img.getWidth() && y < img.getHeight() && x > 0 && y > 0) {
img.setRGB(x, y, color);
floodFill(x, y + 1, color);
}
if(x0&&y>0){
图像设置RGB(x,y,颜色);
漫灌(x,y+1,颜色);
}
假设你的图像是100像素高和宽。每个轴上的像素编号为0-99。Sayfloodfill()。if语句被签出,因此进入下两行,现在调用floodFill(30100,[color])
。该调用将尝试按如下方式运行floodFill()
中的第一行<