我用JAVA写好了一个窗体游戏,怎么加背景音乐?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我用JAVA写好了一个窗体游戏,怎么加背景音乐?相关的知识,希望对你有一定的参考价值。
复制粘贴,采纳即可。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 )
你看这个 我复制这部分进去就这样了,怎么解
鼠标放在错误处,看提示操作即可
参考技术A //有些的代码,如下: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();
参考技术B AudioClip 接口提供了播放音频剪辑的简单抽象。多个 AudioClip 项能够同时播放,得到的声音混合在一起可产生合成声音。在窗口中添加一个实现了AudioClip接口的对象,应该能够实现这个功能,具体做法再看一下API吧
以上是关于我用JAVA写好了一个窗体游戏,怎么加背景音乐?的主要内容,如果未能解决你的问题,请参考以下文章
我用JAVA的swing创建了一个窗口程序,拖了一个jTable控件在窗体上,怎样把数据库中的内容显示在Table上?
我用sqlserver写好了一个存储过程 怎么样才能返回出一个搜索语句的结果集给c#