java动画

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
 
public class Animation extends Frame
{
    private static final long serialVersionUID = 1L;
    private static boolean PRESSED = false;
    private static int pointX = 0;
    private static int pointy = 200;
	private static int RIGHT_GO = 0;
    private static int LEFT_GO = 0;
    //方向
    private static int DIR = 0;
    private static int ANGLE = 0;
    private static int W = 50;
    private static int H = 60;
    private _Canvas canvas = null;
 
    public Animation ()
    {
        add (canvas = new _Canvas ());
      //取消画布repaint方法的重画功能
        setIgnoreRepaint (true);
        requestFocus ();
    }
 
    public class _Canvas extends Canvas implements Runnable
    {
        private static final long serialVersionUID = 1L;
        private BufferedImage bi = null;
        private Image bufferedImage = null;
        private Thread thread = null;
        private long sleepTime = 10;
 
        public _Canvas ()
        {
          
            try
            {
                bi = ImageIO.read (new File ("go.png"));
            }
            catch (IOException e)
            {}
            setBackground (Color.BLACK);
            //使该组件获得焦点
            requestFocus ();
            addKeyListener (new KeyListener ()
            {
            	
                @Override
                public void keyTyped ( KeyEvent e )
                {}
 
                @Override
                public void keyReleased ( KeyEvent e )
                {
                    RIGHT_GO = 0;
                    PRESSED = false;
                }
 
                @Override
                public void keyPressed ( KeyEvent e )
                {                   	
                    // 38 40 37 39上下左右  键盘的Ascii码
                    DIR = e.getKeyCode ();
              
                    PRESSED = true;
                }
            });
        }
 
        @Override
        public void paint ( Graphics g )
        {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint (RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.drawImage (rotateImage (bi.getSubimage (RIGHT_GO, LEFT_GO, W, H), ANGLE, true), pointX, pointy, W, H,
                this);
            g2d.dispose ();
        }
 
        @Override
        public void update ( Graphics g )
        {
            if (null == bufferedImage)
            {
                bufferedImage = createImage (getWidth (), getHeight ());
            }
            Graphics bufferedG = bufferedImage.getGraphics ();
            bufferedG.clearRect (0, 0, getWidth (), getHeight ());
            paint (bufferedG);
            bufferedG.dispose ();
            g.drawImage (bufferedImage, 0, 0, this);
            g.dispose ();
        }
 
        public void start ()
        {
            thread = new Thread (this);
            thread.setName ("TestImage");
            thread.setPriority (Thread.MIN_PRIORITY);
            thread.start ();
        }
 
        public synchronized void stop ()
        {
            thread = null;
            notify ();
        }
 
        @Override
        public void run ()
        {
            Thread me = Thread.currentThread ();
            while (thread == me && !isShowing () || getSize ().width == 0)
            {
                try
                {
                    Thread.sleep (555);
                }
                catch (InterruptedException e)
                {
                    return;
                }
            }
            while (thread == me && isShowing ())
            {
                if (PRESSED)
                {
                    try
                    {
 
                        if (DIR == 39)
                        {
                            RIGHT_GO = RIGHT_GO + 50;
                            LEFT_GO = 0;
                            pointX = pointX + 1;
                            if (pointX > 420)
                            {
                                ANGLE = 90;
                                pointX--;
                                pointy--;
                                W = 60;
                                H = 50;
                            }
                            if (RIGHT_GO > 50)
                            {
                                RIGHT_GO = 0;
                            }
                        }
                        else if (DIR == 37)
                        {
                            pointX = pointX - 1;
                            RIGHT_GO = RIGHT_GO + 50;
                            LEFT_GO = 60;
                            if (pointX < 0)
                            {
                                ANGLE = -90;
                                pointX++;
                                pointy--;
                                W = 60;
                                H = 50;
                            }
                            if (RIGHT_GO > 50)
                            {
                                RIGHT_GO = 0;
                            }
                        }
                        else if (DIR == 38)
                        {
                            W = 50;
                            H = 60;
                            pointy = 150;
                            ANGLE = 0;
                            RIGHT_GO = 100;
                        }
                        else if (DIR == 40)
                        {
                            W = 50;
                            H = 60;
                            ANGLE = 0;
                            pointy = 200;
                            RIGHT_GO = 0;
                        }
                        Thread.sleep (sleepTime);
                        repaint ();
                    }
                    catch (InterruptedException e)
                    {
                        break;
                    }
                }
                else
                {
                    RIGHT_GO = RIGHT_GO + 50;
                    LEFT_GO = 0;
                    pointX = pointX + 1;
                    if (RIGHT_GO >= 50)
                    {
                        RIGHT_GO = 0;
                    }
                    if (pointX > 500)
                    {
                        pointX = 0;
                    }
                    try
                    {
                        Thread.sleep (sleepTime);
                        repaint ();
                    }
                    catch (InterruptedException e)
                    {
                        break;
                    }
                }
            }
            thread = null;
        }
    }
 
    /**
     * 旋转图像为指定角度
     * 
     * @param degree
     * @return
     */
    public static BufferedImage rotateImage ( final BufferedImage image, final int angdeg, final boolean d )
    {
        int w = image.getWidth ();
        int h = image.getHeight ();
        int type = image.getColorModel ().getTransparency ();
        BufferedImage img;
        Graphics2D graphics2d;
        ( graphics2d = ( img = new BufferedImage (w, h, type) ).createGraphics () ).setRenderingHint (
            RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.rotate (d ? -Math.toRadians (angdeg) : Math.toRadians (angdeg), w / 2, h / 2);
        graphics2d.drawImage (image, 0, 0, null);
        graphics2d.dispose ();
        return img;
    }
 
    public static void main ( String[] args )
    {
       
        EventQueue.invokeLater (new Runnable ()
        {
            @Override
            public void run ()
            {
                final Animation ti = new Animation ();
                ti.setSize (new Dimension (500, 300));
                ti.setLocationRelativeTo (null);
                ti.addWindowListener (new WindowAdapter ()
                {
                    @Override
                    public void windowClosing ( WindowEvent e )
                    {
                        System.exit (0);
                    }
 
                    @Override
                    public void windowDeiconified ( WindowEvent e )
                    {
                        ti.canvas.start ();
                    }
 
                    @Override
                    public void windowIconified ( WindowEvent e )
                    {
                        ti.canvas.stop ();
                    }
                });
                ti.setResizable (false);
                ti.canvas.start ();
                ti.setVisible (true);
            }
        });
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值