1

我正在尝试学习如何水平滚动图像。假设用户输入一个整数数量,返回的图像将是一个向左移动该数量的新图像,最左边的像素将出现在图像的右侧。我一直在接近解决这个问题,但遇到了超出范围的错误。

有任何想法吗?我对图像处理和处理很陌生。

这是我到目前为止所写的。

public void scrollHorizontal(int numpixels){
    horizontalscroll = new Picture(image6.getWidth(), image6.getHeight());

    for (int i = 0; i<image6.getWidth(); i++){
        for (int j=0; j<image6.getHeight(); j++){
            pix2 = image6.getPixel(i, j);
            horizontalscroll.setPixel(i-numpixels, j, pix2);
            System.out.println(i);
        }
    }
    for (int i = 0; i< image6.getWidth(); i++){
        for (int j = 0; j<image6.getHeight(); j++){
            pix2 = horizontalscroll.getPixel(i, j);
            horizontalscroll.setPixel(image6.getWidth()-numpixels-i ,j, pix2);
            //System.out.println(i);
        }

    }
    horizontalscroll.store("scrollhorizontal.gif");
}

Picture 类只是根据 (x, y) 创建一个图片对象。getPixel() 是 Picture 类的一个方法。“pix2”是一个像素对象。

4

2 回答 2

0

I ended up figuring it out after some more trial and error. Thanks. Just thought I'd share!

public void scrollHorizontal(int numpixels){ //allows the user to define numpixels to scroll horizontally by.
    int height = image6.getHeight();        //creates a new image that is scrolled horizontally by the entered numpixels amount.
    int width = image6.getWidth();
    horizontalscroll = new Picture(image6.getWidth(), image6.getHeight());
    for (int i = numpixels; i < width; i++){
        for (int j = 0; j < height; j++){
            pix2 = image6.getPixel(i, j);
            horizontalscroll.setPixel(i-numpixels, j, pix2);
        }
    }
    for (int i = 0; i < numpixels; i++){
        for (int j = 0; j < height; j++){
            pix2 = image6.getPixel(i, j);
            horizontalscroll.setPixel((width-numpixels) + i, j, pix2);
        }
    }
    horizontalscroll.store("scrollhorizontal.gif"); //stores new picture object to a new file.
}
于 2013-10-23T05:44:06.100 回答
0

您的错误位于这样的行中:

horizontalscroll.setPixel(i-numpixels, j, pix2);

当 i 小于 numPixels 时,您试图在负坐标处设置值。

一个正确的解决方案是:

public void scrollHorizontal(int numpixels){
  int wid = image6.getWidth();
  int hei = image6.getHeight();
  if (wid < Math.abs(numPixels)) {
    numPixels = numPixels % wid;
  }
  horizontalscroll = new Picture();
  if (numpixels >= 0) {
    for (int i = 0; i< hei; i++){
      for (int j = 0, h = wid - numPixels - 1; j < numPixels; ++j, ++h) {
        pix2 = image6.getPixel(i, h);
        horizontalscroll.setPixel(i, j, pix2);
      }
      for (int j = numPixels, h = 0; j< wid; ++j, ++h){
        pix2 = image6.getPixel(i, h);
        horizontalscroll.setPixel(i, j, pix2);
      }
    }
  } else {
    for (int i = 0; i< hei; i++){
      for (int j = 0, h = numPixels; j< wid - numPixels - 1; ++j, ++h) {
        pix2 = image6.getPixel(i, h);
        horizontalscroll.setPixel(i, j, pix2);
      }
      for (int j = wid - numPixels, h = 0; j < wid; ++j, ++h) {
        pix2 = image6.getPixel(i, h);
        horizontalscroll.setPixel(i, j, pix2);
      }
    }
  }

}

于 2013-10-23T00:47:26.300 回答