add subscripts and a font to a String in graphics on a JFrame

回眸只為那壹抹淺笑 提交于 2019-12-11 01:56:20

问题


I want to Draw a string on my JFrame that has subscripts and a font, I was trying to use an AttributedString but i didn't seem to want to work for whatever reason. It will either display just the font or just the subscripts but not both at once.

private class DrawFormulas extends JComponent
{
    public void paint(Graphics g)
    {

        Graphics2D G2D = (Graphics2D)g;

        G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.setFont(F);

        AttributedString Trig = new AttributedString("a2 + b2 = c2");
        Trig.addAttribute(TextAttribute.FONT, F);
        Trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 1, 2);
        Trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 6, 7);
        Trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 11, 12);
        Trig.addAttribute(TextAttribute.SIZE, F.getSize());
        G2D.drawString(Trig.getIterator(), 170, 75);

    }
}

If somebody can tell me why this doesn't work or has a better way of doing this any help would be appreciated. Thank you


回答1:


I'm not sure why when using both TextAttribute.FONT and TextAttribute.SUPERSCRIPT doesn't merge those attributes, but this answer by @thrashgod gave the idea with your solution.

Separate the Font object into:

  • Font size = 60
  • Font family = Font.SANS_SERIF
  • Font style = Font.PLAIN

As the style is plain we only need font size and font family, so I ended with something like this:

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.TextAttribute;
import java.text.AttributedString;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FormulaDrawer {
    private JFrame frame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new FormulaDrawer()::createAndShowGui);
    }

    private void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        Drawer drawer = new Drawer();

        frame.add(drawer);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    @SuppressWarnings("serial")
    class Drawer extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            AttributedString trig = new AttributedString("a2 + b2 = c2");
            trig.addAttribute(TextAttribute.FAMILY, "Comic Sans MS"); //Change to Font.SANS_SERIF constant
            trig.addAttribute(TextAttribute.SIZE, 20);

            trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 1, 2);
            trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 6, 7);
            trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 11, 12);

            g2d.drawString(trig.getIterator(), 50, 50);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    }
}

However you might have noticed I used a font size of 20 instead of 60, well that's because it seemed too big for me, just change it and you're done, and also I used another font, so you can see you can use any font you want (just be sure to have installed that font or export it within your JAR file)

The general idea was to use the font attributes separately as shown in the above code in these lines:

trig.addAttribute(TextAttribute.FAMILY, "Comic Sans MS");
trig.addAttribute(TextAttribute.SIZE, 20);

And this is how it looks like :)


Also be sure to follow Java naming conventions so your code becomes easier to read for you and us:

  • firstWordLowerCaseVariable
  • firstWordLowerCaseMethod(...)
  • FirstWordUpperCaseClass
  • ALL_WORDS_UPPER_CASE_CONSTANT

Another improvement to your code is: try not extending and overriding JComponent and paint() method respectively, instead extend JPanel or any other component and override it's paintComponent(Graphics g) method and be sure to call super.paintComponent(g) as the first line inside it so you don't break the paint chain.

My main(...) method might look strange to you too because of the Method Reference in Java 8 and the Event Dispatch Thread (EDT) where you should always start your Swing program.


is there a way I can do that with the style of a font?

Yes, there is (as was shown in the link to thrashgod's answer)

  • Bold text is with TextAttribute.WEIGHT
  • Italic text is with TextAttribute.POSTURE

You can find more styles reading the TextAttribute docs

For example:

trig.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
trig.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);



回答2:


If somebody can tell me why this doesn't work

I'm not sure why when using both TextAttribute.FONT and TextAttribute.SUPERSCRIPT doesn't merge those attributes

This is because the FONT TextAttribute is handled in a special way. From the javadoc:

... Normally, all the attributes are examined and used to select and configure a Font instance. If a FONT attribute is present, though, its associated Font will be used. ... Typically, there will be no other attributes in the Map except the FONT attribute.

Javadoc also explains that other attributes can be used together with the FONT attribute, but NOT the following ones: FAMILY, WEIGHT, WIDTH, POSTURE, SIZE, TRANSFORM, SUPERSCRIPT, and TRACKING.

That's why the SUPERSCRIPT attribute was ignored.



来源:https://stackoverflow.com/questions/44764890/add-subscripts-and-a-font-to-a-string-in-graphics-on-a-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!