nyanchan367
2015-06-14 17:07
采纳率: 33.3%
浏览 1.7k
已采纳

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条回答 默认 最新

  • JonsonJiao 2015-06-15 02: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);
        }
    
    
    点赞 打赏 评论
  • huangshanchun 2015-06-15 00:20

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

    点赞 打赏 评论
  • edouardzyc 2015-06-15 00:51

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

    点赞 打赏 评论
  • 爱学习的东方人 2015-06-21 13:48

    用import导入包中的类

    点赞 打赏 评论

相关推荐 更多相似问题