java 小马_Java 窗口 小马图像窗口

1 packageponyimg;2

3 import java.awt.*;4 importjava.awt.event.ActionEvent;5 importjava.awt.event.ActionListener;6 importjava.awt.event.MouseAdapter;7 importjava.awt.event.MouseEvent;8 importjava.awt.event.MouseMotionAdapter;9 importjava.awt.event.MouseWheelEvent;10 importjava.awt.event.MouseWheelListener;11 import java.io.*;12 importjavax.swing.ImageIcon;13 importjavax.swing.JButton;14 importjavax.swing.JFileChooser;15 importjavax.swing.JFrame;16 importjavax.swing.JLabel;17

18 public class PonyIMG extendsJFrame19 {20 static int minx=192,miny=108;21 static boolean ishide=false;22 static intpframexOri,pframeyOri,pframex,pframey,ponyresult;23 static int ori_pict_x,ori_pict_y,pict_x,pict_y,ponyscale=20;24 staticString ponySname;25 staticImageIcon ponyimgtmp;26

27 public static void main(String[] args) throwsException28 {29 JFrame ponyFrame=new JFrame("PonyIMG");30 File directory = new File("");31 ponyFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\32.png"));32 ImageIcon ponydefaultimg=new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\pictures\\default.png"));33 ori_pict_x=ponydefaultimg.getIconWidth();34 ori_pict_y=ponydefaultimg.getIconHeight();35 JButton ponyb_front = new JButton("");36 JButton ponyb_exit = new JButton("");37 JButton ponyb_open = new JButton("");38 JButton ponyb_hide = new JButton("");39 ImageIcon ponyb_fronti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\front.png"));40 ImageIcon ponyb_nfronti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\nfront.png"));41 ImageIcon ponyb_exiti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\exit.png"));42 ImageIcon ponyb_openi = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\open.png"));43 ImageIcon ponyb_hidei = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\hide.png"));44 ImageIcon ponyb_uhidei = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\uhide.png"));45 JFileChooser ponychooser = new JFileChooser(directory.getCanonicalPath()+"\\pictures");46 ponychooser.setFileSelectionMode(JFileChooser.FILES_ONLY);47 ponychooser.setDialogTitle("Get an image...");48

49 /*Whole*/

50 ponyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);51 ponyFrame.setLayout(null);52 ponyFrame.setResizable(false);53 ponyFrame.setLocationRelativeTo(null);54 ponyFrame.setSize(ponydefaultimg.getIconWidth(),ponydefaultimg.getIconHeight());55 ponyFrame.setUndecorated(true);56 ponyFrame.setAlwaysOnTop(false);57 ponyFrame.setBackground(new Color(0,0,0,0));58

59 /*BUTTON_FRONT*/

60 ponyb_front.setIcon(ponyb_nfronti);61 ponyb_front.setSize(27, 27);62 ponyb_front.setLocation(ponydefaultimg.getIconWidth()-64, 5);63 ponyb_front.addActionListener(newActionListener(){64 public voidactionPerformed(ActionEvent e)65 {66 if(ponyFrame.isAlwaysOnTop()==true)67 {68 ponyFrame.setAlwaysOnTop(false);69 ponyb_front.setIcon(ponyb_nfronti);70 }71 else

72 {73 ponyFrame.setAlwaysOnTop(true);74 ponyb_front.setIcon(ponyb_fronti);75 }76 }77 });78 ponyFrame.add(ponyb_front);79

80 /*BUTTON_EXIT*/

81 ponyb_exit.setIcon(ponyb_exiti);82 ponyb_exit.setSize(27, 27);83 ponyb_exit.setLocation(ponydefaultimg.getIconWidth()-32, 5);84 ponyb_exit.addActionListener(newActionListener(){85 public voidactionPerformed(ActionEvent e)86 {87 System.exit(0);88 }89 });90 ponyFrame.add(ponyb_exit);91

92 /*BUTTON_OPEN*/

93 ponyb_open.setIcon(ponyb_openi);94 ponyb_open.setSize(27, 27);95 ponyb_open.setLocation(5, ponydefaultimg.getIconHeight()-32);96 ponyFrame.add(ponyb_open);97

98 /*BUTTON_HIDE*/

