• Swing动画之游戏角色


    一、动画效果:实现了飞机飞行的动画效果,也实现了飞机的移动。                 

    二、实现原理:

      1.飞机飞行 的效果:事实上也还是重写paintComponent,依照一定的时间间隔更换图片就有了飞行的效果动画就是更换图片                                             

      2.移动飞机:实现KeyListener这个接口,这个接口能够接收键盘的事件。用这个面板容器去监听些事件。                                              

    三、代码:

    package com.jack;
    
    import java.awt.GridLayout;
    import java.awt.List;
    
    import javax.swing.JFrame;
    
    /**
     *
     * @author laughing
     * @date 2014年11月16日 下午8:11:39
     */
    public class Main extends JFrame {
    	GamePanel	panel	= new GamePanel();
    	public Main() {
    		setSize(panel.getWidth(), panel.getHeight());
    		getContentPane().add(panel);
    		setVisible(true);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocationRelativeTo(this);
    	}
    
    	public static void main(String args[]) {
    		new Main();
    	}
    
    }
    

    package com.jack;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    import javax.swing.border.SoftBevelBorder;
    
    /**
     *
     * @author laughing
     * 
     * @date 2014年11月16日 下午7:58:11
     */
    public class GamePanel extends JPanel implements Runnable, KeyListener {
    
    	private static final int	HEIGHT	= 480;
    	private static final int	WEIGHT	= 320;
    	Image[]						plans	= new Image[6];
    	int							pos		= 0;
    	int							x		= WEIGHT / 2;
    	int							y		= 408;
    	final int					step	= 10;
    
    	public GamePanel() {
    		setSize(WEIGHT, HEIGHT);
    		setBorder(new SoftBevelBorder(1, Color.white, Color.white));
    		// Sets the focusable state of this Component to the specified value.
    		// This value overrides the Component's default focusability.
    		setFocusable(true);
    		// 监听事件
    		addKeyListener(this);
    		new Thread(this).start();
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * 
    	 * 
    	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
    	 */
    	@Override
    	protected void paintComponent(Graphics g0) {
    		// TODO Auto-generated method stub
    		super.paintComponent(g0);
    		Graphics2D g = (Graphics2D) g0;
    
    		try {
    			for (int i = 0; i < 6; i++) {
    				plans[i] = ImageIO.read(this.getClass()
    											.getResource("../../images/plan_" + i + ".png"));
    
    			}
    			g.drawImage(plans[pos], x, y, this);
    
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.lang.Runnable#run()
    	 */
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		while (true) {
    
    			try {
    				pos++;
    				if (pos == 5)
    					pos = 0;
    				Thread.sleep(100);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			repaint();
    		}
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
    	 */
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
    	 */
    	@Override
    	public void keyPressed(KeyEvent e) {
    
    		int key = e.getKeyCode();
    		if (key == KeyEvent.VK_UP) {
    			if (y - step >= 0) {
    				y -= step;
    			}
    		}
    		if (key == KeyEvent.VK_DOWN) {
    
    			if (y + step <= HEIGHT - plans[0].getHeight(null) * 2) {
    				y += step;
    			}
    		}
    		if (key == KeyEvent.VK_LEFT) {
    			if (x - step >= 0)
    				x -= step;
    		}
    		if (key == KeyEvent.VK_RIGHT) {
    
    			if (x + step < WEIGHT - plans[0].getWidth(null))
    				x += step;
    		}
    
    	}
    
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
    	 */
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    }
    

    四、源代码下载:

    点击打开链接

  • 相关阅读:
    感谢梦想
    感谢迷茫
    CSS动画
    ES6笔记③
    Object的增。删。查。改。遍历
    js创建对象的方式 三种
    js 控制台的错误提示
    ES6笔记② 箭头函数
    ES6笔记① var 和 let的区别
    js冒泡排序
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4497642.html
Copyright © 2020-2023  润新知