2 nyanchan367 nyanchan367 于 2015.06.15 01:07 提问

java怎么调用其它的类「新人,请多包涵」

package v3;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;

public class exam extends Frame {

private Image img = null;
private Image img2 = null;
private Image img3 = null;
int x = 500;
int y = 400;

String hurl;

AudioClip audio;


public static void main(String[] args) {
    new exam();

}

public exam() {
    this.setSize(1366,768);
    this.setLocation(100,100);

    img = Toolkit.getDefaultToolkit().createImage("./photos/car.jpg");
    img3 = Toolkit.getDefaultToolkit().createImage("./photos/car.jpg");
    img2 = Toolkit.getDefaultToolkit().createImage("./photos/car.jpg");

    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    this.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_RIGHT)
            x = x + 5;
            else if(e.getKeyCode() == KeyEvent.VK_LEFT)
            x = x -5;
            else if(e.getKeyCode() == KeyEvent.VK_UP)
            y = y -5;
            else if(e.getKeyCode() == KeyEvent.VK_DOWN)
            y = y +5;
            repaint();
        }
    });
    String hurl;
    AudioClip audio;

    this.setVisible(true);
}


public void paint(Graphics g) {
    g.drawImage(img2,100,400,200,200, this);
    g.drawImage(img3,x,y,200,200, this);
    g.drawImage(img,1200,400,100,200,this);
}

}

package v3;

import java.awt.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
public class AudioPlay extends Applet{

String hurl;

AudioClip audio;

public void init()  {
    resize(300,100);
    if(hurl==null)  {
        hurl="audio/nocompleted.au";
    }
    try  {
        audio = getAudioClip(new URL(getDocumentBase(),hurl));
    }  catch(Exception e)  {   }
    this.setBackground(Color.lightGray);
    audio.play();           

}

}

希望能在控制图片移动的同时播放音频,怎么实现呢?

4个回答

tongyi55555
tongyi55555   2015.06.15 10:32
已采纳

可以直接在SWING加音频的,如下:

public exam() {
        this.setSize(1366, 768);
        this.setLocation(100, 100);

        img = Toolkit.getDefaultToolkit().createImage("./photos/rabit.jpg");
        img3 = Toolkit.getDefaultToolkit().createImage("./photos/rabit.jpg");
        img2 = Toolkit.getDefaultToolkit().createImage("./photos/rabit.jpg");

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        this.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_RIGHT)
                    x = x + 5;
                else if (e.getKeyCode() == KeyEvent.VK_LEFT)
                    x = x - 5;
                else if (e.getKeyCode() == KeyEvent.VK_UP)
                    y = y - 5;
                else if (e.getKeyCode() == KeyEvent.VK_DOWN)
                    y = y + 5;
                repaint();
            }
        });
        if (hurl == null) {
            hurl = "./audio/BEEP1.wav";
        }
        try {
            URL url = new URL("file:" + hurl);
            audio = Applet.newAudioClip(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                audio.play();
            }
        }.start();
        this.setVisible(true);
    }

tongyi55555
tongyi55555 怎么会崩溃呢?怎么回事?有帮助的话记得采纳
2 年多之前 回复
nyanchan367
nyanchan367 刚才自己折腾一下,可以了,谢谢了
2 年多之前 回复
nyanchan367
nyanchan367 能完整一点吗?我贴过去直接崩了,谢谢了
2 年多之前 回复
huangshanchun
huangshanchun   2015.06.15 08:20

同时播放音频,要用到多线程技术。

edouardzyc
edouardzyc   2015.06.15 08:51

播放音乐的方法 新建个现成来启动

u013176244
u013176244   2015.06.21 21:48

用import导入包中的类

Csdn user default icon
上传中...
上传图片
插入图片