我需要制作一个鼓乐器。我已经有了背景图像和声音。现在我需要在鼓上添加 4 个透明圆圈来产生 4 种不同的声音。
import javax.swing.*;
import java.awt.event.*;
public class PictureOnClickOneSound
{
public static void main(String args[])
{
// Create a "clickable" image icon.
ImageIcon icon = new ImageIcon("C:\\Users\\apr13mpsip\\Pictures\\Drum2.jpg");
JLabel label = new JLabel(icon);
label.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
sound1.Sound1.play();
System.out.println("CLICKED");
}
});
// Add it to a frame.
JFrame frame = new JFrame("My Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
}
最佳答案
创建一个 map ,其中包含代表图像上某个位置的形状以及单击该位置时要播放的声音文件。像这样的东西:
Map<Shape, Sound> shapes = new Hashmap<Shape, Sound>();
shapes.put(new Ellipse2D.Double(0, 0, 50, 50), soundFile1);
shapes.put(new Ellipse2D.Double(50, 50, 50, 50), soundFile2);
然后在 mouseClicked() 事件中,您需要搜索 map 以查看是否在任何形状中单击了鼠标。像这样的东西:
for (Shape shape: shapes.keySet())
{
if (shape.contains(event.getPoint());
{
Sound sound = shapes.get(shape);
sound.play();
}
}
关于java - 将透明圆圈添加到背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17396498/