更改背景中图像的坐标 Java

Change an image's coordinates across a background Java

我正在编写这段代码,但我正在努力弄清楚如何在 targetPicture1(背景)中更改图像的坐标,在本例中为地球图片。

import java.awt.*;
public class CopyCatDemo
{
 public static void main(String[] args)
 {
    Picture sourcePicture = new Picture("earth.jpg");
    System.out.println("Width: " + sourcePicture.getWidth());
    System.out.println("Height: " + sourcePicture.getHeight());
    Picture targetPicture1 = new Picture(400,400);
    targetPicture1.setAllPixelsToAColor(Color.BLACK);

    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);         
        }
    }

    sourcePicture.show();
    targetPicture1.show();
    targetPicture1.write("NewFile.jpg");
 }//end of main method
}//end of class

我建议您使用偏移量。这样您就可以在复制图像时指定图像的放置位置。

试试这样的东西:

import java.awt.*;
public class CopyCatDemo
{
 public static void main(String[] args)
 {
    Picture sourcePicture = new Picture("earth.jpg");
    System.out.println("Width: " + sourcePicture.getWidth());
    System.out.println("Height: " + sourcePicture.getHeight());
    Picture targetPicture1 = new Picture(400,400);
    targetPicture1.setAllPixelsToAColor(Color.BLACK);

    int offsetX = 0;
    int offsetY = 0;

    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(offsetX + x, offsetY + y);
            targetPixel.setColor(sourceColor);         
        }
    }

    sourcePicture.show();
    targetPicture1.show();
    targetPicture1.write("NewFile.jpg");
 }//end of main method
}//end of class

然后假设您希望图像位于屏幕的右下角。只需相应地设置偏移量:

int offsetX = 400 - sourcePicture.getWidth();
int offsetY = 400 - sourcePicture.getHeight();

然后将开始绘制图像,其宽度距离屏幕右下角一个高度。

回答@Ben Rocco 的第二个问题:

I have another question that would be great if you answered, how would I cut the image in half, like have only the northern hemisphere show for the earth?

您需要更改 java 循环的起点。让我们只打印照片的下半部分:

import java.awt.*;
public class CopyCatDemo
{
 public static void main(String[] args)
 {
    Picture sourcePicture = new Picture("earth.jpg");
    System.out.println("Width: " + sourcePicture.getWidth());
    System.out.println("Height: " + sourcePicture.getHeight());
    Picture targetPicture1 = new Picture(400,400);
    targetPicture1.setAllPixelsToAColor(Color.BLACK);

    int offsetX = 0;
    int offsetY = 0;

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

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

    sourcePicture.show();
    targetPicture1.show();
    targetPicture1.write("NewFile.jpg");
 }//end of main method
}//end of class

然后我们需要更改 Y 的偏移量:

int offsetX = 400 - sourcePicture.getWidth();
int offsetY = 400 - sourcePicture.getHeight()/2;

多田!