【问题标题】:How to create an interactive animation?如何制作互动动画?
【发布时间】:2013-03-09 00:23:43
【问题描述】:

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

无论如何。我有三个代码文件。 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 事物中,这导致与绘画(图形 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)。”

修复它。最后。

【问题讨论】:

  • 请重新阅读我的帖子和教程。我已经多次告诉过你这个方法必须驻留在什么类型的类中。

标签: java swing animation keylistener key-bindings


【解决方案1】:

建议:

  • 阅读 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对象。

【讨论】:

  • 如果这是关于 zorx1.java 文件如何用所有这些 x 和 y 绘制的荒谬,我从老师那里收到了那个代码,我猜他很难像那样阅读目的。我想我应该重新表述我的问题,但我的问题是我不知道如何使用 KeyListeners、绑定或其他任何东西,并且通常周围的示例将它们的图形与 keylisteners 和诸如此类的东西保持在同一类中,即老师命令我们不要做的事情。也就是说,我们需要将外星人保存在单独的.java文件中。
  • @JackSnyder:我不确定哪个代码是你的,哪个是你的老师,但我主要谈论的丑陋猜测代码是你的第二堂课,你的动画课看起来到处都是。如果这是您老师的密码,请退出课程。关于分离,我建议您一开始就将键绑定与绘图类一起保存,然后一旦开始工作,重构代码以将它们分成单独的类。这称为Tracer Bullet 编程方法。
  • 嗯,你对丑陋的猜测部分是对的,因为当它归结为那里的任何事情时,我真的不知道我在做什么。当我输入它时,我唯一知道自己在做什么的是 Zorx1.show(g);部分。在那之后,一切都顺风顺水了。无论如何,在谈论分离的事情时,我真的不知道该怎么做,因为我不知道如何做这个键绑定(或监听)的事情,所以我已经卡在第 1 步了。
  • @JackSnyder:这就是上帝发明教程的原因,您应该寻找这些教程,它们以及可以在本网站上找到的示例,例如我在上一个项目符号中放置的链接。
  • 好的,我想我现在就在某个地方。我在框架中添加了一个面板,然后将 KeyListener 添加到该面板中,同时在其中绘制外星人?到目前为止我是对的吗?
猜你喜欢
  • 2016-12-02
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
相关资源
最近更新 更多