功能实现:选择图片后,可以对图片进行移动和拉伸操作

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;public class PicTest {static class Demo {private String picPath = "F:\\迅雷下载\\1.png";private int x, y, width, height;private Image image;public Demo() throws IOException {this.image = new ImageIcon(picPath).getImage();this.width = 200;this.height = 200;this.x = 0;this.y = 0;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public Image getImage() {return image;}public void setImage(Image image) {this.image = image;}}static class TestPanle extends JPanel {private Demo demo;public TestPanle(Demo demo) {this.demo = demo;}@Overridepublic void paint(Graphics g) {super.paint(g);g.drawImage(demo.getImage(), demo.getX(), demo.getY(), demo.getWidth(), demo.getHeight(), null);}}static class TestFrame extends JFrame {//窗体拉伸属性private final static int RESIZE_WIDTH = 5;// 判定是否为调整窗口状态的范围与边界距离private int mouseX;private int mouseY;int picX;int picY;int width;int height;int cursorType;public TestFrame() throws IOException {this.setTitle("Lin");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(800, 800);Demo demo = new Demo();TestPanle panle = new TestPanle(demo);panle.addMouseMotionListener(new MouseMotionListener() {//鼠标拖动触发@Overridepublic void mouseDragged(MouseEvent e) {// 用于判断移动或拉伸if (e.getX() > demo.getX() &&e.getX() < demo.getX() + demo.getWidth() &&e.getY() > demo.getY() &&e.getY() < demo.getY() + demo.getHeight() &&// 如果光标类型没有改变就代表是移动,避免事件冲突cursorType == Cursor.DEFAULT_CURSOR) {demo.setX(picX + e.getX() - mouseX);demo.setY(picY + e.getY() - mouseY);
//                        System.out.println("============" + demo.getX() + " : " + demo.getY());} else {if (cursorType == Cursor.N_RESIZE_CURSOR) { // 向上拉伸demo.setY(picY + (e.getY() - mouseY));if (e.getY() - mouseY > 0) {demo.setHeight(height - (e.getY() - mouseY));} else {demo.setHeight(height - (e.getY() - mouseY));}} else if (cursorType == Cursor.S_RESIZE_CURSOR) { // 向下拉伸if (e.getY() - mouseY > 0) {demo.setHeight(height + (e.getY() - mouseY));} else {demo.setHeight(height + (e.getY() - mouseY));}} else if (cursorType == Cursor.W_RESIZE_CURSOR) { // 向左拉伸demo.setX(picX + (e.getX() - mouseX));if (e.getX() - mouseX > 0) {demo.setWidth(width - (e.getX() - mouseX));} else {demo.setWidth(width - (e.getX() - mouseX));}} else if (cursorType == Cursor.E_RESIZE_CURSOR) { // 向右拉伸if (e.getX() - mouseX > 0) {demo.setWidth(width + (e.getX() - mouseX));} else {demo.setWidth(width + (e.getX() - mouseX));}} else if (cursorType == Cursor.NW_RESIZE_CURSOR) { // 向左上拉伸if (e.getY() - mouseY > 0 || e.getX() - mouseX > 0) {demo.setY(picY + (e.getY() - mouseY));demo.setHeight(height - (e.getY() - mouseY));demo.setX(picX + (e.getX() - mouseX));demo.setWidth(width - (e.getX() - mouseX));} else if (e.getY() - mouseY < 0 || e.getX() - mouseX < 0) {demo.setY(picY + (e.getY() - mouseY));demo.setHeight(height - (e.getY() - mouseY));demo.setX(picX + (e.getX() - mouseX));demo.setWidth(width - (e.getX() - mouseX));}} else if (cursorType == Cursor.NE_RESIZE_CURSOR) { // 向右上拉伸if (e.getY() - mouseY > 0 || e.getX() - mouseX > 0) {demo.setY(picY + (e.getY() - mouseY));demo.setHeight(height - (e.getY() - mouseY));demo.setWidth(width + (e.getX() - mouseX));} else if (e.getY() - mouseY < 0 || e.getX() - mouseX < 0) {demo.setY(picY + (e.getY() - mouseY));demo.setHeight(height - (e.getY() - mouseY));demo.setWidth(width + (e.getX() - mouseX));}} else if (cursorType == Cursor.SW_RESIZE_CURSOR) { // 向左下拉伸if (e.getY() - mouseY > 0 || e.getX() - mouseX > 0) {demo.setHeight(height + (e.getY() - mouseY));demo.setX(picX + (e.getX() - mouseX));demo.setWidth(width - (e.getX() - mouseX));} else if (e.getY() - mouseY < 0 || e.getX() - mouseX < 0) {demo.setHeight(height + (e.getY() - mouseY));demo.setX(picX + (e.getX() - mouseX));demo.setWidth(width - (e.getX() - mouseX));}} else if (cursorType == Cursor.SE_RESIZE_CURSOR) { // 向右下拉伸if (e.getY() - mouseY > 0 || e.getX() - mouseX > 0) {demo.setHeight(height + (e.getY() - mouseY));demo.setWidth(width + (e.getX() - mouseX));} else if (e.getY() - mouseY < 0 || e.getX() - mouseX < 0) {demo.setHeight(height + (e.getY() - mouseY));demo.setWidth(width + (e.getX() - mouseX));}}}}// 鼠标移动触发@Overridepublic void mouseMoved(MouseEvent e) {int mouseX = e.getX();int mouseY = e.getY();// 设置鼠标默认类型cursorType = Cursor.DEFAULT_CURSOR;if (mouseY >= demo.getY() && mouseY <= demo.getY() + RESIZE_WIDTH &&mouseX >= demo.getX() && mouseX <= demo.getX() + RESIZE_WIDTH) { // 光标在左上角cursorType = Cursor.NW_RESIZE_CURSOR;} else if (mouseY >= demo.getY() && mouseY <= demo.getY() + RESIZE_WIDTH &&mouseX <= demo.getX() + width && mouseX >= demo.getX() + width - RESIZE_WIDTH) { // 光标在右上角cursorType = Cursor.NE_RESIZE_CURSOR;} else if (mouseY <= demo.getY() + height && mouseY >= demo.getY() + height - RESIZE_WIDTH &&mouseX >= demo.getX() && mouseX <= demo.getX() + RESIZE_WIDTH) { // 光标在左下角cursorType = Cursor.SW_RESIZE_CURSOR;} else if (mouseY <= demo.getY() + height && mouseY >= demo.getY() + height - RESIZE_WIDTH &&mouseX <= demo.getX() + width && mouseX >= demo.getX() + width - RESIZE_WIDTH) { // 光标在右下角cursorType = Cursor.SE_RESIZE_CURSOR;} else if (mouseY >= demo.getY() && mouseY <= demo.getY() + RESIZE_WIDTH &&mouseX > demo.getX() && mouseX < demo.getX() + width) { // 光标在上边界cursorType = Cursor.N_RESIZE_CURSOR;} else if (mouseY <= demo.getY() + height && mouseY >= demo.getY() + height - RESIZE_WIDTH &&mouseX > demo.getX() && mouseX < demo.getX() + width) { // 光标在下边界cursorType = Cursor.S_RESIZE_CURSOR;} else if (mouseX >= demo.getX() && mouseX <= demo.getX() + RESIZE_WIDTH &&mouseY > demo.getY() && mouseY < demo.getY() + height) { // 光标在左边界cursorType = Cursor.W_RESIZE_CURSOR;} else if (mouseX <= demo.getX() + width && mouseX >= demo.getX() + width - RESIZE_WIDTH &&mouseY > demo.getY() && mouseY < demo.getY() + height) { // 光标在右边界cursorType = Cursor.E_RESIZE_CURSOR;}// 改变光标类型panle.setCursor(Cursor.getPredefinedCursor(cursorType));}});panle.addMouseListener(new MouseListener() {// 鼠标点击监听@Overridepublic void mouseClicked(MouseEvent e) {}// 鼠标按下监听@Overridepublic void mousePressed(MouseEvent e) {mouseX = e.getX();mouseY = e.getY();picX = demo.getX();picY = demo.getY();width = demo.getWidth();height = demo.getHeight();}// 鼠标释放监听@Overridepublic void mouseReleased(MouseEvent e) {mouseX = e.getX();mouseY = e.getY();picX = demo.getX();picY = demo.getY();width = demo.getWidth();height = demo.getHeight();}// 鼠标进入监听@Overridepublic void mouseEntered(MouseEvent e) {}// 鼠标退出监听@Overridepublic void mouseExited(MouseEvent e) {}});this.add(panle);this.setVisible(true);// 利用循环不断绘制画面while (true) {panle.repaint();}}}public static void main(String[] args) throws IOException {new TestFrame();}}

运行结果如下:

在窗口中可以对图片进行移动和拉伸操作,由于拉伸方面判断逻辑比较多,比较繁杂,但整体功能实现还是比较简单。
由于只是总结用的,所以就只是简单的功能实现,直接运行就可以。。。

JavaFX图片移动和拉伸相关推荐

  1. 小程序进入页面图片渲染会拉伸闪下变形优化bug

    小程序进入页面图片渲染会拉伸闪下变形优化bug image的mode图片剪裁缩放模式用mode='widthFix'(宽度不变,高度自动变化,保持原图宽高比不变),记得要在css里也加上height: ...

  2. 聊天气泡图片的动态拉伸、适配与镜像

    聊天气泡图片的动态拉伸.适配与镜像 前情提要 创建.9.png格式的图片 从资源文件夹加载.9.png图片 从本地文件加载".9.png"图片 项目痛点 进阶探索 iOS中的方式 ...

  3. css——图片缩放,拉伸,变形的解决办法

    你的图片即将变得超级丝滑 图片为什么会拉伸变形? 怎么解决? css的object-fit属性 object-fit属性有什么用 介绍一下object-position 举个小栗子 图片为什么会拉伸变 ...

  4. App启动图片变形,拉伸

    问题: Hbuilderx开发App上传启动图片变形,拉伸 解决: 使用.9.png图,俗称(9妹图) 注:9妹图以 .9.png 为后缀 制作9妹图: 安装Android SDK 打开 androi ...

  5. 【UGUI】 全屏背景图片等比例拉伸自适应

    效果如下: 首先感叹一下,UGUI的自适应做的还是非常不错的,RectTransform中提供了非常多种的自适应方式.对做界面来说还是很友好的. 如果界面上需要有个全屏的背景图(UI界面一般都是有的吧 ...

  6. android设置背景图片不填充整个_Android应用开发之android解决背景图片平铺拉伸问题...

    本文将带你了解Android应用开发之android解决背景图片平铺拉伸问题,希望本文对大家学Android有所帮助 最近开发时遇到一个问题: 使用一条图片(1200x128)平铺作为背景,测试机型是 ...

  7. javaFX中解决填充(拉伸)问题

    1.margin设置实现 在项目过程中,遇到此问题,如图: 如果窗口缩小,HBox(左边的包含TitledPane那部分)看不到底部 如果窗口拉大,下面就出现空白,HBox高度没拉神 办法:对包含HB ...

  8. Android 保持ImageVIew大小不变,让图片按比例拉伸

    目录 结果总览 结论:保持图片宽高比总体有两种思路: 实践示例 效果图: 原理说明: 补充说明: 再次汇总 结果总览 全篇为个人理解终结,如有出入请参考官方文档 ImageVIew 按比例拉伸图片,前 ...

  9. html 如何完美的显示图片,不拉伸图片,完整显示等等。

    效果图: 代码: <!DOCTYPE html> <html lang="en"> <head><meta charset="U ...

最新文章

  1. 文凭-决定的人生成败?下
  2. Python Module_Socket_网络编程
  3. 微信小程序——获取openGid
  4. DHTML【2】--HTML
  5. js调用c语言程序设计,HTML页面,测试JS对C函数的调用简单实例
  6. java构建protobuf中的map,Protobuf对象作为Maps中的键
  7. pytorch 音频分类_Pytorch中音频的神经风格转换
  8. hdu 3572 Task Schedule 网络流
  9. Vue之x-template(1)
  10. React 生命周期
  11. 使用Eclipse插件DB viewer进行MySQL(SQL Server)等 数据库操作
  12. 直播评论发弹幕切图功能点集合
  13. HDU4282 A very hard mathematic problem 快速幂
  14. 呼叫中心点击拨打接口升级代码
  15. 罗杨美慧 20190912-1 每周例行报告
  16. VueJS学习资料大全
  17. 微信、支付宝支付绑定多个商户号
  18. 服务器上建个新文件夹怎么共享打印机,新服务器怎样设置共享打印机
  19. 韦尔奇:企业经营的10个锦囊
  20. 计算机科学大师唐纳德,计算机科学大师唐纳德.克努特指出,杨辉三角

热门文章

  1. java语言动画模拟_java swing动画模拟太阳系行星运行动画特效
  2. 面试试题总结——欢迎前来补卡
  3. c语言数据结构算法设计题,数据结构题集(C语言版)算法设计题答案[].doc
  4. 微信小程序购物车组件
  5. python吴枫千寻的_Python基础语法习题参考(0-9关)
  6. 从H265文件读取nalu
  7. 浏览器连接不上网络但是qq等服务能联网的解决方法
  8. cbow和skip-gram实现关键代码解析
  9. 安装无线监控需要服务器么,无线 监控系统安装 方案
  10. 【测试经验向】提测质量差 + 测试工期压缩,我要怎么办?