我有3个关于我的动画项目的问题。带有MediaTracker的Java动画小程序,线程和定时器
- 是对程序正确的结构(见下文)
- 我的第一图像(从阵列)无法正常运行。有时会弹出,然后消失,然后其他图像正确显示。
- 如何控制音频剪辑何时开始播放。它有时听起来像是一根针正在翻录一张唱片...?
,对于程序的结构: 是在正确的顺序如下:
在INIT:
- 设置applet的大小。
- 运行带睡眠时间的计时器任务
- 获取声音文件。
- 从阵列中获取图像。
- 初始化MediaTracker对象并告诉它“等待所有”图像。
- 播放声音文件。
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
嗨MadProgrammer,感谢您的所有提示。我必须详细看看您的每条评论,因为它们对于我在此计划中的位置有点高级。我还没有与Swing合作过。也许你建议的读数会让它更清晰。再次感谢! – codechick 2012-08-14 00:05:48
不要忘了规则#1 - 玩得开心:) – MadProgrammer 2012-08-14 00:09:44
它运行时很有趣...; d – codechick 2012-08-14 04:23:48