用于JTextArea中控制字符的Java图像 [英] Java image for control characters in JTextArea

查看:180
本文介绍了用于JTextArea中控制字符的Java图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个由2个面板组成的应用程序。一个用于输入文本的JTextArea,另一个用于显示文本。

在这些文本中我有两个特殊字符,即STX / ETX(ASCII码0x02,0x03)。
当我输入这些字符时,它们显示为不可见或其他符号。我想使它们可见和可复制像记事本++中:



任何人都可以指向一个解决方案如何实现与(可能是自定义)JTextArea?
是否可以覆盖字体图像或类似的东西?



对于我的其他看法,我不需要操纵文本我将有HTML将这些字符显示为图像(如果您知道更好的解决方案,请告诉我)。 解决方案

,但它似乎需要CPU资源,并使GUI滞后。

更好的解决方案是在修改过的TrueType字体文件中使用字体字形。您可以为STX,ETX等绘制字形,并将它们添加到字体文件中,但是重现您在Notepadd ++中看到的Scintilla字形还有很多工作要做。

您可以使用另一个简单的方法:


  1. 下载一个像UbuntuMono-R.ttf这样的TTF文件,在FontForge中打开文件,可以看到
  2. 在FontForge中打开另一个类似Digits.ttf的TTF文件,并复制您感兴趣的字符并粘贴到名单的开始。在这里,我粘贴了表示控制字符十六进制代码的圆圈数字。

  3. 生成新的TTF文件,其中包含您的控制字符表示形式。
  4. 将字体文件嵌入到jar中,并将其注册到您的应用程序中:

      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    try(InputStream fontStream = new BufferedInputStream(SwingAppender.class.getResourceAsStream(/ com / jsql / view / swing / resources / font / UbuntuMono-Regular-new.ttf))){
    字体ubuntuFont = Font.createFont(Font.TRUETYPE_FONT,fontStream);
    ge.registerFont(ubuntuFont);
    } catch(FontFormatException | IOException e){
    LOGGER.warn(正在载入字体Ubuntu失败,e);



    $ li
    $ li元素字体并将其应用到JTextComponent:

      public static final Font FONT_UBUNTU = new Font(Ubuntu Mono,Font.PLAIN,12); 


如果有人找到原始的Scintilla字形或自己绘制,将它们添加到FontForge的列表中,您将在文本组件中看到它们(我已经在


I'm writing an application which consists of 2 panels. One JTextArea for entering text and another one for showing text.

In these texts I have two special characters namely STX/ETX (0x02, 0x03 in ASCII). When I enter these characters they are shown as invisible or some other symbol. I would like to make them visible and copyable like in Notepad++:

Could anyone point me to a solution how to achieve that with a (probably custom) JTextArea? Is it possible to overwrite Font images or something like that?

For my other view, where I don't need to manipulate the text I will have html showing these characters as images (if you know of a better solution, please tell me).

解决方案

Replacing control characters with images works, nevertheless it seems to take CPU resources and make the GUI lag.

A better solution is to use font glyph in a modified TrueType font file. You can draw a glyph for STX, ETX, etc and add them to the font file but it's a lot of work to reproduce the Scintilla glyph that you see in Notepadd++.

You can use another simple method:

  1. Download a TTF file like UbuntuMono-R.ttf, open the file in FontForge, you can see a list of all the native glyph and the empty control characters at the start of the list.
  2. Open another TTF file like Digits.ttf in FontForge and copy characters you are interested in and paste them at the start of the list. Here I pasted the circled numbers representing Hexadecimal codes of control characters.
  3. Generate the new TTF file which contains your control characters representation.
  4. Embbed the font file in your jar and register it in your application:

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try (InputStream fontStream = new BufferedInputStream(SwingAppender.class.getResourceAsStream("/com/jsql/view/swing/resources/font/UbuntuMono-Regular-new.ttf"))) {
        Font ubuntuFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
        ge.registerFont(ubuntuFont);
    } catch (FontFormatException | IOException e) {
        LOGGER.warn("Loading Font Ubuntu failed", e);
    }
    

  5. Instanciate the font and apply it to a JTextComponent:

    public static final Font FONT_UBUNTU = new Font("Ubuntu Mono", Font.PLAIN, 12);
    

The process will be identical if someone find the original Scintilla glyph or draw it themself, add them to the list in FontForge and you will see them in your text component (I have tested and drawn the control character EOT pixel by pixel on http://fontstruct.com, it was OK in Swing but I'm too lazy to draw the rest).

这篇关于用于JTextArea中控制字符的Java图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