在JavaFX中的TilePane中进行信件可视化 [英] Letter visualisation in TilePane in JavaFX
问题描述
我的问题是什么:
我写了一些代码来抓取 text1
并保存为 WritableImage
。然后我使用 PixelReader
来读取这个图片的每个像素,这是通过 argb
方法来读取的。在循环中,当每个整数rgb值大于TRESHOLD(更亮)时,添加具有白色背景的新瓦片。除非我添加黑色背景的瓷砖。但是,我的想法仍然有问题,我得到这个效果:
有我的代码:
public class Main extends Application {
@Override
public void start(Stage primaryStage)throws Exception {
//创建单个字符串
String letter =a;
//创建新文本
文字text1 =新文字(字母);
//设置字体为文本
text1.setFont(Font.font(Calibri,FontWeight.NORMAL,12));
//将text1保存为可写图像
WritableImage newImg = text1.snapshot(null,null);
//带上字母
int imgHeight =(int)newImg.getHeight();
int imgWidth =(int)newImg.getWidth();
//从newImg创建像素读取器
PixelReader reader = newImg.getPixelReader();
//这是预览图像
ImageView imgPreview = new ImageView(newImg);
//创建tilePane
TilePane tilePane = new TilePane();
//在
中有tilePane的新组合Group display = new Group(tilePane);
// bg color
tilePane.setStyle( - fx-background-color:grey;);
//区块之间的细微差距
tilePane.setHgap(2);
tilePane.setVgap(2);
//设置列数等于图像宽度
tilePane.setPrefColumns(imgWidth);
//设置行数等于图像高度
tilePane.setPrefRows(imgHeight);
//这里我设置容差阈值
int TOLERANCE_THRESHOLD = 0xf0; (int x = 0; x for(int y = 0; y
int argb = reader.getArgb(x,y);
int r =(argb>> 16)& 0xFF的;
int g =(argb>> 8)& 0xFF的;
int b = argb& 0xFF的;
if(r> = TOLERANCE_THRESHOLD
&& g> = TOLERANCE_THRESHOLD
&& b> = TOLERANCE_THRESHOLD){
tilePane.getChildren ()。新增(的createElement(Color.WHITE));
}
else tilePane.getChildren()。add(createElement(Color.BLACK));
$ b //创建新的舞台
Stage stage = new Stage();
//创建新组
组grupa = new Group();
场景场景=新场景(grupa,400,300,Color.GRAY);
grupa.getChildren()。addAll(display);
stage.setScene(scene);
stage.show();
}
private矩形createElement(Color color){
矩形矩形=新矩形(15,15);
//rectangle.setStroke(Color.ORANGE);
rectangle.setFill(color);
返回矩形;
public static void main(String [] args){
launch(args);
$ b 我做错了什么? Maby是采取这种效果的其他方式吗?
你的外循环变量是x坐标,内循环变量是y坐标。
这意味着您从左到右逐列读取像素。 TilePane
但是需要从上到下依次排列子列表。
要解决这个问题交换循环。 (int y = 0; y< imgHeight; y ++){
for(int x = 0; x ...
}
}
而不是
for(int x = 0; x for(int y = 0; y< imgHeight; y ++){
...
}
}
What I need to do:
I trying to write some code for visualise every pixel of any character. I decided best way for it will be show pixels as rectangle in Tile Pane. So this is effect which i need to reach:
What's my problem:
I wrote some code to make it by take snaphost of text1
and save it as WritableImage
. Then i use PixelReader
for read each pixel of this image by argb
method. In loop when each integer rgb value is greater than TRESHOLD (is brighter) I add new tile with white background. Unless I add tile with black background. But something is still wrong with my idea and I get this effect:
There is my code:
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
//Create single letter string
String letter = "a";
//Create new text
Text text1 = new Text(letter);
//Set font for text
text1.setFont(Font.font("Calibri", FontWeight.NORMAL, 12));
//Save text1 as writable image
WritableImage newImg = text1.snapshot(null, null);
//Take bounds of letter
int imgHeight = (int) newImg.getHeight();
int imgWidth = (int) newImg.getWidth();
//Create pixel reader from newImg
PixelReader reader = newImg.getPixelReader();
//This is for preview image
ImageView imgPreview = new ImageView(newImg);
//Create tilePane
TilePane tilePane = new TilePane();
//New group with tilePane inside
Group display = new Group(tilePane);
//Bg color
tilePane.setStyle("-fx-background-color: gray;");
//Small gaps between tiles
tilePane.setHgap(2);
tilePane.setVgap(2);
//Set quantity of columns equals image width
tilePane.setPrefColumns(imgWidth);
//Set quantity of rows equals image height
tilePane.setPrefRows(imgHeight );
//Here I set tolerance treshold
int TOLERANCE_THRESHOLD = 0xf0;
for (int x = 0; x < imgWidth; x++) {
for (int y = 0; y < imgHeight; y++) {
int argb = reader.getArgb(x, y);
int r = (argb >> 16) & 0xFF;
int g = (argb >> 8) & 0xFF;
int b = argb & 0xFF;
if (r >= TOLERANCE_THRESHOLD
&& g >= TOLERANCE_THRESHOLD
&& b >= TOLERANCE_THRESHOLD) {
tilePane.getChildren().add(createElement(Color.WHITE));
}
else tilePane.getChildren().add(createElement(Color.BLACK));
}
}
// Create new stage
Stage stage = new Stage();
//Create new group
Group grupa = new Group();
Scene scene = new Scene(grupa, 400, 300, Color.GRAY);
grupa.getChildren().addAll(display);
stage.setScene(scene);
stage.show();
}
private Rectangle createElement(Color color) {
Rectangle rectangle = new Rectangle(15, 15);
//rectangle.setStroke(Color.ORANGE);
rectangle.setFill(color);
return rectangle;
}
public static void main(String[] args) {
launch(args);
}
}
What I doing wrong? Maby are other ways to take this effect?
Your outer loop variable is the x-coordinate and the inner loop variable is the y-coordinate.
This means you read the pixels column by column left to right. TilePane
however requires the children list to be ordered row by row from top to bottom.
To fix the issue simply swap the loops. Use:
for (int y = 0; y < imgHeight; y++) {
for (int x = 0; x < imgWidth; x++) {
...
}
}
Instead of
for (int x = 0; x < imgWidth; x++) {
for (int y = 0; y < imgHeight; y++) {
...
}
}
这篇关于在JavaFX中的TilePane中进行信件可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!