简体   繁体   English

如何在 Java 小程序中使用声音和图像?

[英]How to use sound and images in a Java applet?

Question 1: How should I structure my project so the sound and images files can be loaded most easily?问题 1:我应该如何构建我的项目,以便最轻松地加载声音和图像文件? 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.包含图像和.wav 文件。 Is this the best structure or should I put the assets somewhere else?这是最好的结构还是我应该把资产放在其他地方?

Question 2:问题2:

What's the best way to refer to the images/sounds without using the full path eg C:\java\pacman\assets\something.png to them?在不使用完整路径的情况下引用图像/声音的最佳方法是什么,例如C:\java\pacman\assets\something.png给他们? If I use the getCodeBase() function it seems to refer to the C:\java\pacman\bin instead of C:\java\pacman\ .如果我使用getCodeBase() function 它似乎指的是C:\java\pacman\bin而不是C:\java\pacman\ .java\

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.我想使用这样的函数/类,当我在 jar 中编译小程序时以及现在通过 eclipse 测试小程序时,它会自动工作。

Question 3: How should I load the images/sounds?问题 3:我应该如何加载图像/声音? This is what I'm using now:这就是我现在正在使用的:

1) For general images: 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();
}

The images returned from this function are used in the drawImage method of the Graphics class in the paint method of the applet.从这个function返回的图像在小程序的paint方法中的Graphics class的drawImage方法中使用。

2) For a buffered image, which is used to get subImages and load sprites from a sprite sheet: 2)对于缓冲图像,用于获取子图像并从精灵表加载精灵:

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: 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();
    }

}

Place them on your classpath (place them with your.class files) and load them using the ClassLoader .将它们放在您的类路径中(将它们与您的 .class 文件一起放置)并使用ClassLoader加载它们。

Java: 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:控制台 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM