java 图片水平翻转_java中实现图片的水平翻转

java中实现图片的水平翻转

找了很多资料,网上看了很多翻转,都没有我想要的,现在终于找到了,但是经过自己的加工的!

在游戏设计时会有用的哦!如将向右人物图像,用代码翻转,可以减少图片资源的使用。

不多说了,直接看代码:

public AffineTransform flip(ImageIcon icon,int x,int y) {

Image img = icon.getImage();

AffineTransform transform = new AffineTransform();

transform.scale(-1, 1);

transform.translate(-2 * img.getWidth(null) / 2,

0 * img.getHeight(null) / 2);

transform.translate((double) -x, (double) y);

return transform;

}将图片显示在屏幕上,则用Graphics2D类中的drawImage()方法。

代码:

Graphics2D g2 = (Graphics2D)g;

g2.drawImage(img,transform,this) ;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 JavaFX 实现图片 3D 翻转效果的示例代码: ```java import javafx.animation.Interpolator; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.geometry.Point3D; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.media.AudioClip; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.scene.transform.Rotate; import javafx.stage.Stage; import javafx.util.Duration; public class Image3DFlipDemo extends Application { private static final String AUDIO_URL = "http://www.music.helsinki.fi/tmt/opetus/uusmedia/esim/a2002011001-e02.wav"; private static final double IMAGE_WIDTH = 400.0; private static final double IMAGE_HEIGHT = 300.0; private ImageView imageView; private AudioClip audioClip; @Override public void start(Stage stage) throws Exception { // 创建 ImageView 并加载图片 Image image = new Image("https://picsum.photos/" + (int) IMAGE_WIDTH + "/" + (int) IMAGE_HEIGHT); imageView = new ImageView(image); imageView.setFitWidth(IMAGE_WIDTH); imageView.setFitHeight(IMAGE_HEIGHT); // 创建背景矩形 Rectangle bgRect = new Rectangle(IMAGE_WIDTH, IMAGE_HEIGHT, Color.BLACK); // 创建容器 Group 并添加 ImageView 和背景矩形 Group group = new Group(bgRect, imageView); // 创建场景 Scene 并添加容器 Group Scene scene = new Scene(group, IMAGE_WIDTH, IMAGE_HEIGHT, Color.BLACK); // 创建旋转变换 Rotate Rotate rotate = new Rotate(0, new Point3D(0, 0, 0)); // 将旋转变换 Rotate 添加到 ImageView 上 imageView.getTransforms().add(rotate); // 创建 Timeline 并添加旋转动画 Timeline timeline = new Timeline( new KeyFrame(Duration.ZERO, new KeyValue(rotate.angleProperty(), 0, Interpolator.LINEAR)), new KeyFrame(Duration.seconds(1), new KeyValue(rotate.angleProperty(), 180, Interpolator.EASE_OUT)) ); // 设置 Timeline 循环次数为 2 timeline.setCycleCount(2); // 设置 Timeline 播放完成后的回调函数 timeline.setOnFinished(event -> { // 将 ImageView 的图片翻转为镜像 imageView.setScaleX(imageView.getScaleX() * -1); // 播放音效 if (audioClip != null) { audioClip.play(); } }); // 设置场景 Scene 的鼠标点击事件 scene.setOnMouseClicked(event -> { // 如果 Timeline 正在播放,则返回 if (timeline.getStatus() == Timeline.Status.RUNNING) { return; } // 设置 Timeline 的起始值为当前旋转角度 double startAngle = rotate.getAngle(); // 创建 KeyValue 并添加到 Timeline KeyValue keyValue = new KeyValue(rotate.angleProperty(), startAngle + 180, Interpolator.EASE_IN); timeline.getKeyFrames().set(1, new KeyFrame(Duration.seconds(1), keyValue)); // 播放 Timeline timeline.play(); }); // 创建音效 AudioClip audioClip = new AudioClip(AUDIO_URL); // 显示场景 Scene stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } ``` 在上面的代码,我们创建了一个 ImageView 并加载了一张图片,然后创建了一个背景矩形和一个容器 Group,并将 ImageView 和背景矩形添加到容器 Group ,最后创建了一个场景 Scene 并将容器 Group 添加到场景 Scene 。 接着,我们创建了一个旋转变换 Rotate,并将其添加到 ImageView 上,然后创建了一个 Timeline 并添加了旋转动画,然后设置 Timeline 循环次数为 2,并设置 Timeline 播放完成后的回调函数,回调函数将 ImageView 的图片翻转为镜像,并播放了一个音效。 最后,我们设置了场景 Scene 的鼠标点击事件,当鼠标点击场景时,如果 Timeline 正在播放,则直接返回,否则设置 Timeline 的起始值为当前旋转角度,并播放 Timeline。 你可以将上面的代码复制到一个 Java并运行,然后点击场景看看图片的 3D 翻转效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值