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(); }}
/*