2012-08-13 45 views
0

我有3个关于我的动画项目的问题。带有MediaTracker的Java动画小程序,线程和定时器

  1. 是对程序正确的结构(见下文)
  2. 我的第一图像(从阵列)无法正常运行。有时会弹出,然后消失,然后其他图像正确显示。
  3. 如何控制音频剪辑何时开始播放。它有时听起来像是一根针正在翻录一张唱片...?

,对于程序的结构: 是在正确的顺序如下:

在INIT:

  1. 设置applet的大小。
  2. 运行带睡眠时间的计时器任务
  3. 获取声音文件。
  4. 从阵列中获取图像。
  5. 初始化MediaTracker对象并告诉它“等待所有”图像。
  6. 播放声音文件。

IN START(图形克) 1.绘制小程序并加载阵列

IN START的第一图像:, 1.检查空值的螺纹,如果不为空,启动它们

在RUN: 1.再次画出的小程序: 1.使用变量“iPictureNumber”通过同样使用重绘和方法的Thread.sleep

更新中按顺序的图像进行迭代。


此代码是另一个程序的更新版本,我没有使用线程,所以我不知道这是否是正确的结构。 我的目标是使用这种简单程序的最佳实践。如果需要,我可以在压缩文件中提供图像和声音。请提前告知,谢谢。 这里是代码:

// 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 

回答

1

我不会用Applet,用JApplet,将解决几十个问题关闭蝙蝠;)

我可能会使用单独的RunnablesthreadTrainAnimationthreadSoundFile,但我没有太多的声音经验。但我看到的问题是,您有两个线程同时访问代码的一部分,执行相同的操作。这只是让事情变得糟糕。

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

使用java.util.Timer对于您尝试达到的目标而言不正确,因为运行threadTrainAnimation,因为它们通常会执行相同的操作。在这种情况下,我可能只是摆脱它。在未来,你使用javax.swing.Timer,因为它提供了Event Dispatching Thread

我更好的支持,个人的更好,不会加载我的资源,在init方法,你现在的样子。这将减慢小应用程序的加载速度并使用户感到不安。你最好放置一个不错的“加载”图像,并使用Thread来执行加载。完成后,使用类似SwingUtilities.invokeLater的动画来启动动画。

请勿覆盖update方法,而应改写paint方法。

这也有可能是图像被改变更快那么他们可以画,你可能要考虑一下这个问题,以及 - 这是run方法paint之前可以执行的次数被称为

作为一个建议,我会通过

如果你是认真的动画,我也想看看

哪些是Java的动画框架

+0

嗨MadProgrammer,感谢您的所有提示。我必须详细看看您的每条评论,因为它们对于我在此计划中的位置有点高级。我还没有与Swing合作过。也许你建议的读数会让它更清晰。再次感谢! – codechick 2012-08-14 00:05:48

+0

不要忘了规则#1 - 玩得开心:) – MadProgrammer 2012-08-14 00:09:44

+0

它运行时很有趣...; d – codechick 2012-08-14 04:23:48