- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Java 项目中有两个类,一个是
反弹
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rebound
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Rebound");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ReboundPanel());
frame.pack();
frame.setVisible(true);
}
}
第二个是ReboundPanel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
public class ReboundPanel extends JPanel
{
private final int WIDTH = 600, HEIGHT = 300;
private final int DELAY = 20, IMAGE_SIZE = 35;
private ImageIcon image1, image2, image3;
private Timer timer;
private int x1, y1, moveX1, moveY1, x2, y2, moveX2, moveY2, x3, y3;
Clip clip;
DataLine.Info info;
public ReboundPanel() throws Exception
{
timer = new Timer(DELAY, new ReboundListener());
image1 = new ImageIcon ("happyFace1.gif");
image2 = new ImageIcon ("happyFace2.gif");
x1 = 0;
y1 = 100;
x2 = 40;
y2 = 0;
moveX1 = moveY1 = 3;
moveX2 = moveY2 = 5;
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setBackground (Color.black);
timer.start();
image3 = new ImageIcon ("fire.gif");
File soundFile = new File("explosion.wav");
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
DataLine.Info info = new DataLine.Info (Clip.class, sound.getFormat());
clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
clip.start();
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
image1.paintIcon (this, page, x1, y1);
image2.paintIcon (this, page, x2, y2);
image3.paintIcon (this, page, x2, y2);
}
private class ReboundListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
x1 += moveX1;
y1 += moveY1;
x2 += moveX2;
y2 += moveY2;
if (x1 <= 0 || x1 >= WIDTH-IMAGE_SIZE)
moveX1 = moveX1 * -1;
if (y1 <= 0 || y1 >= HEIGHT-IMAGE_SIZE)
moveY1 = moveY1 * -1;
if (x2 <= 0 || x2 >= WIDTH-IMAGE_SIZE)
moveX2 = moveX2 * -1;
if (y2 <= 0 || y2 >= HEIGHT-IMAGE_SIZE)
moveY2 = moveY2 * -1;
if (Math.abs(y1-y2) <= IMAGE_SIZE-2 && Math.abs(x1-x2)<= IMAGE_SIZE-2)
{
moveY2 = moveY2 * -1;
moveX2 = moveX2 * -1;
moveY1 = moveY1 * -1;
moveX1 = moveX1 * -1;
x3=x2;
y3=y2-2*IMAGE_SIZE;
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
if (Math.abs(y1-y2) >= 4*IMAGE_SIZE && Math.abs(x1-x2) >= 4*IMAGE_SIZE)
{
x3=-200;
y3=-200;
if (clip.isRunning()) clip.stop();
}
repaint();
}
}
}
当我尝试运行该程序时出现以下错误
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1187)
at ReboundPanel.<init>(ReboundPanel.java:47)
at Rebound.main(Rebound.java:21)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ReboundPanel$ReboundListener.actionPerformed(ReboundPanel.java:99)
at javax.swing.Timer.fireActionPerformed(Timer.java:312)
at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
根据我的代码,我的目标是让两张笑脸在画面中移动,当它们碰撞时,声音和火焰/爆炸的图像应该出现并消失
请帮助我向我的导师展示这个项目......
最佳答案
仅仅因为文件是 WAV 并不意味着 JavaSound 支持它。 WAV 文件是一种容器格式,实际的原始数据(样本)可能采用不同的格式,包括各种位深度的有符号/无符号 PCM,或者它可能采用压缩格式。我找不到 JavaSound 的兼容性表,但 JMF 的兼容性表(这会给你一个好主意)在这里:
http://www.oracle.com/technetwork/java/javase/formats-138492.html
解决方案是使用可以读取当前文件的转换程序(SoX、Adacity、QuickTime、ProTools 等)将 WAV 文件转换为更通用的格式,例如 16 位 PCM。
更准确地说,您可能想要创建一个位深度为 16 位的 WAV 文件(little endian -- 这是默认设置,所以如果您看不到也不要担心)、立体声或单声道应该工作。您可能还需要考虑常见的采样率,例如 44,100 Hz 或 48,000 Hz(现在大多数计算机都应该支持这两种速率,但前者更为常见)。
关于Java Wav 文件错误(javax.sound.sampled.UnsupportedAudioFileException : could not get audio input stream from input file),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943962/
我正在尝试实现具有以下签名的方法: public static Pair, Stream> flatten(Iterator, Stream>> iterator); 该方法的目标是将每种流类型展平
我有两个流从两个不同的 api 获取。 Stream get monthOutStream => monthOutController.stream; Stream get resultOutStre
Stream.of(int[])返回 Stream ,而 Stream.of(String[])返回 Stream . 为什么这两种方法的行为不同?两者都应该返回 Stream和 Stream或 St
我正在使用 rxdart在 dart 中处理流的包。我被困在处理一个特殊的问题上。 请看一下这个虚拟代码: final userId = BehaviorSubject(); Stream getSt
我到处都找遍了,还是没弄明白。我知道你可以用流建立两个关联: 用于支持数据存储的包装器意味着作为消费者和供应商之间的抽象层 数据随着时间的推移变得可用,而不是一次全部 SIMD 代表单指令,多数据;在
考虑下面的代码: List l=new ArrayList<>(); l.add(23);l.add(45);l.add(90); Stream str=l.stream
我有一个大型主干/requirejs 应用程序,我想迁移到 webpack,最新的“webpack”:“^4.27.1”,但我遇到了一个我无法解决的错误。 我一直在阅读 https://webpack
我正在使用 xmpp 开发聊天应用程序,根据我们的要求,我们有三台服务器 Apache Tomcat 7、ejabbered 2.1.11 和 mysql 5.5, to run xmppbot on
我知道如何使用 Java 库,并且我可以编写一些循环来执行我需要的操作,但问题更多,为什么 scala.collection.JavaConverters 中没有任何内容或scala.collecti
我正在尝试创建一个单一的衬里,它应该计算一个非常长的文本文件中的唯一单词。独特的词例如:márya fëdorovna scarlet-liveried,...所以基本上都是非英语词。 我的问题是我的
如果我有以下情况: StreamWriter MySW = null; try { Stream MyStream = new FileStream("asdf.txt"); MySW =
有人可以帮我将以下语句转换为 Java8: 我有一个像这样的 HashMap : private Map, List>> someMap; 我想在java8中转换以下逻辑: private Strin
有人可以帮我将以下语句转换为 Java8: 我有一个像这样的 HashMap : private Map, List>> someMap; 我想在java8中转换以下逻辑: private Strin
考虑两种测试方法parallel()和sequential(): @Test public void parallel() throws Exception { System.ou
我是 NodeJS 的新手,我基本上想做的是通过 HTTP 将 .pdf 上传到我的服务器。我正在使用 POST rquest 来处理 Content-Type multipart/form-data
哪个更好:MemoryStream.WriteTo(Stream destinationStream) 或 Stream.CopyTo(Stream destinationStream)?? 我正在谈
给定一个 Stream,我想创建一个新的 Stream,其中的元素在它们之间有时间延迟。 我尝试使用 tokio_core::reactor::Timeout 和 Stream 的 and_then
我是 Kafka Streams 和 Spring Cloud Stream 的新手,但在将集成相关代码移动到属性文件方面已经阅读了有关它的好东西,因此开发人员可以主要专注于事物的业务逻辑方面。 这里
源代码看起来非常相似:pump , pipe .为什么我要使用一个而不是另一个?一个只是另一个的更好版本吗? 最佳答案 Stream.pipe 现在显然是自 0.3.x 以来的首选方法,因此尽可能尝试
我正在寻找是否有更好的方法来解决我不得不使用这些签名的困境(注意:由于 Spock 测试,T[][] 是必需的,我提供 T[][] 作为数据提供商) 我的方法签名是: public T[][] cr
我是一名优秀的程序员,十分优秀!