首页 > 代码库 > Java Swing 动画(二)
Java 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 } }
四、源码下载:
点击打开链接
Java Swing 动画(二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。