java - 将透明圆圈添加到背景图像

标签 java image swing awt java-2d

我需要制作一个鼓乐器。我已经有了背景图像和声音。现在我需要在鼓上添加 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/

相关文章:

jquery - 检查 jquery 插件启动前是否加载了图像

image - 如何在 Flutter 中缩放 ListView 内的图像

java - Java 小程序上的paint() 设置为无限循环。我如何避免我的方法做同样的事情?

java - Editable JCombobox avoid multiple DocumentEvents when the selection change from the popup

java - 将最小化/最大化按钮添加到 JDialog 或 JPanel

java - JRE-9 deprecate browser plug-in - web start (JNLP) 可以直接在 JRE-9 中运行吗?

java - Java 中缺少 return 语句错误

HTML img 扭曲,直到我突出显示图像

java - BHSM-Servlet 不允许浏览器缓存用户名

java - Ant WsImport 在 gradle 中编译生成的 java 文件时抛出错误