99 ponyb_hide.setIcon(ponyb_uhidei);100 ponyb_hide.setSize(27, 27);101 ponyb_hide.setLocation(5, 5);102 ponyb_hide.addActionListener(newActionListener(){103 public voidactionPerformed(ActionEvent e)104 {105 if(ishide==false) {106 ishide=true;107 ponyb_hide.setIcon(ponyb_hidei);108 ponyb_front.setVisible(false);109 ponyb_exit.setVisible(false);110 ponyb_open.setVisible(false);111 }112 else{113 ishide=false;114 ponyb_hide.setIcon(ponyb_uhidei);115 ponyb_front.setVisible(true);116 ponyb_exit.setVisible(true);117 ponyb_open.setVisible(true);118 }119

120 }121 });122 ponyFrame.add(ponyb_hide);123

124 /*IMG*/

125 JLabel Label00 = newJLabel(ponydefaultimg);126 Label00.setSize(ponydefaultimg.getIconWidth(),ponydefaultimg.getIconHeight());127 ponyFrame.add(Label00);128

129 /*BUTTON_OPEN*/

130 ponyb_open.addActionListener(newActionListener(){131 public voidactionPerformed(ActionEvent e)132 {133 ponyresult=ponychooser.showOpenDialog(ponyFrame);134 if(ponychooser.APPROVE_OPTION ==ponyresult)135 {136 ponySname=ponychooser.getSelectedFile().getPath();137 ponyimgtmp=newImageIcon(Toolkit.getDefaultToolkit().getImage(ponySname));138

139 if(!(ponyimgtmp.getIconWidth()>=minx && ponyimgtmp.getIconHeight()>=miny))140 {141 ponyimgtmp=ponydefaultimg;142 }143 ori_pict_x=ponyimgtmp.getIconWidth();144 ori_pict_y=ponyimgtmp.getIconHeight();145 Label00.setIcon(ponyimgtmp);146 ponyFrame.setSize(ponyimgtmp.getIconWidth(),ponyimgtmp.getIconHeight());147 Label00.setSize(ponyimgtmp.getIconWidth(),ponyimgtmp.getIconHeight());148 ponyb_front.setLocation(ponyimgtmp.getIconWidth()-64, 5);149 ponyb_exit.setLocation(ponyimgtmp.getIconWidth()-32, 5);150 ponyb_open.setLocation(5, ponyimgtmp.getIconHeight()-32);151 ponyscale=20;152 }153 }154 });155

156 /*MOUSE*/

157 ponyFrame.addMouseListener(newMouseAdapter() {158 @Override159 public voidmousePressed(MouseEvent e) {160 pframexOri=e.getX();161 pframeyOri=e.getY();162 }163 });164 ponyFrame.addMouseMotionListener(newMouseMotionAdapter() {165 @Override166 public voidmouseDragged(MouseEvent e) {167 ponyFrame.setLocation(e.getXOnScreen()-pframexOri,e.getYOnScreen()-pframeyOri);168 }169 });170 ponyFrame.addMouseWheelListener(newMouseWheelListener() {171 @Override172 public voidmouseWheelMoved(MouseWheelEvent e) {173 if(e.getWheelRotation()==-1){174 if(ponyscale<=19) {175 ++ponyscale;176 pict_x=(ori_pict_x/20)*ponyscale;177 pict_y=(ori_pict_y/20)*ponyscale;178 if(ponyscale==20) {179 pict_x=ori_pict_x;180 pict_y=ori_pict_y;181 }182 ImageIcon ponytmp=newImageIcon(ponyimgtmp.getImage().getScaledInstance(pict_x, pict_y, Image.SCALE_DEFAULT));183 Label00.setIcon(ponytmp);184 ponyFrame.setSize(pict_x,pict_y);185 Label00.setSize(pict_x,pict_y);186 ponyb_front.setLocation(pict_x-64, 5);187 ponyb_exit.setLocation(pict_x-32, 5);188 ponyb_open.setLocation(5, pict_y-32);189 }190 }191 if(e.getWheelRotation()==1){192 if(ponyscale>=1 && ((ori_pict_x/20)*(ponyscale-1)>=minx) && ((ori_pict_y/20)*(ponyscale-1)>=miny)) {193 --ponyscale;194 pict_x=(ori_pict_x/20)*ponyscale;195 pict_y=(ori_pict_y/20)*ponyscale;196 ImageIcon ponytmp=newImageIcon(ponyimgtmp.getImage().getScaledInstance(pict_x, pict_y, Image.SCALE_DEFAULT));197 Label00.setIcon(ponytmp);198 ponyFrame.setSize(pict_x,pict_y);199 Label00.setSize(pict_x,pict_y);200 ponyb_front.setLocation(pict_x-64, 5);201 ponyb_exit.setLocation(pict_x-32, 5);202 ponyb_open.setLocation(5, pict_y-32);203 }204 }205 }206 });207

208 ponyFrame.setVisible(true);209 }210 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值