【问题标题】:How could one draw a semibreve (whole note) in Java Swing?如何在 Java Swing 中画出半短音符(全音符)?
【发布时间】:2021-06-19 07:50:14
【问题描述】:

我正在创建一个类来处理我的音乐培训应用程序中乐谱的创建,该应用程序是使用 Java Swing 构建的。因此,我将 Bravura 字体用于大多数符号,例如高音谱号和临时记号(使用带有 unicode 字符的 Graphics drawString 方法)。

但是,我找不到用这种方法绘制半短音符或整个音符的方法;根据我使用fileformat.info 的研究,当我输入序列"\uD834\uDD5D" 时,我得到一个矩形而不是所需的字符,这应该对应于整个音符。

我的 JFrame 代码如下:

public MusicalNotation() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
    
    JTextArea semibreveTextArea = new JTextArea();
    semibreveTextArea.setBounds(50, 100, 200, 200);
    semibreveTextArea.setFont(new Font("Bravura", Font.PLAIN, 24));
    semibreveTextArea.setText("\uD834\uDD5D");
    contentPane.add(semibreveTextArea);
    semibreveTextArea.setBackground(getBackground());
    semibreveTextArea.setEditable(false);
}

但是,生成的窗口如下所示:

有没有其他字体有这个功能,或者有其他方法来绘制半短句?

非常感谢任何帮助。提前谢谢你。

【问题讨论】:

    标签: java graphics notation


    【解决方案1】:

    Are there any other fonts which have this functionality?。是的,有(几个),其中之一是您已经在使用的字体。

    Bravura 音乐字体已经包含 Semibreve,您不需要绘制任何内容,因为 Bravura 是一种字体。您需要做的就是为您想要的注释、行和间距提供适当的 Unicode 值,当然,"\uD834\uDD5D" 对于显示 Semibreve 注释是有效的,前提是您正在显示字符的文本组件具有 Bravura 字体设置为它(并非所有字体都支持所有这些 Unicode 音乐字符),例如:

    try {
        // Load the "Bravura.otf" font file.
        Font bravura = Font.createFont(Font.TRUETYPE_FONT, new File("Bravura.otf"));
        // Font Size - NEEDED! I believe default is 1. 
        // Set it to what you want but you may find 
        // size 12 too small.
        bravura = bravura.deriveFont(36f); 
        // Set the font to a JTextArea (or whatever).
        jTextArea1.setFont(bravura);
        // Display the Semibreve.
        jTextArea1.setText("\uD834\uDD5D");
    }
    catch (FontFormatException | IOException ex) {
        ex.printStackTrace();
    }
    

    您应该在 JTextAea 中看到一个 Semibreve。

    【讨论】:

    • 您好!非常感谢你的回复。按照您的建议,我实现了问题中的代码 - 但出现了同样的问题。我做错了吗?
    • 如果在 MAC 上,您可能需要使用 Installer 安装 Bravura 字体。
    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多