有趣的挑战:
尝试创建一个显示大词形式的应用程序,该应用程序由用户输入一组重复文本。
我使用PixelReader
从字母A
的图片中获取每一点:
//create pixel readers and writer to write to canvas
PixelReader pixreader = imga.getPixelReader();
PixelWriter pixwriter = gc.getPixelWriter();
//point arrays
ArrayList<Integer> xpoints = new ArrayList<>();
ArrayList<Integer> ypoints = new ArrayList<>();
//Read shape of letter by the color of its pixels against a white background
for(int readY=0;readY<imga.getHeight();readY++){
for(int readX=0;readX<imga.getWidth();readX++){
Color color = pixreader.getColor(readX, readY);
if(!color.equals(Color.WHITE) ){
//color = Color.TRANSPARENT;
xpoints.add(readX);
ypoints.add(readY);
}
}
}
我从用户那里得到了混乱的单词:
public void writeOut(String bigWord, String littlewords){
HBox word = new HBox();
word.setPadding(new Insets(10,10,10,10));
String[] letters = bigWord.split("\\B");
littleWordArray = littlewords.split(" ");
inputText = littlewords;
private String getRandomSmallWord(){
int size = littleWordArray.length;
return littleWordArray[rand.nextInt(size)];
}
问题是现在我有所有这些构成A
的点我想在该模型中统一编写文本......
我失败了几次尝试:
for(int ix = 0;ix<xpoints.size();ix+=100){
String nextWord = getRandomSmallWord();
int nextwordlength = nextWord.length();
int currentX = xpoints.get(ix);
int currentY = ypoints.get(ix);
gc.strokeText(nextWord,currentX,currentY);
//gc.strokeText(nextWord,currentX+5,currentY+5);
//gc.strokeText(getRandomSmallWord(),(currentX+nextwordlength),currentY,30.0);
System.out.println("("+currentX+", "+currentY+")");
}
我正在向那些高级开发人员伸出援手,借给我你的智慧!
答案 0 :(得分:2)
所以这不是StackOverflow风格的问题,也许我不应该在这里回答。但无论如何,这里是一个创建由较小单词组成的单词的方法的示例。它没有使用Canvas,而只是使用混合标签。它可能对你没用,但可能会给你一些想法。至少,我希望它可以帮助您编辑和澄清您的问题,以帮助更全面地解释您正在尝试完成的内容以及阻止您完成任务的确切问题。
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.effect.BlendMode;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class WordViewer extends Application {
private static final int NUM_ITERATIONS = 200;
private static final String bigWords =
"Lorem";
private static final String littleWords =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
public void start(Stage stage) {
Label smallLabel = new Label(
IntStream.range(0, NUM_ITERATIONS)
.mapToObj(i -> littleWords)
.collect(Collectors.joining(" "))
);
smallLabel.setStyle(
"-fx-font-size: 9px; " +
"-fx-background-color: black; " +
"-fx-text-fill: white"
);
smallLabel.setWrapText(true);
smallLabel.setPrefWidth(1350);
Label largeLabel = new Label(bigWords);
largeLabel.setStyle(
"-fx-font-size: 400px; " +
"-fx-background-color: black; " +
"-fx-text-fill: white"
);
largeLabel.prefWidthProperty().bind(smallLabel.widthProperty());
largeLabel.prefHeightProperty().bind(smallLabel.heightProperty());
largeLabel.setAlignment(Pos.CENTER);
largeLabel.setBlendMode(BlendMode.MULTIPLY);
StackPane layout = new StackPane(
smallLabel,
largeLabel
);
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.show();
}
}