如何在java swing中制作流畅的动画

时间:2017-05-22 05:45:22

标签: java swing animation

嗨我正在使用Timer类和JPanel的重绘方法,但每当我按下DOWN并且太空船在屏幕中间移动时,它会收缩并保持原位。它永远不会到达窗口的底部。当我把它移到右边时也一样。

此外,当我尝试将其置于不同的y位置时,背景不再是黑色。请有人帮我这个吗?以下是我的代码示例:

package javapaint;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SpaceShipFlight 
{
    private JFrame windowFrame;
    private BufferedImage shipSprite;
    private BufferedImage spaceBackground;


    /** MAIN METHOD **/

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    SpaceShipFlight window = new SpaceShipFlight();
                    window.windowFrame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }



    /** CONSTRUCTOR **/

    public SpaceShipFlight() throws IOException
    {
        /*************************************************************************
                               SPACE SHIP FLIGHT JFRAME
        *************************************************************************/

        windowFrame = new JFrame("Space Ship Flight");


        windowFrame.setBounds(0, 0, 950, 700);


        windowFrame.setBackground(Color.BLACK);





        /*************************************************************************
                           SPACE SHIP FLIGHT SPRITES (FOR DRAWING)
        *************************************************************************/

        spaceBackground = ImageIO.read(new File("Space (Medium).png")); 
        // 450 x 374


        shipSprite = ImageIO.read(new File("Ship Sprite.png"));
        // 64 x 64 png of space ship





        /*************************************************************************
                           SPACE SHIP FLIGHT JPANEL (FOR DRAWING)
        *************************************************************************/

        SpacePanel spacePanel = new SpacePanel();



        windowFrame.add(spacePanel);



        /*************************************************************************
                                 SPACE SHIP FLIGHT TIMER 
        *************************************************************************/

        Timer timer = new Timer(10, new AnimationListener(spacePanel));
        timer.start();






        /*************************************************************************
                           EVENT HANDLER TO MOVE SPACE SHIP
        *************************************************************************/
        /*
        EventHandler <KeyEvent> moveShip = new EventHandler<KeyEvent> ()
        {
            @Override
            public void handle(KeyEvent event) 
            {
                if (event.getCharacter().equalsIgnoreCase("X"))
                {
                    spacePanel.setX( spacePanel.getX() + 1 );
                }
            }

        };

        */

        KeyListener moveShip = new KeyListener()
        {

            @Override
            public void keyTyped(KeyEvent e) 
            {

            }

            @Override
            public void keyPressed(KeyEvent e) 
            {
                if (e.getKeyCode() == KeyEvent.VK_RIGHT && spacePanel.getX() < 375)
                {
                    spacePanel.setX( spacePanel.getX() + 5 );
                    spacePanel.repaint();
                }

                if (e.getKeyCode() == KeyEvent.VK_LEFT && spacePanel.getX() > 0)
                {
                    spacePanel.setX( spacePanel.getX() - 5);
                    spacePanel.repaint();
                }

                if (e.getKeyCode() == KeyEvent.VK_UP && spacePanel.getY() > 0)
                {
                    spacePanel.setY( spacePanel.getY() - 5);
                    spacePanel.repaint();
                }

                if (e.getKeyCode() == KeyEvent.VK_DOWN && spacePanel.getY() < 700)
                {
                    spacePanel.setY( spacePanel.getY() + 5 );
                    spacePanel.repaint();
                }

                if (e.getKeyCode() == KeyEvent.VK_X)
                {
                    System.out.printf("X: %d, Y: %d\n", spacePanel.getX(),
                                                            spacePanel.getY());
                }
            }

            @Override
            public void keyReleased(KeyEvent e) 
            {

            }

        };





        windowFrame.addKeyListener(moveShip);




    }





    /** ANIMATION LISTENER CLASS **/

    public static class AnimationListener implements ActionListener
    {
        private SpacePanel panel;
        private Graphics graphics;


        public AnimationListener(SpacePanel panel)
        {
            this.panel = panel;
            this.graphics = panel.getGraphics();
        }


        @Override
        public void actionPerformed(ActionEvent e) 
        {
            panel.repaint();
        }

    }





    /** SPACE PANEL SUBCLASS **/

    public class SpacePanel extends JPanel
    {
        // Variables for the space ship's position temporarily placed here
        private int x;
        private int y;


        // Constructor
        public SpacePanel()
        {
            super();

            x = 0;
            y = 0;
        }


        // Getters
        public int getX()
        {
            return x;
        }

        public int getY()
        {
            return y;
        }


        // Setters
        public void setX(int newX)
        {
            x = newX;
        }

        public void setY(int newY)
        {
            y = newY;
        }






        @Override
        public void paint(Graphics g)
        {
            g.drawImage(spaceBackground, 0, 0, this);
            g.drawImage(shipSprite, x, y, this);
        }
    }









}

0 个答案:

没有答案