Android Question Introduction, and help on XUI graphics

wollagt40

Member
Licensed User
Hi everyone, my name is Dave, a Mechanical Engineer by profession, working in the field of Noise and Vibration, and over the years have used programming for writing dynamics programs for my own and others' use. My programming background is linear Fortran and Visual Basic, but it's all been a 'means to an end' exercise so I wouldn't call myself an expert in programming and struggle with this new-fangled object-oriented programming (I wrote my first programs on punched cards you know and handed them to a white-coated 'operator' through a hatch in a wall through which I never actually saw 'the computer' ), but the stuff I write generally involves matrices and complex numbers in programs for eigenvalue extraction for rotor critical speeds, or time step integration for transient analysis for example which all means that it's easy enough to get tied in knots. Recently, I decided that soon to retire, I would keep sharp by doing something with Android, and being pre-Java and having dread of learning it brought me to B4A.
So that brings me to my cry for help on XUI graphics, what am I doing wrong?
I am writing a program that generates a window of data, plots it in a Panel x, time and y, vibration, and carries on to plot the next. Of course, I need to erase the first before I can plot the next, and that is where I am stuck.
Here is the meat of the issue
I have declared as follows in Sub Globals;
Private xui As XUI 'Invoke XUI graphics package
Private xpnlGraph As B4XView
Private Panel1 As B4XView
Private xcvsGraph As B4XCanvas
Private rect1 As B4XRect

Then in Sub Activity_Create
Activity.LoadLayout("HomeScreen")
xpnlGraph = Panel1 'Link Panel1 as container for the Canvas
xcvsGraph.Initialize(xpnlGraph)

Another Sub is called where some calculation occurs and in which the result is plotted piecewise
xcvsGraph.DrawLine(x1(0), x1(2), x2(0), x2(2), xui.Color_RGB(255, 0, 0), 2dip) ' Draw a line
xcvsGraph.Invalidate

The above draws the data fine, but when I want to erase before the next plot I can't seem to make DrawRect below work, even if I put it in the same sub, between Drawline and Invalidate
xcvsGraph.DrawRect(rect1,xui.Color_RGB(0, 0, 255),True,2dip)
so the successive plots just add to what's already there and I can't seem to fix it, so probably a beginners' error.

If the above doesn't encapsulate the issue, please let me know what more is needed.

Thanks in advance
Dave
 

wollagt40

Member
Licensed User
In fact I used the B4X Graphics as a template, except that I created the panel in the designer, whereas the example used CreatePanel in the coding, so I will try that next.
Dave
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Can you post your project as a zip file and I will have a look at it.

1580888554911.png


I am wondering what Panel1 is for.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
can you replace the xui.color part with just 0xff0000ff and see if that works?

it could be that xui.color doesn't have the required alpha level added to the value so it's drawing a transparent blue on the image.


edit: ok, you used that method for the white drawing so it must have set the alpha to 255/FF then.

show the rect code aswell maybe you start erasing a pixel to far (out of the visible area).
 
Upvote 0

wollagt40

Member
Licensed User
Thanks Klaus, attached is the zip you requested.
In answer to your first question Panel1 was intended to be the B4XView panel for the canvas to draw on as per the BX4 Graphics Booklet 'A B4XCanvas is used for drawing over a B4XView panel'
Definitions at Globals, lines 63 to 67
setting up in Activity_Create, lines 101 to 108
Drawing in lines 229 to 232 and238 and 239
At this early stage, scaling is only set up for one case, so if you run it, set left spinner to 400, right spinner to 200 then hit the graph area to run. Not that easy to notice at first, but you will see the first pass points are still visible at LHS centre of graph.
rect1 is defined in Sub Globals Private rect1 As B4XRect

Thanks sorex, confirming your thought I tried the 0xff0000ff but no change.

Dave
 

Attachments

  • test.zip
    12.1 KB · Views: 205
Upvote 0

sorex

Expert
Licensed User
Longtime User
doesn't it have to be

B4X:
    rect1.Initialize(0,0,xpnlGraph.Width,xpnlGraph.Height)

if you add the .left and .top to it you draw outside the visible area.
 
Upvote 0

wollagt40

Member
Licensed User
sorex, yes that's the answer, it needs the relative coords and I was using the screen coords. That seems to work now, in debug mode I can see it work, but it's over too fast to see in release mode. So I can now move forward and review exactly where I put this rectangle draw, thanks sorex.
Klaus, thanks, if you have already looked at the coding and have any general comments, I would be glad to see them.
Dave
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I had a look at your project and have some comments.
1. No need for Panel1, you can use directly xpnlGraph, but you need to change the Click event into Touch event.
2. Instead of defining a pi = 4*ATan(1) variable, you can use directly the B4X constant cPi.
3. Be careful with the layouts. You define the Homescreen layout with specific dimensions 1280 x 800 for a tablet.
The Grapho layout has 320 x 480 dimensions.
Would be interesting to read chapter 2.9 Layout variants in the B4X Visual Designer booklet.
Anchors and AutoScale might also be interesting.
4. No need to invalidate the graph after every DrawLine, invalidate only at the end.
5 You can declare several variables in one line like Dim maxy, sx, ofx, sy, ofy As Float, instsead of one line per variable.
6. Use as much as possible B4XViews, they are cross platform and have some properties which are not directly accessible in B4A.
7. I don't yet understand what exactly you want to draw, currently not much is drawn.

Attached your project with some modifications.
 

Attachments

  • test1.zip
    12.2 KB · Views: 211
Last edited:
Upvote 0

wollagt40

Member
Licensed User
Klaus, excellent exactly the sort of advice I was looking for, it's much appreciated, I will work on it and let you know.
As regards your number 7, yes it's in the early stages yet, and more windows will be added, input force and vibration response time histories and spectra simulating the dynamics of machines under different conditions.
Dave
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You might have a look at DynSim, a program I wrote quite some time now.
And the original program written with Basic4PPC (for Windows Moblie), the predecessor of B4A.
Just to show what can be done with B4A (B4X). I haven't written a B4X version.
 
Last edited:
Upvote 0

wollagt40

Member
Licensed User
Klaus, interesting and well presented. It looks like a digital version of the analogue computers I saw so many years ago and looks highly useful for testing controller circuits. When I started work with gas turbines, the control systems were very basic, having no digital content, and the core of the control system was a set of diodes called a 'least gate' with a few potentiometers and various speed and temperature inputs and a whole rack of solenoid relays, and pneumatic timers for auxiliaries control that could be accommodated on a chip these days. We would use matchsticks to jam a solenoid and slips of card to test the control logic and make sure it was as per design. Later we installed the first GEM controllers in a power station where the machines had been monitored with chart recorders and alarm set-points were contacts that were made by the chart recorder pen arms! Of course, that type of system was open to abuse as I saw that they used matchsticks to inhibit alarms! I left the gas turbine trade and moved into pumps over 40 years ago, so have many stories to tell so I digress :)
Dave
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I had given a course on automatic control in the company I worked for, when I was about getting retired, almost 14 years ago, I am a mechanical engineer too.
It was originally intented for mechanical engineers, but finally there were also electronic engineers attending the course.
To illustrate the theory I gave them the program to let them play with it to see what parameter change has what influence on the system without any risk.
 
Upvote 0

wollagt40

Member
Licensed User
I think your example illustrates that mechanical and electrical disciplines are so intertwined. When I first started work full time, the very first task I was given was to test control panel circuits and logic in newly assembled panels, as I was to discover, this was a precursor to being sent to the middle east and north sea to commission new plant. Throughout my career I have had to learn more and more electrical to do what I do.
 
Upvote 0
Top