在我的遊戲對話框結束之後,可怕的彈出窗口會顯示尖叫的噪音。當我點擊按鈕(BPanel)時,圖片顯示「損壞」,直到聲音文件結束播放。尖叫聲結束後,圖片彈出。面板無法在Java Swing中同時顯示聲音文件
是否可以簡單地將兩者同步? 請注意,問題發生在使用Scaryface.png和Sound類的Woc類中。
主要方法:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game {
public static void main(String args[]) {
Woc WineOrCheese = new Woc("Wine or Cheese");
WineOrCheese.applyBackground("TitleBackground.png");
WineOrCheese.applyButton("Play.png", 250, 200, 400, 200);
}
}
婦委會是JFrame的
import static java.lang.System.out;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Woc extends JFrame {
private JFrame window;
private Woc.BPanel background;
private BufferedImage backgroundImg;
final int HEIGHT = 600;
final int WIDTH = 900;
private BufferedImage scaryImg;
public Woc(String text) {
/*********************************
* Sets up window. *
********************************/
window = new JFrame(text);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(null);
window.setVisible(true);
window.setSize(WIDTH, HEIGHT);
window.setLocationRelativeTo(null);
}
public void applyBackground(String ImgName) {
try {
backgroundImg = ImageIO.read(getClass().getResource(ImgName));
} catch (IOException e) {
out.println("No image detected");
}
background = new Woc.BPanel(backgroundImg, 0, 0, WIDTH, HEIGHT);
window.add(background);
background.setBounds(0, 0, WIDTH, HEIGHT);
}
public void applyButton(String ImgName, int x1, int y1, int width,
int height) {
BufferedImage buttonImg = null;
try {
buttonImg = ImageIO.read(getClass().getResource(ImgName));
} catch (IOException e) {
}
Woc.BPanel button = new Woc.BPanel(buttonImg, x1, y1, width, height);
window.add(button);
button.setBounds(0, 0, WIDTH, HEIGHT);
button.addMouseListener(new Clicker());
}
public static void play(String filename) {
try {
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(filename)));
clip.start();
} catch (Exception exc) {
exc.printStackTrace(System.out);
}
}
private class BPanel extends JPanel {
public BufferedImage img;
public int x1, y1, width, height;
public BPanel(BufferedImage img, int x1, int y1, int width, int height) {
super();
this.img = img;
this.x1 = x1;
this.y1 = y1;
this.width = width;
this.height = height;
// this.setOpaque(false);
this.setBackground(new Color(0, 0, 0, 0));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, x1, y1, width, height, null);
}
}
private class Clicker implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0) {
//Dialog of game is here in the form of JOptionPane.
applyBackground("Scaryface.png");
for (int k = 0; k < 10; k++) {
for (int z = 0; z < 10; z++) {
out.println(".");
}
}
Sound scary = null;
try {
scary = new Sound("scary.wav", window);
} catch (Exception e) {
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
Sound類:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;
public class Sound {
public Sound(String s, JFrame win) throws InterruptedException {
Clip play = null;
try {
File in = new File(s);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(in);
play = AudioSystem.getClip();
play.open(audioInputStream);
FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(1.0f); // Reduce volume by 10 decibels.
play.start();
// Loop until the Clip is not longer running.
// We loop this way to allow the line to fill, otherwise isRunning will
// return false
do {
Thread.sleep(10);
} while (play.isRunning());
play.drain();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
ex.printStackTrace();
} finally {
try {
play.close();
} catch (Exception exp) {
}
}
}
}
順便說一句,還有如何使我的遊戲太多的任何提示更容易寫?任何方法或類可以改善和緩解我必須經歷的痛苦? (好吧罰款不真正的痛苦,但它仍然是痛苦的)
沒有while循環,聲音無法播放。我該如何解決這個問題,因爲這是我能夠播放聲音的唯一方式。 –
不要關閉'剪輯' – MadProgrammer
我已經嘗試了很多東西(我讀過教程),但我找不到方法。有什麼建議麼? –