B4J Library TextArea with lined background - Custom View

I've been wanting to do this for a while for a specific purpose, but it has eluded me, until now. So I thought I'd share it in case it's useful.

upload_2018-10-30_13-57-58.png



No other added functionality, the standard TextArea callbacks are replicated. Automatically sizes the background to the pane size and lines to the font size.

Set the line colour in the designer.

I hope you find it useful.
 

Attachments

  • TextArea_LinedBackground.zip
    4.3 KB · Views: 380

stevel05

Expert
Licensed User
Longtime User
Update: added Prompt and Editable Fields to the designer.
 

panagiotisden2

Active Member
Licensed User
Longtime User
Thank you for this, that's awesome

I have a little issue this it, It crashes with very big text size (a lot of lines) due to a bug in javafx when trying to snapshot the canvas (see https://bugs.openjdk.java.net/browse/JDK-8088198)

A workaround is posted in the last post on stackoverflow here but i cant see how to implement this in B4J since i cant get a partial snapshot.

Any suggestions?


Thank you.
 

stevel05

Expert
Licensed User
Longtime User
This is a port of the code, I haven't tried it on a large image as I don't know how large it needs to be to fail. Let me know if it works.

B4X:
Public Sub ExportPng(NJO As JavaObject, FileDir As String, FileName As String, Size As Int)
  
    Sleep(0)
  
    Dim B As JavaObject = NJO.RunMethod("getLayoutBounds",Null)
    Dim W As Int = B.RunMethod("getWidth",Null)
    Dim H As Int = B.RunMethod("getHeight",Null)
  
    Dim Full As JavaObject
    Full.InitializeNewInstance("javafx.scene.image.WritableImage",Array(W,H))
  
    Dim TileWidth As Int = W / Size
    Dim TileHeight As Int = H / Size
    Dim TileWidthD As Double = TileWidth
    Dim TileHeightD As Double = TileHeight
  
    For Col = 0 To Size - 1
        For Row = 0 To Size - 1
            Dim X As Int = Row * TileWidth
            Dim Y As Int = Col * TileHeight
            Dim XD As Double = X
            Dim YD As Double = Y
          
            Dim R2D As JavaObject
            R2D.InitializeNewInstance("javafx.geometry.Rectangle2D",Array(XD,YD,TileWidthD,TileHeightD))
          
            Dim P As JavaObject
            P.InitializeNewInstance("javafx.scene.SnapshotParameters",Null)
          
            P.RunMethod("setViewport",Array(R2D))
          
            Sleep(0)
            Full.RunMethodJO("getPixelWriter",Null).RunMethod("setPixels",Array(X,Y,TileHeight,TileWidth,NJO.RunMethodJO("snapshot",Array(P,Null)).RunMethod("getPixelReader",Null),0,0))
        Next
    Next
  
    Dim Img As Image = Full
    Dim OS As OutputStream = File.OpenOutput(FileDir,FileName,False)
    Img.WriteToStream(OS)
    OS.Close
  
End Sub
 

stevel05

Expert
Licensed User
Longtime User
To Call it
B4X:
ExportPng(LinedOutput1.GetBase,"D:\","test.png",2)
 

panagiotisden2

Active Member
Licensed User
Longtime User
Thanks for your time.
Unfortunately is crashes at this line
B4X:
Full.RunMethodJO("getPixelWriter",Null).RunMethod("setPixels",Array(X,Y,TileHeight,TileWidth,NJO.RunMethodJO("snapshot",Array(P,Null)).RunMethod("getPixelReader",Null),0,0))
(in the first loop of col and row) and the reason being the same bug in javafx.

A possible solution is to make a canvas with only the visible x and y of the textArea. and draw it at the scrolled x position, but i dont know if it will cause a porformance issue.
i will try to code id and post results.

to give you an idea of what im trying to achieve:
Untitled.png
its a HEX compare tool where you can also mark known areas.

Thanks again.
 

stevel05

Expert
Licensed User
Longtime User
Try increasing the size parameter, it defines the number of rows / columns to split the image into. If it's a problem combining the parts back into a full image, then it's a different matter.

How big is the view?
 

stevel05

Expert
Licensed User
Longtime User
Just looked at your post, so the aim is not to save the full image?

If you want to highlight some text, you could use a second canvas and draw an appropriate shape in the correct position with a semi opaque colour to highlight it, taking into account scrolling.
 

panagiotisden2

Active Member
Licensed User
Longtime User
I tried it and it seems no difference whether it's size is 2 or all the way up 80.

The view's size is 535*70555 but it crashes at about 534*8000 or above.

No the aim is not to save the image, its to draw it as a background in the textarea as seen in the image in my previous post which is working great for small sizes but not for big ones.

That's a very good suggestion, i will start working on this.
 
Top