在背景Java中更改图像的坐标

时间:2016-07-29 23:33:36

标签: java image colors coordinates bluej

我正在研究这段代码,但我正在努力弄清楚如何在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

2 个答案:

答案 0 :(得分:1)

我建议你使用偏移量。通过这种方式,您可以决定复制图像时图像的位置。

尝试类似:

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

然后图像将开始被绘制其宽度远离屏幕右下角的高度。

答案 1 :(得分:1)

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

  

我还有另一个问题,如果你的回答会很好,我怎么会把图像切成两半,就像北半球只有地球一样?

您需要更改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;

多田!