Java Swing animation (2)

Source: Internet
Author: User

Java Swing animation (2)
I. animation effect: it realizes the animation effect of airplane flight and the movement of aircraft.

II. Implementation principle:

1. Aircraft Flight effect: in fact, it is still rewritten.PaintComponent,Changing images at a certain interval will have the flight effect.,Animation is to change the image.

2. Mobile Aircraft: implements the KeyListener interface, which can receive Keyboard Events. Use this panel container to listen for some events.

Iii. Code:

Package com. jack; import java. awt. gridLayout; import java. awt. list; import javax. swing. JFrame;/***** @ author laughing * @ date 8:11:39 on January 1, November 16, 2014 */public class Main extends JFrame {GamePanelpanel = new GamePanel (); public Main () {setSize (panel. getWidth (), panel. getHeight (); getContentPane (). add (panel); setVisible (true); setResizable (false); setdefaclocloseoperation (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 7:58:11 on January 1, November 16, 2014 */public class GamePanel extends JPanel implements Runnable, KeyListener {private static final intHEIGHT = 480; private static final intWEIGHT = 320; Image [] plans = new Image [6]; intpos = 0; intx = WEIGHT/2; inty = 408; final intstep = 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); // listener event addKeyListener (this); new Thread (this ). start ();}/** (non-Javadoc) ***** @ see javax. swing. JComponent # paintComponent (java. awt. graphics) */@ Overrideprotected void paintComponent (Graphics g0) {// TODO Auto-generated method stubsuper. 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 blocke. printStackTrace () ;}}/** (non-Javadoc) ** @ see java. lang. runnable # run () */@ Overridepublic void run () {// TODO Auto-generated method stubwhile (true) {try {pos ++; if (pos = 5) pos = 0; Thread. sleep (100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} repaint () ;}}/** (non-Javadoc) ** @ see java. awt. event. keyListener # keyTyped (java. awt. event. keyEvent) * // @ Overridepublic void keyTyped (KeyEvent e) {// TODO Auto-generated method stub}/** (non-Javadoc) ** @ see java. awt. event. keyListener # keyPressed (java. awt. event. keyEvent) */@ Overridepublic 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) * // @ Overridepublic void keyReleased (KeyEvent e) {// TODO Auto-generated method stub }}

4. Download source code:

Click Open Link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.