0

I have 3 questions about my animation project.

  1. Is the structure of the program correct (see below)
  2. My first image (from an array) isn't behaving properly. Sometimes it pops up and then disappears, and then the rest of the images display correctly.
  3. How do I control when the audio clip starts to play. It also sometimes sounds like a needle is ripping across a record...?

REGARDING THE PROGRAM's STRUCTURE: Are the following in the right order:

IN INIT:

  1. Set applet size.
  2. Run a timer task with a sleep time
  3. Get the sound file.
  4. Get the images from the array.
  5. Initialize the MediaTracker object and tell it to "wait for all" images.
  6. Play the sound file.

IN START(Graphics g) 1. Draw the applet and load the first image of the array

IN START: 1. Check the threads for null values, if not null, start them

IN RUN: 1. Use a variable "iPictureNumber" to iterate through the images in sequential order also using repaint and Thread.sleep methods

IN UPDATE: 1. Draw the applet again.


This code is an updated version of another program I have that didn't use threads, so I'm not sure if this is the correct structure. My goal is to use best practices with this type of simple program. I can provide the images and sound in a zip file if needed. Please advise, thanks in advance. HERE IS THE CODE:

// Java animation project with array of images and 1 sound file using threads

 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
4

1 に答える 1

1

私は使用しませんApplet、使用しますJApplet、それはすぐに何十もの問題を解決します;)

と にはおそらくセパレートRunnablesを使用するでしょうが、過去にサウンドの経験があまりありませんでしたthreadTrainAnimationthreadSoundFile私が見る問題は、2 つのスレッドがコードの一部に同時にアクセスして同じことを行っていることです。これは、物事を混乱させるだけです。

サウンドとアニメーションを同期させる方法を理解する必要があります。

の使用は、java.util.Timer達成しようとしているものに対して正しくありません。threadTrainAnimation実行しているという事実を考えると、一般的に同じことをしているため、やり過ぎです。この場合、私はおそらくそれを取り除くでしょう。将来的には、javax.swing.Timerより良いサポートを提供するため、使用する方が良いEvent Dispatching Thread

私は、個人的には、あなたのようにメソッドに私のリソースをロードしませんinit。これにより、アプレットの読み込みが遅くなり、ユーザーが動揺します。素敵な「読み込み」画像を配置し、を使用しThreadて読み込みを実行することをお勧めします。完了したらSwingUtilities.invokeLater、アニメーションを開始するようなものを使用します。

メソッドをオーバーライドしないでください。代わりにメソッドupdateをオーバーライドする必要があります。paint

画像がペイントされるよりも速く変更されている可能性もあります。これについても考えてみてください。つまり、runメソッドpaintは が呼び出される前に何度も実行される可能性があります。

提案として、私は読み通します

本気でアニメをやるならこちらもチェック

Java のアニメーション フレームワークはどれですか

于 2012-08-13T23:58:17.007 に答える