gpt4 book ai didi

java - 如何沿着java2d路径绘制阿拉伯语或希伯来语文本

转载 作者:行者123 更新时间:2023-11-30 06:47:57 25 4
gpt4 key购买 nike

有人有一个如何在 java2d 中沿着路径绘制阿拉伯语或希伯来语文本的示例吗?我看这个例子http://www.java2s.com/Code/Java/2D-Graphics-GUI/Drawtextalongacurve.htm但是当我尝试使用阿拉伯语或希伯来语文本时,它不起作用。我有镜面效果。

谢谢。

最佳答案

首先确保您的 Java 代码以 UTF-8 格式保存 enter image description here

然后一个简单的解决方案是反转文本,如下所示:

String s = (new StringBuffer("מה הבעיה אחי?")).reverse().toString();

但这会导致特殊字符出现问题(如 (,{,> ))

正确的方法是使用layoutGlyphVector方法
给它 Font.LAYOUT_RIGHT_TO_LEFT 标志:

GlyphVector gv = font.layoutGlyphVector(g2.getFontRenderContext(), s.toCharArray(), 0, s.length(), Font.LAYOUT_RIGHT_TO_LEFT);

而不是

 GlyphVector gv = font.createGlyphVector(frc, s,);

然后您的文本布局将正确:

Full working code:

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;

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

public class RollingText extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;

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

String s = "מה הבעיה אחי?";
Font font = new Font("LucidaSans", Font.PLAIN, 24);
FontRenderContext frc = g2.getFontRenderContext();
g2.translate(40, 80);

GlyphVector gv = font.layoutGlyphVector(g2.getFontRenderContext(), s.toCharArray(), 0, s.length(), Font.LAYOUT_RIGHT_TO_LEFT);

int length = gv.getNumGlyphs();
for (int i = 0; i < length; i++) {
Point2D p = gv.getGlyphPosition(i);
double theta = (double) i / (double) (length - 1) * Math.PI / 4;
AffineTransform at = AffineTransform.getTranslateInstance(p.getX(), p.getY());
at.rotate(theta);
Shape glyph = gv.getGlyphOutline(i);
Shape transformedGlyph = at.createTransformedShape(glyph);
g2.fill(transformedGlyph);
}
}

public static void main(String[] args) {
JFrame f = new JFrame("RollingText v1.0");
f.getContentPane().add(new RollingText());
f.setSize(600, 300);
f.setVisible(true);
}
}

关于java - 如何沿着java2d路径绘制阿拉伯语或希伯来语文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43345782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com