java处理位图_java中裁剪图(处理位图)

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;

}

}

e8f21a8c292eb21bd81196a5908283d3.png

大小: 328.9 KB

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2009-08-06 10:03

浏览 931

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值