如何使用Java.awt镜像映像

时间:2012-09-14 22:30:23

标签: java image awt graphics2d

我有一个用Java完成的任务,我无法想象我的生活。我应该使用Graphics2D和Java.AWT。在x轴和y轴上镜像图像。

目前的代码是:

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

}

1 个答案:

答案 0 :(得分:2)

图片来源于什么? Image?如果是这样,您可以使用Image.scale(-1, -1)直接镜像图像。

如果没有,您可以直接在AffineTransform.getScaleInstance(-1, -1)上下文中使用Graphics,但您必须翻译图片的位置

您还可以查看使用此技术的AffineTransform.rotate() - how do I xlate, rotate, and scale at the same time?