Board Logo
« Extracting a data from a graphicbox »

Welcome Guest. Please Login or Register.
Jan 18th, 2018, 3:55pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Extracting a data from a graphicbox  (Read 163 times)
Angelo2449
Member in Training
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 58
xx Extracting a data from a graphicbox
« Thread started on: Nov 12th, 2017, 04:41am »

Hi Guys,
I made a graphic in a graphicbox, there are lines, circles and names (text).
I would like, by clicking a name, to extract the relative text.
It's possible?
Thank you.
User IP Logged

Facundo
Board Moderator


member is offline

Avatar




PM

Gender: Male
Posts: 1303
xx Re: Extracting a data from a graphicbox
« Reply #1 on: Nov 12th, 2017, 06:39am »

Place each text position, x and y coordinates,in an array first. The same for the width, and the text itself. Then when you click on the graphicbox, loop through the array until the current mouse x and mouse y position matches ( ie: MouseX>= arrayX and MouseX <=arrayX + width) with one of the items from you array. You'll need a little bit of math. Just an idea.
User IP Logged

cundo aka MSlayer
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Extracting a data from a graphicbox
« Reply #2 on: Nov 12th, 2017, 09:41am »

Quote:
I would like, by clicking a name, to extract the relative text.
It's possible?

Without some preparations - no
Preparations will involve things Facundo said.
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Angelo2449
Member in Training
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 58
xx Re: Extracting a data from a graphicbox
« Reply #3 on: Nov 12th, 2017, 2:51pm »

It can be done, is the technique used by CAD vector programs.
The problem is the zoom management.
Thank you.
User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Extracting a data from a graphicbox
« Reply #4 on: Nov 12th, 2017, 3:40pm »

Have a look at this
JUST BASIC ROTATE FONT
It might not be pretty but it solves zoom problem
(and text rotation to boot)
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Rod
Administrator
ImageImageImageImageImage


member is offline

Avatar

Graphics = Goosebumps!


PM

Gender: Male
Posts: 3151
xx Re: Extracting a data from a graphicbox
« Reply #5 on: Nov 13th, 2017, 09:54am »

You do need to track where the text is and know its area so that you can check what the mouse is over. Andy did some work on knowing if you are inside a polygon. Its on the archive site.

As you have Liberty BASIC an alternative is to check what color the mouse has clicked on. The downside to this technique is that the mouse pointer needs to be on the text not in the general area.

