【问题标题】:Index pixels to color certain location of pictures索引像素为图片的特定位置着色
【发布时间】:2016-03-08 19:57:01
【问题描述】:

我正在创建一个方法,该方法采用两个参数和 2 个索引,一个开始和一个结束,它采用正在编辑的图片的位置并将这些像素转换为不同的颜色。使用 while 循环来索引开始和结束。

我遇到的问题是我只有一小部分可以改变颜色:

不要介意一些被注释掉的代码。我尝试了一堆不同的东西。

public void negative(int start, int end)
{
    Pixel[] pixelArray = this.getPixels(); //pixelarray index
    Pixel pixel = null;
    // int height = this.getHeight();
    //int paintPoint = height / 2;
    //int width = this.getWidth();
    int i = 0;
    int red, green, blue = 0;
    // int x = 0;
    Pixel topPixel = null;
    Pixel bottomPixel = null;
    //int startY;
    //int startX;
    int y = start;
    int x = end;
    //int count;
    while( y < this.getHeight())
    {
        y++;
        while (x < this.getWidth()) //loops through index
        {
          pixel = this.getPixel(x,y);
          red = pixel.getRed();
          green = pixel.getGreen();//collects color green
          blue = pixel.getBlue();//collects color blue 
          Color negColor = new Color( 255 - red, 255 - green, 255 - blue);//sets new values of pixels
          pixel.setColor(negColor);

          x++;
          //count = count + 1;
          i++;//indexes continuing
        }
    }
}

【问题讨论】:

    标签: java arrays indexing while-loop pixel


    【解决方案1】:

    图片是 2D 的,但您将其视为 1D(请注意,在通过内部 x 循环后,它永远不会重置为其最小值)。如果您希望为给定照片中的任意矩形着色,则参数应包括两个点:minx、miny 和 maxx maxy,然后您的一对 2D 循环逐行访问该区域中的每个点。

    // do sanity checks on your parms 
    
    if (this.getWidth() < maxx) {
        maxx = this.getWidth();
    }
    if (this.getHeight() < maxy) {
        maxy = this.getHeight();
    }
    if (minx < 0) { 
        minx = 0;
    }
    if (miny < 0) { 
        miny = 0;
    }
    
    
    for (y = mixy; y < maxy; y++) {
    
        for (x = mixx; x < maxx; x++) {
    
            // now your have valid x and y values
        }
    }
    

    【讨论】:

    • 感谢您的意见。我不久前解决了这个问题。我不被允许使用 for 循环,我们被指示使用 while 循环。
    猜你喜欢
    • 2013-10-04
    • 2012-11-21
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    相关资源
    最近更新 更多