我该如何解决这个问题;坐标超出范围”;Java代码中的错误?

我该如何解决这个问题;坐标超出范围”;Java代码中的错误?,java,coordinates,bounds,Java,Coordinates,Bounds,下面是我的代码的样子。我收到一个错误,上面写着“线程中的异常”main“java.lang.arrayIndexOutofBoundsException:坐标超出范围!” 我不知道这意味着什么,也不知道如何修复它,因此非常感谢您的帮助 import java.awt.Color; public class Assignment9 { /** * @param args * @return */ public static void remove

下面是我的代码的样子。我收到一个错误,上面写着“线程中的异常”main“java.lang.arrayIndexOutofBoundsException:坐标超出范围!” 我不知道这意味着什么,也不知道如何修复它,因此非常感谢您的帮助

import java.awt.Color;

public class Assignment9 {

    /**
     * @param args
     * @return
     */

    public static void removeBlue(Picture pic){
        Color cPic = pic.get(100,100);
        //remove blue color pane from image, set blue weight to 0
        int h = pic.height();
        int w = pic.width();
        System.out.println(cPic);
        //^this shows the red, green, and blue weights
        int b = cPic.getBlue();
        int r = cPic.getRed();
        int g = cPic.getGreen();
        System.out.println("r=" +r +"g="+g+"b="+b);
        pic.setColor(w, h, r, g, 0);   
        for(int x=0; x<w ; x++){
                //need to set color
                pic.setColor(w, h, r, g, 0);}

    }
    public static void drawredStripe(Picture pic){
    //draw a red vertical stripe that is 200 pixels wide through the middle of the image
        Color cPic = pic.get(100,100);
        int h = pic.height();
        int w = pic.width();
        int b = cPic.getBlue();
        int r = cPic.getRed();
        int g = cPic.getGreen();
        for(int x=0; x<h ; x++){
            for (int y = (w/2)-100; y <(w/2)+100; y++){
                //need to set color
                pic.setColor(x, y, r, 0, 0);
            }
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Picture dolphin= new Picture("dolphin_swimming.jpg");
        removeBlue(dolphin);   
        dolphin.show();
        drawredStripe(dolphin);
        dolphin.show(); 
}
}
导入java.awt.Color;
公共课堂作业9{
/**
*@param args
*@返回
*/
公共静态空洞清除蓝色(图片pic){
颜色cPic=pic.get(100100);
//从图像中删除蓝色窗格,将蓝色权重设置为0
int h=图片高度();
int w=pic.width();
系统输出打印项次(cPic);
//^这将显示红色、绿色和蓝色权重
intb=cPic.getBlue();
int r=cPic.getRed();
int g=cPic.getGreen();
系统输出打印项次(“r=“+r+”g=“+g+”b=“+b”);
图片:设置颜色(w,h,r,g,0);

对于(intx=0;x我猜它是

pic.setColor(w, h, r, g, 0);
您正在对x进行迭代,但在removeBlue的for循环中没有使用x


坐标(w,h)可能超出范围,这是错误在调用
setColor
时声称的,
x
y
值(方法调用的前两个参数)是坐标。这些坐标必须位于试图修改的
图片设置的范围内:

  • 如果
    图片的宽度
    w
    ,则
    x
    坐标的边界为
    [0…w-1]

  • 如果
    图片的高度
    h
    ,则
    y
    坐标的边界为
    [0…h-1]

“坐标超出范围”消息表示坐标(即
x
y
值)不在各自的范围内

例如,您正在执行以下操作:

    int h = pic.height();
    int w = pic.width();
    // ...
    pic.setColor(w, h, r, g, 0);
X轴上的边界为
0
w-1
,但您提供的
X
值为
w
。Y轴上的边界为
0
h-1
,但您提供的
Y
值为
h
。(代码中还有其他地方会出现这种错误和类似错误。)


我不知道这意味着什么,也不知道如何修复它,因此非常感谢您的帮助

import java.awt.Color;

public class Assignment9 {

    /**
     * @param args
     * @return
     */

    public static void removeBlue(Picture pic){
        Color cPic = pic.get(100,100);
        //remove blue color pane from image, set blue weight to 0
        int h = pic.height();
        int w = pic.width();
        System.out.println(cPic);
        //^this shows the red, green, and blue weights
        int b = cPic.getBlue();
        int r = cPic.getRed();
        int g = cPic.getGreen();
        System.out.println("r=" +r +"g="+g+"b="+b);
        pic.setColor(w, h, r, g, 0);   
        for(int x=0; x<w ; x++){
                //need to set color
                pic.setColor(w, h, r, g, 0);}

    }
    public static void drawredStripe(Picture pic){
    //draw a red vertical stripe that is 200 pixels wide through the middle of the image
        Color cPic = pic.get(100,100);
        int h = pic.height();
        int w = pic.width();
        int b = cPic.getBlue();
        int r = cPic.getRed();
        int g = cPic.getGreen();
        for(int x=0; x<h ; x++){
            for (int y = (w/2)-100; y <(w/2)+100; y++){
                //need to set color
                pic.setColor(x, y, r, 0, 0);
            }
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Picture dolphin= new Picture("dolphin_swimming.jpg");
        removeBlue(dolphin);   
        dolphin.show();
        drawredStripe(dolphin);
        dolphin.show(); 
}
}
  • 阅读并理解以上解释
  • 查看调用
    setColor
    的位置,分析使用的
    x
    y
    值是否在范围内
  • 如有必要,请更改代码
  • 测试…并根据需要重复前面的步骤

  • 错误来自哪一行?w又是什么?如果w<200,则y负值可能会出现问题。此外,您可以在代码中交换h和w。在
    drawredstripe
    中,您使用
    x
    作为垂直坐标,使用
    y
    作为水平坐标。我认为这不正确。