如何创建交互式动画?

时间:2013-03-20 03:06:27

标签: java swing animation keylistener key-bindings

所以我的老师要我制作一个互动动画。问题是他没有告诉班级怎么做。 (这是一个在线课程)。要点是他希望我制作一个可以用键盘控制的图形。我在网上看了一下,但显然我没有问过正确的问题,因为出于某种原因,我要空了。

反正。我有三个代码文件。 Zorx1,Zorx2和Zorx3是同一个外星人,颜色略有不同,由老师给出。

// The Zorx alien object
import java.awt.*;
public class Zorx1
{
/* the fields of the object defined here as static to make the 
interface as simple as possible to start*/
static int x = 100, y = 100;
static int size = 50;
static Color clr = Color.red;
static boolean showing = false;
// paints Zorx on the screen Note this method is not part of Interface
static void paint (Graphics g)
{
    g.setColor (clr);
    g.fillRect (x, y, size / 4, size);
    g.setColor (Color.black);
    g.drawLine (x-3,y-2,x-1,y+size/8);//These four lines will add fangs to this guy. Nasty!
    g.drawLine (x-2,y-2,x-1,y+size/8);
    g.drawLine (x+3,y-2,x+5,y+size/8);
    g.drawLine (x+2,y-2,x+4,y+size/8);
    g.setColor (clr);
    g.fillOval (x - (3 * size / 8), y - (size / 2), size, size / 2);
    g.drawLine (x - 3 * size / 8, y + size, x - 3 * size / 8 + size, y + size);
    g.drawLine (x, y + size / 2, x - size, y);
    g.drawLine (x - size, y, x - size, y - size / 4);
    g.drawOval (x - size - size / 8, y - size / 4 - size / 8, size / 8, size / 8);
    g.setXORMode (Color.black); //Makes his eyes black. Sinister
    g.fillOval (x - size / 4, y - size / 3, size / 12, size / 12);
    g.fillOval (x + size / 4, y - size / 3, size / 12, size / 12);
    g.fillOval (x - size / 1000, y - size / 4, size / 12, size / 12); //adds a third eye to our alien. Spooooooooky.
    g.setPaintMode ();
} // end of paint method

// Show the Zorx alien. The show method is part of the interface.
public static void show (Graphics g)
{
    paint (g);
    showing = true;
} // end of show method

// Erase Zorx from the screen. The erase method
// is not part of the interface.
static void erase (Graphics g, Color backgroundColor)
{
    g.setColor (backgroundColor);
    g.fillRect (x, y, size / 4, size);
    g.fillOval (x - (3 * size / 8), y - (size / 2), size, size / 2);
    g.drawLine (x-3,y-2,x-1,y+size/8);
    g.drawLine (x-2,y-2,x-1,y+size/8);
    g.drawLine (x+3,y-2,x+5,y+size/8);
    g.drawLine (x+2,y-2,x+4,y+size/8);
    g.drawLine (x - 3 * size / 8, y + size, x - 3 * size / 8 + size, y + size);
    g.drawLine (x, y + size / 2, x - size, y);
    g.drawLine (x - size, y, x - size, y - size / 4);
    g.drawOval (x - size - size / 8, y - size / 4 - size / 8, size / 8, size / 8);
    g.fillOval (x - size / 4, y - size / 3, size / 12, size / 12);
    g.fillOval (x + size / 4, y - size / 3, size / 12, size / 12);
} // end of erase method

// Hide Zorx The hide method is part of the interface.
public static void hide (Graphics g, Color backgroundColor)
{
    erase (g, backgroundColor);
    showing = false;
} // end of hide method

// Move Zorx from one location to another. The move method
// is part of the interface.
public static void move (int newX, int newY, Graphics g, Color backgroundColor)
{
    if (showing)
    {
        erase (g, backgroundColor);
    }
    x = newX;
    y = newY;
    if (showing)
    {
        show (g);
    }
} // end of move method
} // end of Zorx Class

