gpt4 book ai didi

Java:图形对齐字符串

转载 作者:行者123 更新时间:2023-12-01 12:57:52 26 4
gpt4 key购买 nike

我目前正在尝试创建一个滚动文本方法,其中将采用String 参数。很快,它将开始从左到右绘制,并随着时间的推移进入新行,这样它就不会从屏幕上绘制出来。我正在使用 FontMetrics 来尝试实现我的目标。

我从主类中的渲染方法将参数传递给我的 RollingText.render(Graphics g, int x, int y) 。在我的 render 方法中,我开始设置 Graphics 字体和颜色,并获取所有 FontMetrics,然后我开始获取不同的内容来自FontMetrics。此外,我进入 for 循环将文本绘制到字符串中。

public void render(Graphics g, int x, int y) {
// text is the field that a grab the index of the string from
// the index is the max part of the string I'm grabbing from,
// increments using the update() method
String str = text.substring(0, index);
String[] words = str.split(" ");

Font f = new Font(g.getFont().getName(), 0, 24);
FontMetrics fmetrics = g.getFontMetrics(f);
g.setColor(Color.white);
g.setFont(f);

int line = 1;
int charsDrawn = 0;
int wordsDrawn = 0;
int charWidth = fmetrics.charWidth('a');
int fontHeight = fmetrics.getHeight();
for (int i = 0; i < words.length; i++) {
int wordWidth = fmetrics.stringWidth(words[i]);
if (wordWidth* wordsDrawn + charWidth * charsDrawn > game.getWidth()) {
line++;
charsDrawn = 0;
wordsDrawn = 0;
}

g.drawString(words[i], x * charsDrawn + charWidth, y + fontHeight * line);

charsDrawn += words[i].length();
wordsDrawn += 1;
}
}

目前,此时一切都会正常工作,但剩下的问题是 drawString 方法中每个单词之间的空格被严重夸大,如下所示:

Rolling Text Demo

行:

g.drawString(words[i], x * charsDrawn + charWidth, y + fontHeight * line);

我目前遇到的唯一问题是找到正确的方法来计算 x 位置。目前,它的间距很大,并且根据字长动态变化,我不知道如何让它看起来至少正常。我已经尝试了与当前定义的整数的不同组合,等等。出现的问题可能包括不正确的间距、不适当的定位和闪烁,以及文本运行在一起。

最后,我的问题是,使用什么样的算法来帮助我正确定位 x 坐标,使文本看起来正确?

最佳答案

我不知道为什么你在简单地添加 FontMetrics#stringWidth 时尝试根据各种不同的变量来计算 x/y 位置或FontMetrics#getHeight在我看来,x/y 值会更简单......

您似乎也在使用 FontMetrics#stringWidth 之间切换并根据“样本”宽度计算单个字符的宽度... wordsDrawn + charWidth ,这使您的计算变得简单......令人困惑。您似乎也存在优先级问题...

不应该x * charsDrawn + charWidthx + (charsDrawn * charWidth)

我实际上无法让您的示例代码重现您的结果,相反,我简化了它......

TextLayout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RenderText {

public static void main(String[] args) {
new RenderText();
}

public RenderText() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private String text;
private int index;

public TestPane() {
text = "A long time ago, in a galaxy far, far, away..."
+ "A vast sea of stars serves as the backdrop for the main title. "
+ "War drums echo through the heavens as a rollup slowly crawls "
+ "into infinity."
+ " It is a period of civil war. Rebel spaceships, "
+ " striking from a hidden base, have won their first "
+ " victory against the evil Galactic Empire."
+ " During the battle, Rebel spies managed to steal "
+ " secret plans to the Empire's ultimate weapon, the "
+ " Death Star, an armored space station with enough "
+ " power to destroy an entire planet."
+ " Pursued by the Empire's sinister agents, Princess "
+ " Leia races home aboard her starship, custodian of "
+ " the stolen plans that can save her people and "
+ " restore freedom to the galaxy...";

index = text.length() - 1;
}

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

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
render(g, 0, 0);
g2d.dispose();
}

public void render(Graphics g, int x, int y) {
// text is the field that a grab the index of the string from
// the index is the max part of the string I'm grabbing from,
// increments using the update() method
String str = text.substring(0, index);
String[] words = str.split(" ");

Font f = new Font(g.getFont().getName(), 0, 24);
FontMetrics fmetrics = g.getFontMetrics(f);
// g.setColor(Color.white);
g.setFont(f);

int fontHeight = fmetrics.getHeight();

int spaceWidth = fmetrics.stringWidth(" ");

int xPos = x;
int yPos = y;
for (String word : words) {
int wordWidth = fmetrics.stringWidth(word);
if (wordWidth + xPos > getWidth()) {
yPos += fontHeight;
xPos = x;
}
g.drawString(word, x + xPos, yPos + fmetrics.getAscent());
xPos += wordWidth + spaceWidth;
}
}
}

}

直到您可以提供实际的 runnable example that demonstrates your problem ,这是我能做的最好的了...

关于Java:图形对齐字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23749317/

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