Topic: "Java Fun" (page 1 of 1)

1
Author Post
sniperkid
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Hello!

I am currently looking into messing around in java and i was wondering if it was possible (maybe a hint on towards the correct route if not) to draw multiple filled rectangles/circles but to make them interactive - ie, clicking on a rectangle would show properties in a mouseover and/or separate static panel. As well as using the mousewheel for zooming/panning etc.
During my research i have been able to draw objects but they draw as an image rather than an object (it would be possible on mouse click to get x/y and workout if an object was selected if no other alternative), i have not investigated the mouseovers or adding other panels. Just want to make sure i'm not wasting my precious time :).

Any advice/examples is welcomed!

Thanks!
private message Website
quangntenemy
groupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmastergroupmaster
Maybe there's a third party lib somewhere but I'm too lazy to look for it haha :P
private message EMail Website
aceldama
groupmastergroupmastergroupmastergroupmaster
the answer you're looking for is mathematical. you can see whether a cursor is inside of a circle by using the equation "r^2=x^2+y^2". therefore, if you have the radius of the circle you drew, you can derive x=sqrt((r^2)-(y^2))... ...so! if you have your x and y mouse offset, you can calculate whether the cursor is inside a circle like so:
dim cX as integer = 100 'circle X (Center)
dim cY as integer = 100 'circle Y (Center)
dim cR as integer = 30  'circle radius

dim mX as integer = 10  'Mouse X
dim mY as integer = 100 'Mouse Y

dim offsX as integer = Math.Abs(mX - cX) 'calculate the mouse offset
dim offsY as integer = Math.Abs(mY - cY)

'WARNING: a square root of a negative number will throw an error so make sure the offsets aren't bigger than the radius first!!
if (offsX <= cR) and (offsY <= cR) then 'Check whether or not it's inside the circle's "rectangle"
    dim Range as integer = Math.Round(Math.Sqrt((cR * cR) - (offsY * offsY)))
    if offsX <= Range then
        'you're inside the circle
    else
        'you're not inside the circle
    endif
endif


sorry for giving you VB code, and apologies if there are any errors. i did this top-down without testing. converting it should not be a problem. it's fairly basic code after all.
Edited by aceldama on 07.04.2014 20:01:30
private message
aceldama
groupmastergroupmastergroupmastergroupmaster
as an afterthought, if you store all these in an array you can have a neat little Z-Order going. so, iterate through the array, breaking once you reach your first range, thus ignoring all other circles below it. further optimisation can be done by pre-calculating one quadrant of the circle (usually the one with all positive x and y-values) and saving them in a separate array. and then just calculating wither the absolute offset is smaller than the range (from 0)
private message
aceldama
groupmastergroupmastergroupmastergroupmaster
hah! damn you quang! i only just saw that the original was posted almost exactly a year ago ROFL
private message

Topic: "Java Fun" (page 1 of 1)

1