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条)

报告相同问题?

悬赏问题

  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。