java图像不显示

标签 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");
    }        
}

最佳答案

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

因此,当您绘制第一张图片时,您会使用左侧作为偏移量来绘制它,因为您不会向该位置“添加”任何内容:

targetPixel = targetPicture1.getPixel(x,y);

第二张图片将图片的宽度“添加”到 x 位置,如下所示:

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

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

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

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

例如:

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

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

关于java图像不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30688599/

相关文章:

Java运行时,在关闭应用程序时执行一些操作

image - 图片的默认过期时间

javascript - 循环遍历 JSON 数组中的每个 x 对象

java - 缩短 ImageIcon 的路径

java - 使用 rnd 值防止无限循环

Python 嵌套循环仅适用于第一遍

java - 无法在 Netbeans 7.1 中安装 GWT4NB 插件

java - 如何填写方法 public boolean checkAnswer(String response)?

Java,解密文件的问题

mysql - 将图像从数据库添加到谷歌地图信息窗口