关于动画项目,我有3个问题。


程序的结构是否正确(请参见下文)
我的第一张图片(来自数组)的行为异常。有时它会弹出然后消失,然后其余图像可以正确显示。
如何控制音频剪辑何时开始播放。有时听起来像是一根针在记录中撕裂……?


关于计划的结构:
以下是按正确顺序排列的:

初始化:


设置小程序大小。
运行带有睡眠时间的计时器任务
获取声音文件。
从数组中获取图像。
初始化MediaTracker对象,并告诉它“等待所有”图像。
播放声音文件。


开始时(图形g)
1.绘制小程序并加载数组的第一张图像

开始时:
1.检查线程是否为空值,如果不为空,则启动它们

在运行中:
1.使用变量“ iPictureNumber”也使用repaint和Thread.sleep方法按顺序遍历图像。

更新中:
1.再次绘制小程序。



这段代码是我没有使用线程的另一个程序的更新版本,所以我不确定这是否是正确的结构。
我的目标是在此类简单程序中使用最佳做法。如果需要,我可以在zip文件中提供图像和声音。请告知,在此先感谢。
代码如下:

// Java动画项目,其中包含使用线程的图像数组和1个声音文件

 import java.net.*;
 import java.io.*;
 import java.lang.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.Frame;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.MediaTracker;
 import java.applet.Applet;
 import java.applet.AudioClip;
 import java.util.*;

 public class c_TrainAnimation extends Applet implements Runnable
  {
     Image trainAndBush[];
     int totalImages = 17,
     currentImage = 0,             // Set current image array subscript to 0
     sleepTime = 900;
     Image imageCurrent;  // Set to the matching image in "trainAndBush" Array
     MediaTracker myImageTracker;
     Timer myTimer;
     Thread threadTrainAnimation = new Thread(this);
     Thread threadSoundFile = new Thread(this);
     AudioClip mySound;

     public void init()
     {
    setSize(400,400);

        myTimer = new Timer(true);
    myTimer.schedule( new TimerTask()
          {

              public void run()
               { repaint();}

            } // end TimerTask

               ,0,sleepTime);

       mySound = getAudioClip(getDocumentBase(), "onpoint.au");
       trainAndBush = new Image[ totalImages ];

       // Load the array of images into the Applet when it begins executing
        for( int i = 0; i  < trainAndBush.length; i++ )
       {
             trainAndBush[i] = getImage(getDocumentBase(),
              "Hill" + (i + 1) + ".jpg" );

        myImageTracker = new MediaTracker( this );

        // Give the images an ID number to pass to MediaTracker
        myImageTracker.addImage(trainAndBush[i], i);

        // Tell MediaTracker to wait until all images are loaded
          try
    {
        myImageTracker.waitForAll();
    }
    catch(Exception e) {}

         mySound.play();

         }   // end for loop
     }       // end init

     // check threads for null values and then start threads
     public void start()
      {
     if (threadTrainAnimation != null )
        threadTrainAnimation.start();
     if (threadSoundFile != null )
    threadSoundFile.start();
      }

     // Draw the applet with the first image of the array
     public void start(Graphics g)
      {
     g.drawImage(trainAndBush[0],50,50,300,300, this );
     currentImage = 0;
      }

      // Set "imageCurrent"to appropriate "trainAndBush image" in the array and_
         loop through
     public void run()
       {
     int iPictureNumber[] = {0, 1, 2, 3,4,5,6,7,8,9,10,11,12,13,14,15,16};
       while( true )
    {
          for( int i = 0; i < iPictureNumber.length; i++ )
           {
         imageCurrent = trainAndBush[iPictureNumber[i]];
         repaint();
       try
        {
         Thread.sleep( sleepTime );
        }
        catch( InterruptedException e) {}

         }  // end for loop
       }   // end while statement
     }  // end run

     public void update(Graphics g)
      {
    g.drawImage( imageCurrent, 50, 50, 300, 300, this );
      }

    } // end of Applet

最佳答案

我不会使用Applet,而是使用JApplet,它将立即解决许多问题;)

我可能会对RunnablesthreadTrainAnimation使用单独的threadSoundFile,但是过去我对声音没有太多的经验。我看到的问题是,您有两个线程同时访问同一部分代码。这只是一团糟。

您需要弄清楚如何同步声音和动画。

java.util.Timer的使用对于您要实现的目标是不正确的,考虑到您正在运行threadTrainAnimation的事实(这通常与他们做相同的事情一样),这也太过分了。在这种情况下,我可能会摆脱它。将来,最好使用javax.swing.Timer,因为它为Event Dispatching Thread提供了更好的支持

我个人不会以您的方式在init方法中加载我的资源。这将减慢小程序的加载并使用户不高兴。您最好放置一个不错的“加载”图像,并使用Thread进行加载。完成后,使用类似SwingUtilities.invokeLater的东西开始动画。

不要覆盖update方法,而应该覆盖paint方法。

也可能图像的更改速度更快,然后可以绘制它们,您可能还需要对此有所考虑-即run方法可能在调用paint之前执行了很多次

作为建议,我会通读


Concurrency in Swing
Performing Custom Painting
Painting in AWT and Swing
How to make Applets(他们在这里讨论动画时)


如果您对动画很认真,我也会帮您检查一下


TimingFramework和/或
Trident


哪些是Java的动画框架

关于java - 带有MediaTracker,线程和计时器的Java动画小程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11943032/

10-10 11:25