将下标和字体添加到JFrame中的图形中的字符串 [英] add subscripts and a font to a String in graphics on a JFrame
问题描述
我想在我的JFrame上绘制一个带有下标和字体的字符串,我试图使用AttributedString,但我似乎并不想因为任何原因而工作。它会显示只是字体或只是下标,但不能一次显示。
私人类DrawFormulas扩展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);
如果有人能告诉我为什么这样不起作用或有更好的方式做这个任何帮助,将不胜感激。谢谢
解决方案我不确定为什么使用 TextAttribute.FONT
和 TextAttribute.SUPERSCRIPT
不会合并这些属性,但此
另外请务必关注 Java命名约定,以便您和我们的代码更易于阅读:
-
firstWordLowerCaseVariable
-
firstWordLowerCaseMethod(...)
-
FirstWordUpperCaseClass
-
ALL_WORDS_UPPER_CASE_CONSTANT
对代码的另一个改进是:尽量不要扩展和重写 JComponent
和 paint()
方法,而不是扩展 JPanel
或任何其他组件并覆盖它是 paintComponent(Graphics g)
方法,并且一定要调用 super.paintComponent(g)
作为它的第一行,这样就不会中断绘制链。
我的 main(...)
方法也许看起来很奇怪,因为 Java 8中的方法参考和 Event Dispatch Thread(EDT)您应该始终开始您的Swing程序。
有没有一种方法可以处理字体样式?
是的,有(如链接到thrashgod的答案所示)
- 粗体文字与
TextAttribu te.WEIGHT
- 斜体文本与
TextAttribute.POSTURE
li>
您可以通过阅读 TextAttribute
docs
<例如: trig.addAttribute(TextAttribute.WEIGHT,TextAttribute.WEIGHT_BOLD);
trig.addAttribute(TextAttribute.POSTURE,TextAttribute.POSTURE_OBLIQUE);
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
解决方案 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);
这篇关于将下标和字体添加到JFrame中的图形中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!