gpt4 book ai didi

java - 如何以对齐方式输出文本

转载 作者:行者123 更新时间:2023-12-02 05:34:25 25 4
gpt4 key购买 nike

我正在尝试为 libgdx 创建一个组件,它可以以与 html 使用 <p align=”justify”> ... </p> 相同的方式输出文本。 .

我的想法是添加一些自定义组件,可以通过调整组件的相对 x 和 y 坐标来实现此目的。

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.Align;

public class Paragraph extends WidgetGroup {

private Label space;

public Paragraph(String text, float width, LabelStyle style) {

super();
setWidth(width);

this.space = new Label(" ", style);
this.space.pack();

String[] words = text.split(" ");
for (String word : words) {

Label label = new Label(word, style);
label.pack();

addActor(label);
}
}

public void layout () {

float size = 0;
List<Actor> elements = new ArrayList<Actor>();

float x = getX(Align.topLeft);
float y = getY(Align.topLeft);

for (Actor actor : this.getChildren().items) {

if (actor != null) {
if (elements.isEmpty()) {
elements.add(actor);
size = actor.getWidth();

} else {

if (size + space.getWidth() + actor.getWidth() <= this.getWidth()) {

elements.add(actor);
size += (space.getWidth() + actor.getWidth());

} else {

float spacing = space.getWidth() + ((getWidth() - size) / elements.size());

Actor element = elements.get(0);
element.setPosition(x, y, Align.topLeft);

x += element.getWidth();

for (int i = 1; i < elements.size(); i++) {
element = elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);

x += (spacing + element.getWidth());
}

// new line
elements.clear();
x = getX(Align.topLeft);
y += (this.space.getHeight() * 1.5);

elements.add(actor);
size = actor.getWidth();
}
}
}
}

if (elements.isEmpty() == false) {

float spacing = space.getWidth();

Actor element = elements.get(0);
element.setPosition(x, y, Align.topLeft);

x += element.getWidth();

for (int i = 1; i < elements.size(); i++) {
element = elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);

x += (spacing + element.getWidth());
}
}
}
}

我的问题是,我使用 getX(Align.topLeft) 和 getY(Align.topLeft) 检索的 x 和 y 坐标始终返回 (0,0)而不是舞台上的真实坐标。

组件结构如下所示:

Stage
+ Container
+ Table
+ ScrollPane
+ Table
+ Table
+ Paragraph
+ Paragraph
+ Paragraph
+ Paragraph
+ Image

所以最终结果是不同段落中包含的所有文本都绘制在彼此之上。不在位置 (0,0),而是在周围表格内的同一位置。

最佳答案

我自己解决了。

似乎重写布局方法是一个坏主意。我现在从构造函数中调用不同的方法。

而且我搞乱了 x 和 y 坐标,所以我不得不重写该方法。

诗:我也调整了问题,当我找到解决方案时,它是与小部件组的 x 和 y 坐标无关。

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.Align;

public class Paragraph extends WidgetGroup {

private static final class RowData {

final List<Actor> elements;
final float size;
final boolean lastRow;

RowData(List<Actor> elements, float size, boolean lastRow) {
this.elements = elements;
this.size = size;
this.lastRow = lastRow;
}

static RowData createNewRow(List<Actor> elements, float size) {
return new RowData(elements, size, false);
}

static RowData createLastRow(List<Actor> elements, float size) {
return new RowData(elements, size, true);
}
}

private Label space;

public Paragraph(String text, float width, LabelStyle style) {

super();
setWidth(width);

this.space = new Label(" ", style);
this.space.pack();

String[] words = text.split(" ");
for (String word : words) {

Label label = new Label(word, style);
label.pack();

addActor(label);
}

arrangeActors();
}

private void arrangeActors() {

float size = 0;
float height = space.getHeight();

List<RowData> rows = new ArrayList<RowData>();
List<Actor> elements = new ArrayList<Actor>();

Actor[] actors = this.getChildren().begin();

for (Actor actor : actors) {

if (actor != null) {
if (elements.isEmpty()) {

elements.add(actor);
size = actor.getWidth();

} else if (size + space.getWidth() + actor.getWidth() <= this.getWidth()) {

elements.add(actor);
size += (space.getWidth() + actor.getWidth());

} else {

rows.add(RowData.createNewRow(elements, size));

elements = new ArrayList<Actor>();
height += space.getHeight();

elements.add(actor);
size = actor.getWidth();
}
}
}

this.getChildren().end();

rows.add(RowData.createLastRow(elements, size));

height += space.getHeight();
setHeight(height);

float y = height;

for (RowData data : rows) {

float spacing = space.getWidth();
if (data.lastRow == false) {
spacing += ((getWidth() - data.size) / data.elements.size());
}

Actor element = data.elements.get(0);
element.setPosition(0, y, Align.topLeft);

float x = element.getWidth();

for (int i = 1; i < data.elements.size(); i++) {

element = data.elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);
x += (spacing + element.getWidth());
}

y -= this.space.getHeight();
}
}

@Override
public float getPrefWidth () {
return getWidth();
}

@Override
public float getPrefHeight () {
return getHeight();
}
}

关于java - 如何以对齐方式输出文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183030/

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