如何用它制作万花筒照片?

时间:2019-05-23 21:21:31

标签: java image graphics

对于我的任务,我需要把这张单张照片翻起来。

img Original

对此:

img wanted

我尝试过使用底片并手动将其反转,但这没有效果。

DrawingImages.java
```java
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 Color color = null;
    int height= 250;
    int width = 250;

    DrawingImages(Picture canv, Picture p1)
    {
        newCanvas = canv;
        newCanvas.setAllPixelsToAColor(Color.BLACK);
        g = newCanvas.getGraphics();
        g2 = (Graphics2D)g;  
        pic1 = p1;
    }

    public Picture drawPicture()
    {
        //Flip the image both horizontally and vertically
        g2.drawImage(image, x+(width/2), y+(height/2), -width, -height, null);
        //Flip the image horizontally
        g2.drawImage(image, x+(width/2), y-(height/2), -width, height, null);
        //Flip the image vertically
        g2.drawImage(image, x-(width/2), y+(height/2), width, -height, null); 
        return newCanvas;  

    }    
}
```
DrawingImagesTester.java
```java
import java.awt.Color;
public class DrawImagesTester
{
    public static void main(String[] args)
    {
        Picture canvas = new Picture(500, 500);

        Picture picture1 = new Picture("flower1.jpg");
        DrawingImages draw = new DrawingImages(canvas, picture1, Color.YELLOW);

        canvas = draw.drawPicture();
        canvas.show();


    }
}

1 个答案:

答案 0 :(得分:0)

您需要镜像图像。该过程实际上非常简单,是一个常用的技巧。您只需要沿着要镜像的轴在负方向上缩放图像(然后平移图像,使其重新出现在用户空间中即可)

例如...

Mirrored

BufferedImage img = ImageIO.read(new File("/Users/shanew/Downloads/kAJZbDc.jpg"));
BufferedImage mirrored = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
Graphics2D g2d = mirrored.createGraphics();
g2d.scale(-1, 1);
g2d.translate(-mirrored.getWidth(), 0);
g2d.drawImage(img, 0, 0, null);
g2d.dispose();

BufferedImage combined = new BufferedImage(img.getWidth() * 2, img.getHeight(), img.getType());
g2d = combined.createGraphics();
g2d.drawImage(img, 0, 0, null);
g2d.drawImage(mirrored, img.getWidth(), 0, null);
g2d.dispose();

JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(combined)));