java图像没有显示出来

时间:2015-06-06 23:32:48

标签: java image loops for-loop copy

我正在编写一个程序,我在其中拍摄单独的图像并将它们作为一个整体图片复制到一个模板中。但我无法弄清楚如何复制单独的图像,使它们不在彼此之上。我注意到第一张和第二张图像被复制并彼此相邻放置以形成图像,但之后其他三张图像就堆积在第二张图像的顶部。我弄清楚了 如何分开它们来完成图像。如果有人能告诉我如何解决这个问题,那就太好了。 感谢

我的代码:

import java.awt.*;
class PanoramaTester
{
    public static void main(String[] args)
    {
        Picture sourcePicture = new Picture("Mars1.jpg");
        Picture sourcePicture2 = new Picture("Mars2.jpg");
        Picture sourcePicture3 = new Picture("Mars3.jpg");
        Picture sourcePicture4 = new Picture("Mars4.jpg");
        Picture sourcePicture5 = new Picture("Mars5.jpg");
        Picture targetPicture1 = new Picture(1150,400);

        Pixel sourcePixel, targetPixel = null;
        Color sourceColor, targetColor = null;

        for(int y = 0; y < sourcePicture.getHeight(); y++)
        {
            for(int x = 0; x < sourcePicture.getWidth(); x++)
            {
                sourcePixel = sourcePicture.getPixel(x,y);
                sourceColor = sourcePixel.getColor();
                targetPixel = targetPicture1.getPixel(x,y);
                targetPixel.setColor(sourceColor);
            }
        }

        for(int y = 0; y < sourcePicture2.getHeight(); y++)
        {
            for(int x = 0; x < sourcePicture2.getWidth(); x++)
            {
                sourcePixel = sourcePicture2.getPixel(x,y);
                sourceColor = sourcePixel.getColor();
                targetPixel = targetPicture1.getPixel(x + sourcePicture.getWidth(), y);
                targetPixel.setColor(sourceColor);
            }
        }

        for(int y = 0; y < sourcePicture3.getHeight(); y++)
        {
            for(int x = 0; x < sourcePicture3.getWidth(); x++)
            {
                sourcePixel = sourcePicture3.getPixel(x,y);
                sourceColor = sourcePixel.getColor();
                targetPixel = targetPicture1.getPixel(x + sourcePicture2.getWidth(), y);
                targetPixel.setColor(sourceColor);
            }
        }

        for(int y = 0; y < sourcePicture4.getHeight(); y++)
        {
            for(int x = 0; x < sourcePicture4.getWidth(); x++)
            {
                sourcePixel = sourcePicture4.getPixel(x,y);
                sourceColor = sourcePixel.getColor();
                targetPixel = targetPicture1.getPixel(x + sourcePicture3.getWidth(), y);
                targetPixel.setColor(sourceColor);
            }
        }

        for(int y = 0; y < sourcePicture5.getHeight(); y++)
        {
        for(int x = 0; x < sourcePicture5.getWidth(); x++)
            {
                sourcePixel = sourcePicture5.getPixel(x,y);
                sourceColor = sourcePixel.getColor();
                targetPixel = targetPicture1.getPixel(x + sourcePicture4.getWidth(), y);
                targetPixel.setColor(sourceColor);
            }
        }

        targetPicture1.show();
        targetPicture1.write("FinalPanorama.jpg");
    }        
}

1 个答案:

答案 0 :(得分:1)

我认为这里的问题,从对代码的快速回顾来看,可能所有的源图片都是大约相同的大小,如果不完全相同的话。

因此,当您绘制第一张图片时,您使用左侧作为偏移量绘制它,因为您没有&#34;添加&#34;到位置的任何东西:

targetPixel = targetPicture1.getPixel(x,y);

你要添加的第二张图片&#34;图片的宽度到x位置如下:

targetPixel = targetPicture1.getPixel(x + sourcePicture.getWidth(), y);

对于第三个,你尝试做同样的事情,但你只使用第二张图片的宽度:

targetPixel = targetPicture1.getPixel(x + sourcePicture2.getWidth(), y);

如果我们假设sourcePicture2与sourcePicture的宽度大致相同,那么我们将在第二张图片上绘制第三张图片,而不是通过sourcePicture2 + sourcePicture

的宽度绘制它

e.g:

targetPixel = targetPicture1.getPixel(x + sourcePicture.getWidth() + sourcePicture2.getWidth(), y);

这样,它会推动第三张图片,使其偏移之前绘制的两张图片的宽度。