作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于我的作业,我需要转动这张奇异的图片。
进入此:
我尝试过使用底片并手动反转它,但没有成功。
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();
}
}
最佳答案
您需要镜像图像。这个过程其实很简单,也是一个常用的技巧。您只需沿着要镜像的轴以负方向缩放图像(然后平移图像,以便它将重新出现在用户空间中)
例如...
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/
我是一名优秀的程序员,十分优秀!