java图片插入窗口定时循环移动或者按鼠标位置移动

来源:互联网 发布:合泰触摸单片机芯片 编辑:程序博客网 时间:2024/06/06 01:02
package oo.day06;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.image.BufferedImage;import javax.imageio.ImageIO;import java.awt.Graphics;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.Timer;import java.util.TimerTask;public class Picture extends JPanel {public static final int WIDTH=400;public static final int HEIGHT=700;private int x1=0;private int y1=0;private int x2=140;private int y2=400;private int width1=WIDTH;private int height1=HEIGHT;private int y3=-this.height1;private int step=1;private int width2=97;private int height2=124;public static BufferedImage LoadImage(String fireName) {try {BufferedImage image = ImageIO.read(Picture.class.getResource(fireName));return image;//读取同包中的图片资源}catch(Exception e) {//打印异常e.printStackTrace();throw new RuntimeException();}}private static BufferedImage[] images=new BufferedImage[3];static {images[0]=LoadImage("background.png");//读取图片images[1]=LoadImage("background.png");images[2]=LoadImage("hero0.png");}public void step() {y1+=step;//向下y3+=step;//向下if(y1>=this.height1) {//当y>=窗口的高,意味着出了窗口了y1=-this.height1;//将y图片放到最上面}if(y3>=this.height1) {//当y3>=窗口的高,意味着出了窗口了y3=-this.height1;//将y3图片放到最上面}}public void paint(Graphics g) {//画具体的对象g.drawImage(images[0],x1,y1,null);g.drawImage(images[1],x1,y3,null);g.drawImage(images[2],x2,y2,null);}public void action() {MouseAdapter l =new MouseAdapter() {//创建对象public void mouseMoved(MouseEvent e) {int x=e.getX();//获取鼠标的x坐标int y=e.getY();//获取鼠标的y坐标x2=x-width2/2;//图片的x随着鼠标x移动y2=y-height2/2; //图片的y随着鼠标y移动}};this.addMouseListener(l);//处理鼠标操作事件this.addMouseMotionListener(l);//处理鼠标滑动事件Timer timer = new Timer();// 定时器对象int interval = 10;// 时间间隔(以毫秒为单位)timer.schedule(new TimerTask() {public void run() {//定时干的事step();repaint();}// 第一个10从程序启动到第一次触发的时间,第二个10从第n次到n+1次的时间}, interval, interval);}public static void main(String[] args) {JFrame frame = new JFrame("Fly");// 创建窗口对象Picture picture = new Picture();// 创建一个面板对象frame.add(picture);// 将面板添加到窗口中frame.setSize(WIDTH, HEIGHT);// 设置窗口的宽和高frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗口关闭时退出程序frame.setLocationRelativeTo(null);// 设置居中显示frame.setVisible(true);// 设置窗口可见picture.action();// 启动程序的执行}}


图片:background.png


图片:hero0.png



结果如图: