Java - 如何在JTextPane中正确显示ANSI艺术

时间:2014-05-17 19:02:03

标签: java user-interface fonts jtextpane

我在JTextPane中显示ANSI art时遇到问题。

我认为只需选择固定宽度或等宽字体,并通过JTextPane删除StyledDocument SimpleAttributeSet的行距就足以正确显示{ {3}},但在尝试不同的字体和设置后,我意识到它必须不止于此。

我一直在使用some different pieces of ANSI art来比较我的JTextPane,以便查看我的代码是否正确显示DAMN NFO Viewer

我将JTextPane字体设置为DAMN NFO Viewer使用的相同字体(默认情况下,根据其设置 Courier New,8 )通过artTextPane.setFont(new java.awt.Font("Courier New", 0, 8))。但是,即使我通过StyledDocument.setParagraphAttributes(int, int, AttributeSet, boolean)应用行间距设置,我的JTextPane也无法正确显示艺术品。 以下是两个显示差异的屏幕截图:

DAMN NFO Viewer(Courier New,8):

DAMN NFO Viewer Courier New 8

我的JTextPane(Courier New 8)

JTextPane Courier New 8

真正奇怪的是,当我将JTextPane的font-size设置为9时,对齐似乎是正确的,但有点不清晰。放大在艺术作品中形成 g 的角色会在右侧显示蓝色像素,使其看起来不那么尖锐:

我的JTextPane(Courier New 9)

JTextPane Courier New 9

另外我注意到在记事本中打开the piece of digital art会像DAMN NFO Viewer那样显示它。此外JTextPane似乎显示它更小,例如,棺材'其中绘制的字符显得较小。

首先我认为DAMN NFO Viewer正在使用不同版本的字体 Courier New ,所以我挖掘了它的目录并找到了以下注册表文件:

[HKEY_CURRENT_USER\Software\DAMN\DAMN NFO Viewer\NFO]
"FontName"="[built-in] Courier New"
"FontData"=hex:91,00,03,01
"Window"=dword:00000023
"BackgroundColor"=hex:ff,ff,ff,00
"TextColor"=hex:00,00,00,00
"HyperLinkColor"=hex:bf,cd,db,00
"HyperLinkHoverColor"=hex:99,b4,d1,00
"Text"=dword:00000400

这让我觉得它可能会使用设置为"FontData"=hex:91,00,03,01 Courier New 的派生版本。但是我不知道如何在Java中重现这种派生。

有人能告诉我,我试图以JTextPane显示文本文件的方式错误 DAMN NFO Viewer 吗?

修改 添加了一些我正在使用的测试/示例代码,尝试显示字体"正确" (以一种不破坏ANSI艺术的方式)。我使用art.txt内置的GUI构建器" Mantisse"创建了这个测试GUI,所以它可能不是一个完美的例子,但它应该清楚地显示DAMN屏幕截图的不同之处NFO查看器。

package jtextpanetest;

public class JTextPaneTestUI extends javax.swing.JFrame {

/**
 * Creates new form JTextPaneTestUI
 */
public JTextPaneTestUI() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    scrollPane = new javax.swing.JScrollPane();
    panel = new javax.swing.JPanel();
    textPanesSrollPane = new javax.swing.JScrollPane();
    textPane = new javax.swing.JTextPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    panel.setLayout(new java.awt.BorderLayout());

    textPane.setFont(new java.awt.Font("Courier New", 0, 8)); // NOI18N
    textPane.setText("▀▀▀▀▀▀▀▀▀▄▄▄▄\n███████▄▄▄▄ ▀▀▀▄▄\n▄ ▀█▓█████████▄▄ ▀▀█▄\n █  ▓█▓▄ ▀▀█▓███▓▄  ██\n  █ ▒▓▒▓    ▒█▓█▒▓  █▓█\n  █ ░▒░▒█ █ ░▓▒▓░▒█ █▒▓█\n  █ ░░░░▓ █ ░▒░▒░░▓ ▓░▒█\n  █ ▒░▒░▒ █ ▒░░░▒░▒ ▒░░█\n  █ ▓▒▓▒░ █ ▓░▒░▓▒░ ░▒░█\n  █ █▓█▓▀ █ █▒▓▒█▓░ ░▓▒█\n  █ ██▀ ▄▀█ █▓█▓██  ▒██\n  █ █ ▄▀▄█  █████▀  ▓█\n  ░ █ ░ █ ▄██▀▀ ▄▄█ █░\n  ▒▄▄▄▒ ░▄▄▄▄▄▀▀  █ █\n          █ ██▀ ▄▀                   \n          █ █▌▄▀  \n          ░ █ █                  \n          ▒▄▄▄░ \n\n █████████\n███████████\n███████████\n███████████\n█████ █████\n█████ █████\n█████ █████\n█████ █████\n█████ █████\n███████████\n███████████\n ██████████\n     ██████\n███████████\n██████████      ");
    textPanesSrollPane.setViewportView(textPane);

    panel.add(textPanesSrollPane, java.awt.BorderLayout.CENTER);

    scrollPane.setViewportView(panel);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(JTextPaneTestUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new JTextPaneTestUI().setVisible(true);
        }
    });
}


private javax.swing.JPanel panel;
private javax.swing.JScrollPane scrollPane;
private javax.swing.JTextPane textPane;
private javax.swing.JScrollPane textPanesSrollPane;

}

EDIT2: 有人建议我把艺术作为图像渲染,但我需要它是可编辑的,所以这不是我想的选择。任何提示都表示赞赏!

1 个答案:

答案 0 :(得分:3)

这里的主要问题是JTextPane中的文本在行之间的空间太大而呈现。其他小部件也会出现同样的问题。实际间距取决于字体(因此也取决于JVM)。使用Courier 9可以观察到的蓝色模糊可能是由抗锯齿造成的。

您的代码在MacOS X上使用Java 1.7.0_55-b13呈现如下: Original rendering

使用JTextPane,您可以按如下方式减少行间距(在调用JTextPane.setText之前):

javax.swing.text.MutableAttributeSet set = new javax.swing.text.SimpleAttributeSet(textPane.getParagraphAttributes());
javax.swing.text.StyleConstants.setLineSpacing(set, (float)-0.1);
textPane.setParagraphAttributes(set, false);

-0.1值修复了行间距,您可能需要另一个字体或字体大小的其他值。

Removed space between lines

您可能还需要减少跟踪(字母之间的空格):

java.awt.Font font = new java.awt.Font("Courier New", 0, 8);
java.util.Map<java.awt.font.TextAttribute, Object> attributes = new java.util.HashMap<java.awt.font.TextAttribute, Object>();
attributes.put(java.awt.font.TextAttribute.TRACKING, -0.1);
java.awt.Font font2 = font.deriveFont(attributes);
textPane.setFont(font2);

Rendering with tracking