B4J Question [ non-ui ] How can i draw custom Canvas img ?

emexes

Well-Known Member
Licensed User
Did you try it?

My recollection is that you just include the graphics library (jFX, or maybe now it's jXUI) and then you can Dim a Canvas and have access to the associated drawing methods.

Then use .CreateBitmap to get the final pixels as a Bitmap which you can .WriteToStream as JPEG or PNG ready for emailing.

Admittedly, that does sound too easy to be true, so perhaps I am missing something ;-)
 

Cableguy

Expert
Licensed User
Canvas is not a UI element so it can be used in non-ui apps, but the result cannot be attributed to a canvas capable UI element, like a button or imageview, it has to be saved thought using stream if I'm not mistaken
 

klaus

Expert
Licensed User
Are you sure?
What you are saying is for B4A where you can define a Bitmap and set a Canvas for this Bitmap which aren't UI objects.
But in B4J, a Canvas is a Node on its own, therefore a UI object.
 

klaus

Expert
Licensed User
You are right, it works.
Below my test code and project.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
'    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
'    MainForm.Show

    File.WriteString("D:\", "1.png", "")
    Private imv As Image
    imv.Initialize("D:\", "1.png")
    Private cvs As Canvas
    cvs.Initialize("")
    cvs.PrefWidth = 100
    cvs.PrefHeight = 100
    cvs.DrawLine(10, 10, 100, 100, fx.Colors.Red, 2)
    imv = cvs.Snapshot
    Private Out As OutputStream = File.OpenOutput("D:\", "1.png", False)
    imv.WriteToStream(Out)
    Out.Close
End Sub
 

Attachments

Last edited:

Waldemar Lima

Active Member
Licensed User
You are right, it works.
Below my test code and project.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
'    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
'    MainForm.Show

    File.WriteString("D:\", "1.png", "")
    Private imv As Image
    imv.Initialize("D:\", "1.png")
    Private cvs As Canvas
    cvs.Initialize("")
    cvs.PrefWidth = 100
    cvs.PrefHeight = 100
    cvs.DrawLine(10, 10, 100, 100, fx.Colors.Red, 2)
    imv = cvs.Snapshot
    Private Out As OutputStream = File.OpenOutput("D:\", "1.png", False)
    imv.WriteToStream(Out)
    Out.Close
End Sub

this works on "Console Project" application ?
 

Cableguy

Expert
Licensed User
Non-ui and console app behave the same, IDE wise... The major difference is that you can output to screen in text format and prompt the user for text input.
 

Cableguy

Expert
Licensed User
In a VPS, yes, it will.
It's a jar file, so it works in ANY system that is capable of running a jar file!
 

MarkusR

Well-Known Member
Licensed User
hmm, in a linux vps enviroment u can have trouble to make a ui app auto run (start at/after boot)
 
Top