如何在Java applet 中使用声音和图像?
问题 1:我应该如何构建我的项目,以便最轻松地加载声音和图像文件? 现在,我有一个文件夹:
C:\java\pacman
子目录
C:\java\pacman\src
包含所有代码,并
C:\java\pacman\assets
包含图像和 .wav 文件。 这是最好的结构还是我应该将资产放在其他地方?
问题 2:
在不使用完整路径(例如 C:\java\pacman\assets\something.png
)的情况下引用图像/声音的最佳方法是什么? 如果我使用 getCodeBase()
函数,它似乎引用 C:\java\pacman\bin
而不是 C:\java\pacman\.
我想使用这样一个函数/类,当我在 jar 中编译小程序时以及现在通过 eclipse 测试小程序时它会自动工作。
问题3:如何加载图像/声音? 这就是我现在使用的:
1)对于一般图像:
import java.awt.Image;
public Image getImg(String file)
{
//imgDir in this case is a hardcoded string containing
//"C:\\java\\pacman\\assets\\"
file=imgDir + file;
return new ImageIcon(file).getImage();
}
从此函数返回的图像在 中的
方法。Graphics
类的 drawImage
方法中使用小程序的paint
2) 对于缓冲图像,用于获取子图像并从精灵表加载精灵:
public BufferedImage getSheet() throws IOException
{
return ImageIO.read(new File(img.getPath("pacman-sprites.png")));
}
稍后:
public void loadSprites()
{
BufferedImage sheet;
try
{
sheet=getSheet();
redGhost.setNormalImg(sheet.getSubimage(0, 60, 20, 20));
redGhost.setUpImg(sheet.getSubimage(0, 60, 20, 20));
redGhost.setDownImg(sheet.getSubimage(30, 60, 20, 20));
redGhost.setLeftImg(sheet.getSubimage(30, 60, 20, 20));
redGhost.setRightImg(sheet.getSubimage(60, 60, 20, 20));
}
catch (IOException e)
{
System.out.println("Couldnt open file!");
System.out.println(e.getLocalizedMessage());
}
}
3) 对于声音文件:
import sun.audio.*;
import java.io.*;
public synchronized void play() {
try {
InputStream in = new FileInputStream(filename);
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
} catch (IOException e) {
e.printStackTrace();
}
}
Question 1: How should I structure my project so the sound and images files can be loaded most easily? Right now, I have the folder:
C:\java\pacman
with the sub-directory
C:\java\pacman\src
containing all the code, and
C:\java\pacman\assets
containing the images and .wav files. Is this the best structure or should I put the assets somewhere else?
Question 2:
What's the best way to refer to the images/sounds without using the full path e.g C:\java\pacman\assets\something.png
to them? If I use the getCodeBase()
function it seems to refer to the C:\java\pacman\bin
instead of C:\java\pacman\
.
I want to use such a function/class which would work automatically when i compile the applet in a jar as well as right now when I test the applet through eclipse.
Question 3: How should I load the images/sounds? This is what I'm using now:
1) For general images:
import java.awt.Image;
public Image getImg(String file)
{
//imgDir in this case is a hardcoded string containing
//"C:\\java\\pacman\\assets\\"
file=imgDir + file;
return new ImageIcon(file).getImage();
}
The images returned from this function are used in the drawImage
method of the Graphics
class in the paint
method of the applet.
2) For a buffered image, which is used to get subImages and load sprites from a sprite sheet:
public BufferedImage getSheet() throws IOException
{
return ImageIO.read(new File(img.getPath("pacman-sprites.png")));
}
Later:
public void loadSprites()
{
BufferedImage sheet;
try
{
sheet=getSheet();
redGhost.setNormalImg(sheet.getSubimage(0, 60, 20, 20));
redGhost.setUpImg(sheet.getSubimage(0, 60, 20, 20));
redGhost.setDownImg(sheet.getSubimage(30, 60, 20, 20));
redGhost.setLeftImg(sheet.getSubimage(30, 60, 20, 20));
redGhost.setRightImg(sheet.getSubimage(60, 60, 20, 20));
}
catch (IOException e)
{
System.out.println("Couldnt open file!");
System.out.println(e.getLocalizedMessage());
}
}
3) For sound files:
import sun.audio.*;
import java.io.*;
public synchronized void play() {
try {
InputStream in = new FileInputStream(filename);
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
} catch (IOException e) {
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将它们放在类路径上(将它们与 .class 文件放在一起)并使用 类加载器。
Java:
控制台输出:
Place them on your classpath (place them with your .class files) and load them using the ClassLoader.
Java:
Console output: