我用JAVA写好了一个窗体游戏,怎么加背景音乐?
3个回答
展开全部
复制粘贴,采纳即可。
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Keygame
{
private final static int WIDTH = 800;
private final static int HEIGHT = 600;
public static void main ( String[] args )
{
JFrame jFrame = new JFrame ();
jFrame.setTitle ("指法练习");
jFrame.setBounds (300, 50, WIDTH, HEIGHT);
jFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Mypanel mp = new Mypanel (WIDTH, HEIGHT);
jFrame.add (mp);
Thread thread = new Thread (mp);
thread.start ();
jFrame.addKeyListener (mp);
jFrame.setVisible (true);
}
}
class Mypanel extends JPanel implements Runnable, KeyListener
{
private static final long serialVersionUID = 1L;
int width, height;
int x[] = new int[10];
int y[] = new int[10];
char c[] = new char[10];
int score = 1000;
Image bgImage;
AudioClip christmas = loadSound ("christmas.wav");
public Mypanel ( int width, int height )
{
this.width = width;
this.height = height;
try
{
bgImage = ImageIO.read (new File ("christmas.jpg"));
}
catch (IOException e)
{
e.printStackTrace ();
}
for ( int i = 0; i < 10; i++ )
{
x[i] = (int) ( Math.random () * 800 );
y[i] = (int) ( Math.random () * 600 );
c[i] = (char) ( Math.random () * 26 + 97 );
}
christmas.loop ();
}
public void paint ( Graphics g )
{
super.paint (g);
g.drawImage (bgImage, 0, 0, width, height, this);
g.setFont (new Font (Font.DIALOG, Font.BOLD, 17));
for ( int i = 0; i < 10; i++ )
{
g.setColor (Color.WHITE);
g.drawString (new Character (c[i]).toString (), x[i], y[i]);
g.setColor (Color.green);
g.drawString ("score: " + score, 5, 15);
}
g.dispose ();
}
public void run ()
{
while (true)
{
for ( int i = 0; i < 10; i++ )
{
y[i]++;
if (y[i] > 800)
{
y[i] = 0;
x[i] = (int) ( Math.random () * 600 );
c[i] = (char) ( Math.random () * 26 + 97 );
score -= 10;
}
}
try
{
Thread.sleep (10);
}
catch (Exception e0000)
{
e0000.printStackTrace ();
}
repaint ();
}
}
public AudioClip loadSound ( String filename )
{
URL url = null;
try
{
url = new URL ("file:" + filename);
}
catch (MalformedURLException e)
{}
return JApplet.newAudioClip (url);
}
@Override
public void keyPressed ( KeyEvent e )
{
char keyChar = e.getKeyChar ();
int nowY = -1;
int index = -1;
for ( int i = 0; i < 10; i++ )
{
if (keyChar == c[i])
{
if (y[i] > nowY)
{
nowY = y[i];
index = i;
}
score += 10;
}
}
if (index != -1)
{
y[index] = 0;
x[index] = (int) ( Math.random () * 800 );
c[index] = (char) ( Math.random () * 26 + 97 );
}
}
@Override
public void keyReleased ( KeyEvent e )
{}
@Override
public void keyTyped ( KeyEvent e )
{}
}
展开全部
//有些的代码,如下:
package com.music;
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import javax.swing.JFrame;
public class Music extends JFrame{
File f;
URI uri;
URL url;
Music(){
try{
f = new File("Taylor Swift-Sparks Fly.wav");
uri = f.toURI();
url = uri.toURL(); //解析地址
AudioClip aau;
aau = Applet.newAudioClip(url);
aau.loop(); //循环播放
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[]) {
new Music();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
AudioClip 接口提供了播放音频剪辑的简单抽象。多个 AudioClip 项能够同时播放,得到的声音混合在一起可产生合成声音。在窗口中添加一个实现了AudioClip接口的对象,应该能够实现这个功能,具体做法再看一下API吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询