我有一項任務需要在Java中完成,而且我無法弄清楚這是否適合我的生活。我應該使用Graphics2D和Java.AWT。在x軸和y軸上鏡像圖像。如何使用Java.awt鏡像圖像
當前的代碼:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
public class DrawingImages
{
private Picture newCanvas = null;
private Graphics g = null;
private Graphics2D g2 = null;
private Picture pic1 = null;
private Picture pic2 = null;
private Color color = null;
private Pixel sourcePixel, targetPixel = null;
private Color sourceColor, targetColor = null;
DrawingImages(Picture canv, Color col)
{
Picture sourcePicture = new Picture("WashingtonMonument.jpg");
newCanvas = canv;
newCanvas.setAllPixelsToAColor(Color.BLACK);
for(int y = sourcePicture.getHeight()-1; y >0; y=y-1)
{
for(int x = sourcePicture.getWidth() - 1; x > 0; x = x - 1)
{
sourcePixel = sourcePicture.getPixel(x,y);
sourceColor = sourcePixel.getColor();
targetPixel = newCanvas.getPixel(x+sourcePicture.getWidth() -1,y+sourcePicture.getHeight()- 1);
targetPixel.setColor(sourceColor);
}
}
g = newCanvas.getGraphics();
g2 = (Graphics2D)g;
}
}
究竟是什麼問題,此圖像的位置你有你現在的代碼嗎?你能正確讀取源圖片嗎?你可以創建一個目標圖片,但鏡像是錯誤的? – Nick
它不創建鏡像。它正確讀取源圖像,並將其放在畫布上,但不是鏡像方式 –
我不想給出答案。但假設您的源圖片在x = 4,y = 2時有一個像素。目標圖片中對應的像素是什麼? (你需要知道圖片的寬度和高度。)你能從這個特定的案例中找出一個通用公式嗎? – Nick