2016-07-29 47 views
-1

我正在研究此代碼,但我正在努力弄清楚如何在targetPicture1(背景)中更改圖像的座標(在這種情況下爲地球圖片) 。在背景中更改圖像的座標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); 

    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 
+0

首先導入可變大小的圖像並嘗試將其複製到設置大小的圖像中。這很可能會導致出界異常。你也試圖改變圖像座標是什麼意思? – Dallen

+0

我想要在整個屏幕上移動圖像。而不是在屏幕的左上角,我想將它移動到左下角或右上角等。 –

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

然後,圖像將開始被繪製其寬度離屏幕右下角一個高度。

+0

當我嘗試它時,它不適合我。它仍然卡在屏幕的左上角。 –

+0

立即開始工作。我只是複製並粘貼你的代碼。 –

+0

@BenRocco當時我在這裏有一點被皇室迷惑了。 – Dallen

1

要回答@Ben羅科的第二個問題:

我還有一個疑問是,如果你的回答很棒,我怎麼會削減圖像中的一半,像只北半球秀地球?

你需要改變你的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; 

田田!

+0

這樣做很有道理,我覺得很愚蠢。 –

+0

好吧,我們都有我們的時刻。 – Dallen