当前位置: 首页 > 编程日记 > 正文

JAVA对图片的任意角度旋转,以及镜像操作

package relevantTest;
/*
 * 该代码实现了对图像的水平镜像变换,垂直镜像变换,任意角度旋转,jtf的实时监控,以及对图像的缩放变换,以及按钮的若隐若现效果。
 * 在对图像进行任意角度旋转时最好是在原始图片未进行任何操作时进行,否则由于坐标的改变,图像可能显示在了屏幕之外
 * 请使用鼠标右键弹出菜单进行功能的实现
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class ImgsOperation {
 protected static final int ICONIFIED = 1;
 static Image img=(new ImageIcon("images/王梓盈.jpg")).getImage(); 
 static Image  close=(new ImageIcon("images/close00.png")).getImage();
 static Image  turn=(new ImageIcon("images/turn.png")).getImage();
 static Image min=(new ImageIcon("images/min00.jpg")).getImage();
 static int  imgWidth=img.getWidth(null);
 static int imgHeight=img.getHeight(null);
 static int ScreenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;
 static int ScreenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;
 static int xCoor=(ScreenWidth-imgWidth)/2;
 static int yCoor=(ScreenHeight-imgHeight)/2;
 static int xCoor0=0,yCoor0=0;
 static int count=1;//记录点击的次数
 static int count0=1;
 String Str="";
 String Str0;
static double degree=0;//旋转的角度,都是顺时针旋转
static int xCoor1=ScreenWidth/2,yCoor1=ScreenHeight/2;//旋转中心坐标值
 public ImgsOperation(){
  final JFrame frame=new JFrame();
 final Pics0Panel jp=new Pics0Panel();//显示图片的面板
  jp.setBackground(new Color(51,51,51));
  jp.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
 final JPanel jp0=new JPanel();
  jp0.setPreferredSize(new Dimension(65,20));
  jp0.setBackground(new Color(51,51,51));
  jp0.setLayout(new FlowLayout(FlowLayout.RIGHT,1,0));
 final ClosePanel cjp=new ClosePanel();
  cjp.setPreferredSize(new Dimension(36,18));
  cjp.setVisible(false);
 final MinPanel mjp=new MinPanel();
  mjp.setPreferredSize(new Dimension(27,18));
  mjp.setVisible(false);
  JPopupMenu jpm=new JPopupMenu();
  JMenuItem turn0=new JMenuItem("水平镜像变换");
  JMenuItem turn1=new JMenuItem("垂直镜像变换");
  //JMenuItem turn2=new JMenuItem("中心对称变换");
  JMenuItem turn=new JMenuItem("自定义角度旋转");
 final JTextField jtf=new JTextField(3);
  jtf.setPreferredSize(new Dimension(30,21));
  TurnPanel tjp=new TurnPanel();
  tjp.setPreferredSize(new Dimension(112,21));
  tjp.setBackground(new Color(213,233,255));
  tjp.setLayout(new FlowLayout(FlowLayout.RIGHT,0,0));
  tjp.add(jtf);  
  turn0.setForeground(new Color(204,204,204));
  turn1.setForeground(new Color(204,204,204)); 
  turn.setForeground(new Color(204,204,204));
  turn0.setBackground(new Color(51,51,51));
  turn1.setBackground(new Color(51,51,51)); 
  turn.setBackground(new Color(51,51,51));
  jpm.setBackground(new Color(51,51,51));
  jpm.add(turn0);
  jpm.add(turn1); 
  jpm.addSeparator();
  jpm.add(turn);
  jpm.add(tjp);
  jp.setComponentPopupMenu(jpm);
  jp0.add(mjp);
  jp0.add(cjp);
  jp.add(jp0);
  frame.add(jp);
  frame.setUndecorated(true);
  frame.setSize(ScreenWidth,ScreenHeight);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);
 //若隐若现,控制按钮的显示方式
  jp0.addMouseListener(new MouseAdapter(){
   public void mouseEntered(MouseEvent e){    
    cjp.setVisible(true);
    mjp.setVisible(true);
   }
   public void mouseExited(MouseEvent e){
    cjp.setVisible(false);
    mjp.setVisible(false);
   }
  });
  //关闭窗口代码
  cjp.addMouseListener(new MouseAdapter(){
   public void mouseClicked(MouseEvent e){
    System.exit(0);
   }
   public void mouseEntered(MouseEvent e){
    cjp.setVisible(true);
    mjp.setVisible(true);
   }
   public void mouseExited(MouseEvent e){
    cjp.setVisible(false);
    mjp.setVisible(false);
   }
  });
  //最小化窗口代码
  mjp.addMouseListener(new MouseAdapter(){
   public void mouseClicked(MouseEvent e){
    frame.setExtendedState(ICONIFIED);
   }
   public void mouseEntered(MouseEvent e){
    mjp.setVisible(true); 
    cjp.setVisible(true);
   }
   public void mouseExited(MouseEvent e){
    mjp.setVisible(false);
    cjp.setVisible(false);
   }
  });
 //水平翻转代码
  turn0.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
        count+=1; 
        if(count%2==0){
     xCoor0=img.getWidth(null);
     imgWidth*=-1;
     jp.repaint();
     }
        else{
         xCoor0=0;
         imgWidth=img.getWidth(null);
         jp.repaint();
        }
    
    
   }
  });
  //垂直翻转代码
  turn1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    count0+=1;
    if(count0%2==0){    
     yCoor0=img.getHeight(null);
     imgHeight*=-1;
     jp.repaint();
     }else{
      yCoor0=0;
      imgHeight=img.getHeight(null);
      jp.repaint();
     }
    
   }
  });
  //自定义角度旋转,jtf的监听代码,如何实现数据一输入,就产生相关变化,对jtf的内容变化进行监听,达到实时监控的效果
  jtf.addCaretListener(new CaretListener(){   
   public void caretUpdate(CaretEvent arg0) {
    Str=jtf.getText();
    if(Str==""||Str.length()==0){
     degree=0;
     jp.repaint();     
    }else{
     degree=(Double.parseDouble(Str))/180.0*3.1415926;
     jp.repaint();
    }    
   }
   
  });
  
 }
 //缩放效果的代码,使用Java2D的Graphics2D的g.scale(m,n)进行缩放,如果宽=m*原始宽度,高=原始高度*n,大于0放大,小于0,缩小
 static class Pics0Panel extends JPanel{  
  private static final long serialVersionUID = 1L;
  protected void paintComponent(Graphics g){
   super.paintComponent(g);
   Graphics2D g0=(Graphics2D)g;
   g0.translate(xCoor0,yCoor0);//平移,xCoor0>0则向正方向平移xCoor0个单位,同理yCoor0>0,则向y轴正方向平移yCoor0个单位
   g0.rotate(degree,xCoor1,yCoor1);//以坐标xCoor1,yCoor1为中心顺时针旋转degree弧度
   g0.drawImage(img,xCoor,yCoor,imgWidth,imgHeight,this);
   
  }
 }
 //关闭按钮面板
 static class ClosePanel extends JPanel{  
  private static final long serialVersionUID = 1L;
  protected void paintComponent(Graphics g){
   super.paintComponent(g);   
   g.drawImage(close,0,0,36,18,this);
  }
 }
 
 //最小化按钮面板
 static class MinPanel extends JPanel{  
  private static final long serialVersionUID = 1L;
  protected void paintComponent(Graphics g){
   super.paintComponent(g);
   g.drawImage(min,0,0,27,18,this);
  }
 }
 //输入旋转角度面板
 static class TurnPanel extends JPanel{  
  private static final long serialVersionUID = 1L;
  protected void paintComponent(Graphics g){
   super.paintComponent(g);
   g.drawImage(turn,0,0,26,21,this);
   g.drawString("旋转角度",30,15);
  }
 }
 public static void main(String[] args){
  new ImgsOperation();
 }

}
/*

相关文章:

【组队学习】【34期】百度飞桨AI达人创造营

百度飞桨AI达人创造营 航路开辟者:百度飞桨领航员:六一航海士:阿水、颜鑫、宋泽山、刘洋、张文恺 基本信息 内容属性:合作课程练习平台:https://aistudio.baidu.com/aistudio/course/introduce/25259?ad-frompdg-1…

安装Python第三方库的三个方法

方法一: (cmd命令行) pip 方法【主要方法&#xff0c;适用于99%的情况】【依赖网络状况】 在命令行输入pip -h 可查看该命令帮助信息 常用pip命令 ① pip install <第三方库名> 安装指定第三方库 参数 -U :update对已经安装的进行版本更新 ② pip uninstall <第三方…

java 基础---继承

继承 一&#xff0c;概述 a) 使用extends关键字可以让一个类继承另一个类&#xff0c;继承的类为子类&#xff0c;被继承的类是父类&#xff0c;子类会自动继承父类的所有方法和属性。 b) 继承使得类和类之间产生了关系 c) 子类可以使用super调用父类成员…

建立CentOS 6.9 的Yum本地源

1、建立一个本地Yum的软件仓库1mkdir /media/cdrom2、把CentOS6.9光盘装载到/media/cdrom1mount /dev/cdrom /media/cdrom3、安装createrepo1 rpm -ivh /media/cdrom/Packages/createrepo-[按tab键] deltarpm-[按tab键] python-deltarpm-[按tab键] createrepo-0.9.9-26.…

【组队学习】【34期】零基础学python编程思维

零基础学python编程思维 航路开辟者&#xff1a;邓林权领航员&#xff1a;沈一航海士&#xff1a;覃嘉俊、马子阳、左凯文 基本信息 开源内容&#xff1a;https://linklearner.com/datawhale-homepage/index.html#/learn/detail/6内容属性&#xff1a;公测课程内容说明&…

Python wordcloud库使用说明

1. 介绍 wordcloud是优秀的词云展示第三方库 -词云以词语为基本单位&#xff0c;更加直观和艺术地展示文本 通过词云&#xff0c;我们可以快速提取大段文本的重要信息 2. 安装 (cmd命令行) pip install wordcloud 3. 使用 w wordcloud.WordCloud()代表一个文本对应的词云…

resin-pro-4.0.34 服務器在windows环境下的配置

resin-pro-4.0.34 服務器在windows环境下的配置(轉載请注明作者:icelong) 到caucho網站上http://www.caucho.com/download/下載resin-pro-4.0.34 Windows下載zip版&#xff0c;Linux下載tgz版 Install JDK 1.4 or later. On Unix, set the JAVA_HOME variable or link /usr/jav…

【组队学习】【34期】Python(一级)

Python&#xff08;一级&#xff09; 航路开辟者&#xff1a;王思齐、马燕鹏领航员&#xff1a;马燕鹏航海士&#xff1a;马燕鹏 基本信息 开源内容&#xff1a;https://github.com/datawhalechina/team-learning-program/tree/master/PythonTest内容属性&#xff1a;公测课…

matlab处理txt文件数据

read_txtfile.,m clear close all clc %load函数一般将用来导入纯数字的文件&#xff0c;可以是文本格式的文件或者是matlab保存的mat格式的文件 positionload(坐标点.txt); %将.txt数据读入到matlab工作空间[m,n]size(position); %获得数据矩阵的大小 j1; sumx0; sumy0; …

Python os库的使用

1. 基本介绍 os提供通用的、基本的操作系统交互功能 os库是Python的标准库&#xff0c;提供几百个处理函数 常用有路径操作、进程管理、环境参数等几类 路径操作&#xff1a;os.path子库&#xff0c;处理文件路径及信息 进程管理&#xff1a;启动系统中其他程序 环境参数&…

(U3D)Time的使用

Time类包含了一个重要的类变量deltaTime&#xff0c;它表示距上一次调用Update或FixedUpdate所用的时间。 因此通过它可以让游戏对象按照一个常速进行旋转&#xff0c;而不是依赖于它的帧频&#xff1a; function Update() { tranform.Rotate(0, 5 * Time.deltaTime, 0); } …

【组队学习】【34期】Scratch(二级)

Scratch&#xff08;二级&#xff09; 航路开辟者&#xff1a;王思齐、马燕鹏领航员&#xff1a;马燕鹏航海士&#xff1a;马燕鹏 基本信息 开源内容&#xff1a;https://github.com/datawhalechina/team-learning-program/tree/master/Scratch内容属性&#xff1a;公测课程…

文件操作示例脚本 tcl

linux 下&#xff0c;经常会对用到文件操作&#xff0c;下面是一个用 tcl 写的文件操作示例脚本&#xff1a; 其中 set f01 [open "fix.tcl" w] 命令表示 打开或者新建一个文件“fix.tcl”&#xff0c;并将其 file ID 设置为 f01&#xff0c;后续就以这个 file ID 来…

Python 第三方库自动安装脚本

需求&#xff1a;批量安装第三方库需要人工干预&#xff0c;能否自动安装&#xff1f; 现假设我们要安装以下库 #BatchInstall.py import os libs {"numpy","matplotlib","pillow","sklearn","requests",\ "jie…

ios 如何改变UISegmentedControl文本的字体大小?

UIFont *Boldfont [UIFont boldSystemFontOfSize:16.0f]; NSDictionary *attributes [NSDictionary dictionaryWithObject:Boldfont forKey:UITextAttributeFont]; [segment setTitleTextAttributes:attributes forState:UIControlStateNormal]; 转载于:https://www.cnblog…

CDN全站加速助力企业云上升级

[2018云栖大会南京分会飞天技术汇专场&#xff0c;阿里巴巴高级技术专家魏晋带来题CDN全站加速助力企业云上升级的演讲。主要内容是结合实际客观案例详细解读全战加速产品如何对动静态业务进行的加速&#xff0c;结合安全WAF等其他运营产品&#xff0c;对如何构建适合大部分业务…

【组队学习】一月微信图文索引

一月微信图文索引 一、组队学习相关 周报&#xff1a; 【新周报&#xff08;049&#xff09;】Datawhale组队学习Datawhale组队学习周报&#xff08;第048周&#xff09;Datawhale组队学习周报&#xff08;第047周&#xff09;Datawhale组队学习周报&#xff08;第046周&…

CSP 2019-09-1 小明种苹果 Python实现+详解

试题 代码 # N,M分别表示树的棵树和疏果轮数 N,M [int(i) for i in input().split()]leftTrees 0#最后所有树树上的苹果总数 reduceMaxIndex 0#疏果个数最多的苹果编号 reduceMaxSum 0#上面那棵树的个数reduceMaxSum 0 reduceMaxIndex 0 leftTrees 0 for i in range(N)…

Easyui 让Window弹出居中与最大化后居中

easyui1.3.2版本,window的弹出不会居中了。而dialog是会居中的,我们必须为为window的open事件做扩展 代码如下&#xff1a;只要加入以下代码即可.如果你是看了MVC项目系列的&#xff0c;把他放到jquery.easyui.plus.js里面就可以了 //让window居中 var easyuiPanelOnOpen func…

电子学会青少年编程等级考试Python一级题目解析11

Python一级题目解析 题目 &#xff08;1&#xff09;下列哪个不是Python的保留字&#xff1f;&#xff08; &#xff09;【2020.06】 A. ifB. orC. doD. for &#xff08;2&#xff09;下面哪一个不是Python的保留字&#xff1f;&#xff08; &#xff09;【2021.06】 A. …

单例和多例的区别

为什么80%的码农都做不了架构师&#xff1f;>>> 需要明白以下几个问题&#xff1a; 1. 什么是单例多例&#xff1b;2. 如何产生单例多例&#xff1b;3. 为什么要用单例多例4. 什么时候用单例&#xff0c;什么时候用多例&#xff1b; 1. 什么是单例多例: 单例&#…

CSP 201909-2 小明种苹果(续)Python实现+详解

试题 代码 # N表示苹果树的棵树 N int(input()) T, D, E 0, 0, 0 # 所有树剩果总数 # 发生苹果掉落的苹果树总数 # 相邻三棵树发生苹果掉落的组数 treeMatrix [] # 存放所有数的数果疏果信息 for i in range(N):treeRow list(map(int, input().split()))treeRow treeRow…

hdu1176

hdu1176 f(x,t)max{ f(x-1,t-1) , f(x,t-1) , f(x1,t-1) }; left(5-t)>0?(5-t):0; right(5t)<10?(5t):10; for left to right <-- i resmax{f(maxt,i)}; #include <stdio.h> #include <string.h> #define mmax(x,y,z) (x)>((y)>(z)?(y):(z))?(…

spring体系

2019独角兽企业重金招聘Python工程师标准>>> 一、spring从体系架构上分为 1. 基本组件框架组合&#xff1a;Spring Framework 2.专项领域应用组件&#xff1a;Spring Portfolio 这个Spring Portfolio主要应用满足于特定的的应用场合&#xff1a;包括了spring Securi…

电子学会青少年编程等级考试四级题目解析07

一、题目&#xff1a;数字反转 Jaime想去城堡探险&#xff0c;在城堡门口遇到了小猫&#xff0c;Jaime必须答对小猫提出的问题才能进入城堡。 小猫出题啦&#xff1a;找到一个四位数&#xff0c;该四位数的各位数字翻转&#xff08;个位变千位&#xff0c;十位变百位&#xf…

如何连接本地mysql+设置无密码登录

如果嫌麻烦&#xff0c;可以设置密码为空&#xff0c;下次登录就不需要密码了。

【GDI+】 线段 文字 定位的问题(二)

继续&#xff1a; 经过上文的分析&#xff0c;似乎可以得到类似这样的想法&#xff1a; 由此 分为左右两侧进行区分绘制&#xff0c;应该就可以获得想要的结果了~ 转载于:https://www.cnblogs.com/loveclumsybaby/p/3440314.html

电子学会青少年编程等级考试Python一级题目解析10

Python一级题目解析 1、题目 下面代码的执行结果是&#xff08;&#xff09; a 123456789 b "*" print("{0:{2}>{1},}\n{0:{2}^{1},}\n{0:{2}<{1},}".format(a,20,b))A. *********123,456,789 ****123,456,789***** 123,456,789*********B. **…

ES6基础之Array.fill函数

引子 关注「时事」的同学都会注意到&#xff0c;ES6和TypeScript是时下「投资」的热门。作为一位老同学&#xff0c;我也注意到这一点&#xff0c;不断收集一些小东东&#xff0c;以备不时之需。 正题 本文中记录的是ES6为Array增加的fill()函数。其功能是&#xff0c;用户可以…

CSP 202006-1 线性分类器 python实现+详解

试题 代码 # 读入m,n m, n [int(i) for i in input().split()]# 创建字典存放已知点 dict {} for i in range(m):line input().split()x, y, t linex, y int(x), int(y)dict[(x, y)] t# 逐条线地去判断每一个点 # 创建两个集合&#xff0c;一个存放坐标代入公式后大于零…