外星物体具有移动物体所需的所有方法和一切,而动画应该控制所有这些。

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Animation extends JFrame implements KeyListener
{
    public Animation (Graphics g)
    {
        Zorx1.show(g);
        Zorx2.show(g);
        Zorx3.show(g);
        this.setFocusable(true);
    }
    public static void main (String args[])
    {
        Animation application = new Animation(g);
        application.setVisible(true);
    }
    public int x=100;
    public int y=100;
    public void keyPressed (KeyEvent e)
    {
        switch (e.getKeyCode()) //I can't seem to get this thing to animate through this so this is just left blank for the sake of necessity.
        {
            case KeyEvent.VK_LEFT:
            break;
            case KeyEvent.VK_RIGHT:
            break;
            case KeyEvent.VK_DOWN:
            break;
            case KeyEvent.VK_UP:
            break;
        }
    }
    public void keyTyped(KeyEvent e) 
    {
        if (e.getKeyChar() == KeyEvent.VK_LEFT) 
        {
            left(g);
        }
        else if (e.getKeyChar() == KeyEvent.VK_RIGHT)
        {
            right(g);
        }
        else if (e.getKeyChar() == KeyEvent.VK_UP)
        {
            up(g);
        }
        else if (e.getKeyChar() == KeyEvent.VK_DOWN)
        {
            down(g);
        }
        if (e.getKeyChar() == KeyEvent.VK_ESCAPE) 
        {
            System.exit(0);
            // If you hit escape you will exit the animation
        }
    }
    public void left (Graphics g)
    {
        x=x-10;
        Zorx1.move (x,y, g, getBackground ());
        Zorx2.move (x,y+50,g,getBackground());
        Zorx3.move (x,y+100,g,getBackground());
    }
    public void right (Graphics g)
    {
        x=x+10;
        Zorx1.move (x,y,g,getBackground());
        Zorx2.move (x,y+50,g,getBackground());
        Zorx3.move (x,y+100,g,getBackground());
    }
    public void up(Graphics g)
    {
        y=y-10;
        Zorx1.move (x,y,g,getBackground());
        Zorx2.move (x,y+50,g,getBackground());
        Zorx3.move (x,y+100,g,getBackground());
    }
    public void down(Graphics g)
    {
        y=y+10;
        Zorx1.move (x,y,g,getBackground());
        Zorx2.move (x,y+50,g,getBackground());
        Zorx3.move (x,y+100,g,getBackground());
    }
    public void keyReleased(KeyEvent e)
    {
        switch (e.getKeyCode()) 
        {
            case KeyEvent.VK_LEFT:
            break;
            case KeyEvent.VK_RIGHT:
            break;
            case KeyEvent.VK_DOWN:
            break;
            case KeyEvent.VK_UP:
            break;
        }
    }
}

我知道上面的代码不起作用,但这可能是因为我不知道我应该做些什么。

我弄清楚导致覆盖静态方法问题的原因。我已经将扩展JFrame添加到zorx1.java中,这导致了与paint(Graphics g)部分的冲突。所以我把它重命名为make(Graphics g)。希望这不会引起并发症。

我想我应该能够将键盘交互部分移动到另一个类(我希望),但我也有问题。

public class Animation extends JFrame
{
    public int x=100;
    public int y=100;
    public Animation (Graphics g)
    {

        Zorx1.show(g);
        Zorx2.show(g);
        Zorx3.show(g);
    }

    public static void main (String args[])
    {
        Animation application = new Animation (g);
    }

新动画(g);找不到符号g。我无法在任何地方添加它,因为它会导致问题。如何让它识别符号,或者我从一开始就不需要它?

    public Animation ()
    {
        repaint();
    }
    public void paintComponent (Graphics g)
    {
        super.paintComponent(g);
        Zorx1.show(g);
        Zorx2.show(g);
        Zorx3.show(g);
    }

好吧,我想我得到了它,但是super.paintComponent(g);返回一个错误,其中说:“找不到符号 - 方法paintComponent(java.awt.Graphics)。”

修正了它。最后。

1 个答案:

答案 0 :(得分:4)

建议:

  • 阅读Swing图形教程,了解使用Swing绘制的最佳方法。您似乎正在制作大量的图形代码,而这根本不起作用。转到源头并学会正确行事。 Google将帮助您找到这些教程。
  • 例如,您不应直接在JFrame上绘图。
  • 而是使用JComponent(例如JPanel的)paintComponent(...)方法绘制。
  • 避免使用KeyListeners,而是使用Key Bindings。
  • 使用Swing Timer进行动画循环。
  • 你可以在这个网站上找到很多这方面的例子(包括我的),只需要一点点搜索。例如,请查看here

编辑:您的最新代码尝试在JFrame构造函数中执行动画,该构造函数不起作用。如上所述,GUI绘图必须位于JPanel或JComponent的paintComponent(Graphics g)覆盖方法中。在这里,您可以使用JVM传递给paintComponent(Graphics g)方法的Graphics对象。