如何在Java applet 中使用声音和图像?

发布于 2024-07-14 21:26:42 字数 2128 浏览 5 评论 0原文

问题 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂归处 2024-07-21 21:26:42

将它们放在类路径上(将它们与 .class 文件放在一起)并使用 类加载器

Java:

package mypackage;

public class MyClass {
    public static void main(String[] args) {
        java.net.URL url = MyClass.class.getResource("/mypackage/image.gif");
        System.out.println(url);
    }
}

控制台输出:

C:\resource>dir /b /s
C:\resource\bin
C:\resource\src
C:\resource\bin\mypackage
C:\resource\bin\mypackage\image.gif
C:\resource\bin\mypackage\MyClass.class
C:\resource\src\mypackage
C:\resource\src\mypackage\MyClass.java

C:\resource>java -cp bin mypackage.MyClass
file:/C:/resource/bin/mypackage/image.gif

Place them on your classpath (place them with your .class files) and load them using the ClassLoader.

Java:

package mypackage;

public class MyClass {
    public static void main(String[] args) {
        java.net.URL url = MyClass.class.getResource("/mypackage/image.gif");
        System.out.println(url);
    }
}

Console output:

C:\resource>dir /b /s
C:\resource\bin
C:\resource\src
C:\resource\bin\mypackage
C:\resource\bin\mypackage\image.gif
C:\resource\bin\mypackage\MyClass.class
C:\resource\src\mypackage
C:\resource\src\mypackage\MyClass.java

C:\resource>java -cp bin mypackage.MyClass
file:/C:/resource/bin/mypackage/image.gif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文