【问题标题】:Letter visualisation in TilePane in JavaFXJavaFX 中 TilePane 中的字母可视化
【发布时间】:2017-04-06 08:02:03
【问题描述】:

我需要做什么:

我尝试编写一些代码来可视化任何字符的每个像素。我决定最好的方法是在 Tile Pane 中将像素显示为矩形。所以这是我需要达到的效果:

我的问题是什么:

我编写了一些代码,通过获取text1 的快照主机并将其保存为WritableImage。然后我使用PixelReader 通过argb 方法读取此图像的每个像素。在循环中,当每个整数 rgb 值大于 TRESHOLD(更亮)时,我添加了具有白色背景的新图块。除非我添加黑色背景的瓷砖。但是我的想法仍然有问题,我得到了这个效果:

这是我的代码:

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);
    }
}

我做错了什么?可能还有其他方法可以达到这种效果吗?

【问题讨论】:

  • 不错的项目 :)

标签: java javafx fonts character letter


【解决方案1】:

你的外循环变量是 x 坐标,内循环变量是 y 坐标。

这意味着您从左到右逐列读取像素。 TilePane 但是要求子列表从上到下逐行排序。

要解决此问题,只需交换循环即可。使用:

for (int y = 0; y < imgHeight; y++) {
    for (int x = 0; x < imgWidth; x++) {
        ...
    }
}

代替

for (int x = 0; x < imgWidth; x++) {
    for (int y = 0; y < imgHeight; y++) {
        ...
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2014-11-16
    • 1970-01-01
    相关资源
    最近更新 更多