1

我有一个要在 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;  
}

}

4

1 回答 1

2

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

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

您还可以查看AffineTransform.rotate() - 我如何同时进行 xlate、旋转和缩放?它使用这种技术

于 2012-09-14T22:58:20.040 回答