I have 3 questions about my animation project.
- Is the structure of the program correct (see below)
- 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.
- 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:
- Set applet size.
- Run a timer task with a sleep time
- Get the sound file.
- Get the images from the array.
- Initialize the MediaTracker object and tell it to "wait for all" images.
- 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