Code:
    Nomainwin
    WindowWidth = 800
    WindowHeight = 600
    UpperLeftX = Int((DisplayWidth - WindowWidth) /2)
    UpperLeftY = Int((DisplayHeight - WindowHeight) /2)
    Graphicbox #main.g, 10, 10, 760, 540
    Open "Get Text Color" for Window as #main
    'Get the device context
    hW=hwnd(#main.g)
    hDC = hDC(hW)


    #main.g "trapclose [quit]"
    #main.g "down ; fill black ; font ariel 24 ;backcolor black ; place 0 50"
    for n= 100 to 105
        #main.g "color 255 255 ";n 'rgb
        #main.g "\";n
    next
    #main.g "when leftButtonDown [check]"
    wait

    [check]
    c=PixelLong(hDC,MouseX,MouseY)
    select case
        case c=100*65536+255*256+255 'bgr
        notice "you selected 100"
        case c=101*65536+255*256+255
        notice "you selected 101"
        case c=102*65536+255*256+255 'bgr
        notice "you selected 102"
        case c=103*65536+255*256+255
        notice "you selected 103"
        case c=104*65536+255*256+255 'bgr
        notice "you selected 104"
        case c=105*65536+255*256+255
        notice "you selected 105"
    end select
    wait

    [quit]
    call ReleaseDC hW,hDC
    close #main
    end


Function PixelLong(hDC, xVar, yVar)
    Open "gdi32"for DLL as #gdi
    CallDLL #gdi, "GetPixel",_
        hDC as Ulong,_
        xVar as Long,_
        yVar as Long,_
        PixelLong as Long
   Close #gdi
End Function

Function hDC(handle)
    CallDLL #user32, "GetDC",_
        handle as Ulong,_
        hDC as Ulong
End Function

Sub ReleaseDC hW, hDC
    CallDLL#user32,"ReleaseDC", _
        hW as Ulong, _
        hDC as Ulong, _
        result as Long
End Sub


 
« Last Edit: Nov 13th, 2017, 09:57am by Rod » User IP Logged

zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 268
xx Re: Extracting a data from a graphicbox
« Reply #6 on: Nov 13th, 2017, 11:12am »

hi,
I add little more to Facundo reply in#1.
if we assume the area occupied by the text is a rectangle ,one should know the height of the text which is proportional to font size.you should know what height/depth it goes from the starting position/coordinates.
one more thing is all characters are not of equal width.
so first thing is to write a program to calculate avg. width and height of each character for given font size and font style.
using the data and formula provided by facundo write another program to get the result you required.
User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Extracting a data from a graphicbox
« Reply #7 on: Nov 13th, 2017, 3:05pm »

Have a look at this.
Code:
maxN=100
dim p(maxN, 4)  'x y w h
dim t$(maxN)
curr=0
ch=16 +4    'add a bit up and down
cw=8

'nomainwin
open "test" for graphics as #gr
#gr "trapclose [quit]"
#gr "font courier_new 10"   'monospaced
#gr "down"
#gr "when leftButtonDown [click]"
wait

[click]
    'MouseX MouseY
    'notice MouseX ;" ";MouseY
    'now, first check if we hit existing rectangle
    'if hit
    x=MouseX
    y=MouseY
    print x;" ";y
    hit = 0
    for i = 1 to curr
        print i;">>";(p(i, 1)<=x);" ";(x <=p(i, 1)+p(i, 3));" ";(p(i, 2)>=y);" ";(y >=p(i, 2)-p(i, 4))
        if (p(i, 1)<=x) and (x <=p(i, 1)+p(i, 3)) _
        and (p(i, 2)>=y) and (y >=p(i, 2)-p(i, 4)) then    '! y reversed
            hit = i: exit for
        end if
    next
    if hit then
        notice "You've pick"+chr$(13)+t$(hit)
        wait
    end if
    'else
    curr=curr+1
    x=MouseX
    y=MouseY
    h=ch
    l=rand(10)+1
    txt$=""
    for i = 1 to l
         txt$=txt$+chr$(asc("A")+rand(26))
    next
    w=cw*(len(txt$)+1)    'add whole char
    p(curr, 1)=x
    p(curr, 2)=y
    p(curr, 3)=w
    p(curr, 4)=h
    t$(curr)=txt$
    print curr;">";x;" ";y;" ";x+w;" ";y-h;" ";txt$
    #gr "place ";x+cw/2;" ";y-4       'add half char, move up a bit
    #gr "\";txt$
    #gr "place ";x;" ";y
    #gr "box ";x+w;" ";y-h   
    #gr "flush"
wait

[quit]
    close #gr
    end

function rand(n)     '0..n-1
    rand = int(rnd(0)*n)
end function
 
« Last Edit: Nov 13th, 2017, 3:07pm by tsh73 » User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Extracting a data from a graphicbox
« Reply #8 on: Nov 14th, 2017, 02:20am »

Quote:
if we assume the area occupied by the text is a rectangle ,one should know the height of the text which is proportional to font size.you should know what height/depth it goes from the starting position/coordinates.
one more thing is all characters are not of equal width.

You can easily get text height for current font with this
Code:
 #g "place 0 0"
 #g "\"
 #g "posxy dummy textHeight"
 print "textHeight=";textHeight 


As for the width, JB2.0b1 supports "stringwidth"
(look into Help | Graphics Commands )

(or you can use monospaced Courier New as I did ;) )
« Last Edit: Nov 14th, 2017, 02:27am by tsh73 » User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Extracting a data from a graphicbox
« Reply #9 on: Nov 14th, 2017, 02:25am »

And just to remind, one not needs LB
Quote:
to check what color the mouse has clicked on

GetPixelValue$ function
« Last Edit: Nov 14th, 2017, 02:26am by tsh73 » User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls