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

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);
        }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 Anaconda navigator updateor一直更新
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序
  • ¥20 Windows 驱动开发版本疑问相关
  • ¥15 MAC 未能打开磁盘映像
  • ¥15 fastcap使用,二维导体输入问题
  • ¥15 hosts修改后不能访问
  • ¥15 关于化学反应速率C++编译问题/FLUENT