JAVA编程eclipse里图形面板怎么插入音乐
在线等大神告诉在哪里插。。。插得内容是什么~~~拜托了!谢谢大神!代码如下:packagemmzml;importjava.awt.Color;importjava.aw...
在线等大神告诉在哪里插。。。插得内容是什么~~~拜托了!谢谢大神!
代码如下:
package mmzml;
import java.awt.Color;
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 javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Keygame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jFrame = new JFrame();
jFrame.setTitle("指法练习");
int width = 800, height = 600;
jFrame.setBounds(300, 50, width, height);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Mypanel mp = new Mypanel(width, height);
// mp.setBackground(Color.white ) ;
jFrame.add(mp);
Thread thread = new Thread(mp);
thread.start();
jFrame.addKeyListener(mp);
jFrame.setVisible(true);
}
}
class Mypanel extends JPanel implements Runnable, KeyListener {
int width, height;
int x[] = new int[10];
int y[] = new int[10];
char c[] = new char[10];
int score = 1000;
Image bgImage;
public Mypanel(int width, int height) {
this.width = width;
this.height = height;
try {
bgImage = ImageIO.read(new File("src/mmzml/1/1.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);
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(bgImage, 0, 0, width, height, this);
for (int i = 0; i < 10; i++) {
g.setColor(Color.red);
g.drawString(new Character(c[i]).toString(), x[i], y[i]);
g.setColor(Color.green);
g.drawString("分数" + score, 5, 15);
}
}
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();
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
char keyChar = e.getKeyChar();
System.out.println("按下的是" + keyChar);
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) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
} 展开
代码如下:
package mmzml;
import java.awt.Color;
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 javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Keygame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jFrame = new JFrame();
jFrame.setTitle("指法练习");
int width = 800, height = 600;
jFrame.setBounds(300, 50, width, height);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Mypanel mp = new Mypanel(width, height);
// mp.setBackground(Color.white ) ;
jFrame.add(mp);
Thread thread = new Thread(mp);
thread.start();
jFrame.addKeyListener(mp);
jFrame.setVisible(true);
}
}
class Mypanel extends JPanel implements Runnable, KeyListener {
int width, height;
int x[] = new int[10];
int y[] = new int[10];
char c[] = new char[10];
int score = 1000;
Image bgImage;
public Mypanel(int width, int height) {
this.width = width;
this.height = height;
try {
bgImage = ImageIO.read(new File("src/mmzml/1/1.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);
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(bgImage, 0, 0, width, height, this);
for (int i = 0; i < 10; i++) {
g.setColor(Color.red);
g.drawString(new Character(c[i]).toString(), x[i], y[i]);
g.setColor(Color.green);
g.drawString("分数" + score, 5, 15);
}
}
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();
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
char keyChar = e.getKeyChar();
System.out.println("按下的是" + keyChar);
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) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
} 展开
1个回答
推荐于2017-09-21
展开全部
已完善,复制粘贴小修改即可 ps: 图片,音乐都放在项目根目录即可
import java.applet.AudioClip;
import java.awt.Color;
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
{
public static void main ( String[] args )
{
JFrame jFrame = new JFrame ();
jFrame.setTitle ("指法练习");
int width = 800, height = 600;
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.play ();
}
public void paint ( Graphics g )
{
super.paint (g);
g.drawImage (bgImage, 0, 0, width, height, this);
for ( int i = 0; i < 10; i++ )
{
g.setColor (Color.red);
g.drawString (new Character (c[i]).toString (), x[i], y[i]);
g.setColor (Color.green);
g.drawString ("分数" + score, 5, 15);
}
}
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 ();
System.out.println ("按下的是" + keyChar);
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 )
{}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询