package test; import java.awt.BorderLayout; import java.awt.Component; import java.awt.GridLayout; import java.awt.Image; import java.awt.Point; 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 java.awt.image.CropImageFilter; import java.awt.image.FilteredImageSource; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.Timer; public class GetImage implements ActionListener{ JFrame jf=null; Image image=null; JPanel jp=null; Toolkit tool; JLabel jb=null; Point xy=new Point(0,0); int currentFrame=2; int countFrame=7; int direction=0; int singleActionWidth=80; int singleActionHeight=91; Timer uiTimer; int speed=50; JTextField jfiled; public GetImage(){ jp=new JPanel(); jf=new JFrame("rpg ha"); jb=new JLabel("exportImage"); jp.add(jb,BorderLayout.NORTH); JPanel buttons=new JPanel(); buttons.setLayout(new GridLayout(3,4)); JButton jb1=new MyButton("上",3); JButton jb2=new MyButton("下",0); MyButton jb3=new MyButton("左",1); MyButton jb4=new MyButton("右",2); MyButton jb5=new MyButton("左上",6); MyButton jb6=new MyButton("左下",4); MyButton jb7=new MyButton("右上",7); MyButton jb8=new MyButton("右下",5); //MyButton jb9=new MyButton("停止",9); jfiled=new JTextField(this.speed); jfiled.addPropertyChangeListener(new PropertyChangeListener(){ @Override public void propertyChange(PropertyChangeEvent evt) { try { speed=Integer.parseInt(GetImage.this.jfiled.getText()); } catch (NumberFormatException e) { //jfiled.setText("请输入正确的整数"); e.printStackTrace(); } } }); addButtons(buttons,jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8); JButton jb9=new JButton("行走"); JButton jb10=new JButton("停止"); buttons.add(jb9); buttons.add(jb10); // buttons.add(this.jfiled); jb9.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { GetImage.this.walk(); } }); jb10.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { stop(); } }); jp.add(buttons,BorderLayout.CENTER); jf.add(jp,BorderLayout.CENTER); jp.setToolTipText("hello"); this.tool=Toolkit.getDefaultToolkit(); image=Toolkit.getDefaultToolkit().createImage("E:\\test\\part1\\javaImage\\src\\h1.png"); //imageAction(); //cutImage(); initTimer(); } public void addButtons(JPanel ct,Component ...args){ for(Component cpc:args){ ct.add(cpc); cpc.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e){ System.out.println("click"); MyButton jb=(MyButton) e.getComponent(); GetImage.this.direction=jb.direction; } }); } } public void imageAction(){ Runnable action=new Runnable(){ @Override public void run() { while(true){ currentFrame=currentFrame+1; if(currentFrame>countFrame){ currentFrame=0; } cutImage(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }; Thread tr=new Thread(action); tr.start(); } public void cutImage(){ CropImageFilter cif=new CropImageFilter(singleActionWidth*currentFrame, this.direction*singleActionHeight, singleActionWidth,singleActionHeight); FilteredImageSource fis=new FilteredImageSource(this.image.getSource(),cif); Image newImage=tool.createImage(fis);//类似flex copypiely ImageIcon newIcon=new ImageIcon(); newIcon.setImage(newImage); jb.setIcon(newIcon); } public void initTimer(){ this.uiTimer=new Timer(speed,this); this.uiTimer.start(); } public void walk(){ if(!uiTimer.isRunning()){ uiTimer.start(); } } public void stop(){ if(uiTimer.isRunning()){ uiTimer.stop(); } } public void show(){ jf.setVisible(true); jf.setSize(400, 400); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String args[]){ GetImage gi=new GetImage(); gi.show(); } @Override public void actionPerformed(ActionEvent e) { currentFrame=currentFrame+1; if(currentFrame>countFrame){ currentFrame=0; } cutImage(); } } class MyButton extends JButton{ int direction=0; boolean addAction=true; public MyButton(String name,int direction){ this(name,direction,true); } public MyButton(String name,int direction,boolean addAction){ super(name); this.direction=direction; this.addAction=addAction; } }