r/JavaFX • u/Express_Grocery4268 • Mar 30 '24
Help Drawing huge text to a canvas
I'd like some advise on an approach here. I'm trying to create an HexEditor component that is able to process / visualize huge files. Tableview will not work due to the number of rows that need to be created.
There is a HexEditor java library out there written in Swing but I'm having issues getting it to work and I'd like to have a javaFx based solution. In addition it's a great oppertunity to get a bit more familiar with certain aspects.
Just to simplify things I'm creating an array with some dummy data. Then I'm looping through the data and writing it to the canvas as text. Currently my canvas size is based on the size of the array and the font size. So if I have a font that has a height of 20 pixels and I have 200.000 records, then the canvas height is 4.000.000 pixels (assuming one record per row)
Ok, so my logic seems to be working for low array sizes like 100,200,500 but above 1000 it's giving undefined behaviour. Not all rows are printed, background rectangles are 'split' etc, memory errors, etc,etc
The canvas itself is within a Scrollpane. What I am wondering is should I actually create such a big canvas as it's clearly resulting in performance issues? My guess is not...
The alternative I can think of is to create a canvas which has the size of the scrollpane and only draw what is expected to be visible, based on the scrollbar position? For example if the scrollbar is at the top, based on the height of the canvas and height of the font I can calculate how many records should be presented in the canvas. As the scrollbar position is at the top I can count from 0 to the maximum presentable rows and draw those on the canvas. If the scrollbar position is changed, I can calculate what the first record should be and then draw again. This way the canvas is only as big as the screen itself and theorarically I would not run into these undefined issues.
Does the latter approach make any sense?
View all comments
Show parent comment
1
u/Express_Grocery4268 Apr 04 '24
I got the table view fairly working although I do still have some doubts on certain things. When I load a file, initially it will just be a single array of bytes[]. It's quite common for hex editors to allow to change to column count like 8 or 16 etc, so what I'm doing is creating a 2 dimensional observable list containing a custom type. This allows to have a dynamic column count. This works like a charm. This custom type represents a single byte and has some conversions going on for different representations (hex string, ascii string). This means I have 3 observable properties in a single class which basically represent the same value. Probably not optimal as I'll explain below.
The table view exists out of two sections, first sections shows the hex representation, then there's an empty divider column and then the ascii representation. All is based on a single table view and single 2 dimensional array. Depending on which columns are created I either take the the hex string or the asci string of the custom object (cellByte).
First problem is memory usage. For a small 2mb file, the 2 dimensional observable array grows up to around 500mb per loaded file, that's a factor 250.... I can probably reduce that by only having the byte as a class field and don't store the hex and ascii presentation string and instead calculate it on the go. I'd need to see the performance impact of that. It would also improve loading time (time needed to create the array) as these objects are created in the constructor. I've already been able to optimize the 2d array creation, but it's not where I want it to be. Initially it was taking around 4 seconds and I've been able to reduce it to 2 seconds so far.
The other problem would be, once the value in the hex presentation cell is updated, then the corresponding ascii presentation cell is updated as well and vice versa. This is the reason why I precalculated the values so that I could create properties out of it and then bind these to the cells and to the byte property. I'm not familiar with the binding stuff so I need to read up on it. Maybe I just should create two table views based on the same observable array but just handle the presentation differently?
Another thing would be changing how the selection of cells works. In table view you'd always select one or more cells in a rectangle shape, let's say from A1 to B2 would select four cells in a rectangle way. But in hex editors it would select the first A row completely and then B1 and B2. On top of that, the selection of the hex presentation, should be mimicked on the ascii presentation. But I guess the latter would be fairly easy by binding something like a selected property somehow.
Anyhow lots of things to do, learn and discover.