gpt4 book ai didi

java - 如何用它创建一张万花筒照片?

转载 作者:行者123 更新时间:2023-12-02 05:17:57 27 4
gpt4 key购买 nike

对于我的作业,我需要转动这张奇异的图片。

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


}
}

最佳答案

您需要镜像图像。这个过程其实很简单,也是一个常用的技巧。您只需沿着要镜像的轴以负方向缩放图像(然后平移图像,以便它将重新出现在用户空间中)

例如...

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

关于java - 如何用它创建一张万花筒照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56283074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com