我试图写入JavaFX画布。我在混合字体方面遇到了问题。我使用黑色和白色。
Font currentFont = new Font("Georgia",125);
FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(currentFont);
canvas.getGraphicsContext2D().setFont(currentFont);
float charWidth = metrics.computeStringWidth(jString);
canvas.getGraphicsContext2D().fillText(jString, 1, 100, charWidth);
float charWidthSum = charWidth;
canvas.getGraphicsContext2D().setFont(new Font("Carlito",60));
metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(new Font("Carlito",60));
charWidth = metrics.computeStringWidth(aString);
canvas.getGraphicsContext2D().fillText(aString, charWidthSum, 100, charWidth);
charWidthSum+=charWidth;
canvas.getGraphicsContext2D().setFont(new Font("Ariel",50));
metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(new Font("Ariel",50));
charWidth = metrics.computeStringWidth(vString);
canvas.getGraphicsContext2D().fillText(vString, charWidthSum,100, charWidth);
charWidthSum+=charWidth;
canvas.getGraphicsContext2D().setFont(new Font("Times",125));
metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(new Font("Times",125));
charWidth = metrics.computeStringWidth(a2String);
canvas.getGraphicsContext2D().fillText(a2String, charWidthSum, 100, charWidth);
//HOW TO IMPLEMENT - to collect the content of a string, keeping font type the same
/*
public void setPixels(int x, int y, int w, int h,
PixelFormat<ByteBuffer> pixelformat,
byte buffer[], int offset, int scanlineStride);
*/
ByteBuffer byteBuffer = null;
byte[] bytes = new byte[0];
PixelFormat<ByteBuffer> pixelFormat = null;
canvas.getGraphicsContext2D().getPixelWriter().setPixels(0,0,0,0,null,bytes, 0,0); //write my new character
//
将是相同的字体,因为我假设我只是设置当前可写字体。如果您看到未实现的部分,我正在考虑尝试使用像素编写器来编写字体。如何让他们进入正确的形式?我使用黑色单调字体
另外,我了解TextFlow和StyledTextArea。 StyledTextArea用于相同的高度文本,TextFlow不能写入画布,只能写入节点。我认为我需要画布功能。