aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes overlapping problem of jpanels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "overlapping problem of jpanels" Watch "overlapping problem of jpanels" New topic
Author

overlapping problem of jpanels

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 498

As i got reply from Wendy Gibbons in JDBC section's topic which i created
worry about other considerations when you have the simplest version working.

Here i try simplest version as per my thought.
I make simple swing program (Just swing no jdbc or hibernate & db),
Name simpleExample.java
Contains 1 JFrame, Array of jpanels.(only 3 jpanels i create by array).
Major code is here., but i modified it as i try in 2-3 days after last post in that question.
If you want to see how it developed,
1. 3 questions regarding JPanel.

Creation of problem:- When program runs 3 panels we can see, 0,1,2.
If we drag & drop panel 0 on panel 2, it will locate beside it because code goes to check_overlap_panel(); & run for loop & check if condition & set as below.
simpledm.comp_panel.setLocation(simpledm.comp_panel.getX()+25, simpledm.comp_panel.getY()+25);

but when we drag & drop panel 1 on panel 2,it will do same for checking ovelaping against panel 2, & setlocation as per above quote,but it forget already that location occupy by panel 0.
Problem panel 1 not try to cross check overlap against panel 0.

If i increase myindex = any number, then it should be try to setlocation of panel as avoid overlap against another panel.
I try my best & then post question...I also try sscce for code

What i do:- Here i try to create 13 boolean flags to check many if-else conditions & for easiness i create 5 functions
Conditions like
1. In check_resolutionboundries() function i check panel should not move beyond systems resolution, so if program run on any monitor which has any resolution, then it will run without trouble. This function i called in mousedragged event.
2. When program runs all integers are 0 so 1st condition is check integer against 0 in function check_int_zero().
3. When function check_int_zero() calls in chek_flags(), to check probability of flags true & false.
4. check_overlap_panel() function contain code for checking panels overlap or not.

5. In set_location() function i try to check probability of flags which true or false by if-else conditions & as per its status i try to set location of jpanel.
& in mouse released event i just call set_location() function.
Here is complete code:-

Can anyone help me to avoid jpanels to overlap each other for n number of times we drag & drop knowingly it overlap to another jpanel?


Millions saw the apple fall, but Newton asked why.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Can anyone help me to avoid jpanels to overlap each other for n number of times we drag & drop knowingly it overlap to another jpanel?

we've shown you, and presumably your code reflects, how to determine if a dragged panel overlaps another panel.

you are the only one that can determine where it goes if it does overlap, no one here can do that.

consider this:
drag a panel, it overlaps
so we move it to the right a bit (or should it go left, up or down)
it now overlaps another panel, so we move it a bit more to the right (or any other direction)
now it's outside the bounds of the frame
where does it go now?

you are the only one who can set the rules, and those rules will be complex.

I could spend a bit of time and write this (because it's not that hard, really), but I know
as soon as I post it, the response will be 'no, I don't want it to end up there' (I know this
because of the numerous times it has happened in other problems).

in its simplest form, once you determine your set of rules:
the panel is dragged
mouseReleased checks against all panels to see if it overlaps another
if it does, your rules moves the panel to a new location
the new location is handled exactly the same as if it was dragged there,
and relocated if it again overlaps another panel.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 498

Thanks Michael Dunn ,
I could spend a bit of time and write this (because it's not that hard, really), but I know
as soon as I post it, the response will be 'no, I don't want it to end up there' (I know this
because of the numerous times it has happened in other problems).


I know that repeatedly increase chain of questions within question so...my reply looks like as you write...
But anyone can see how & where i reach after by study,try, & help.
Now i change my code little bit
I change 2 functions

I am setting panels location (x+25,y+25)
Right now i am trying to do within boundary(no extreme conditions) & only 3-4 panels.
Now for 1st time all panels avoid overlapping each other...eg.
If i change myindex =4, & run, as per my code panels locations

Now i give you incidence which i create

1. when i drag panel 0 on panel 3 & mouse-released
0th panel location (274,150)

2. After it i drag panel 1 on 3 & mouse-released
1st panel location(246,120)

3. After it i drag panel 2 on 3 & mouse-released
2nd panel location (184,59)

Now till here all ok(Whether it right or wrong)
4. Now 0th panel again i drag & try to overlap on 3
its location shows (215,89), but on screen its seen as it overlap on 2nd panel which location(246,120). I dont know why?

I exit eclipse & restart it & run program atleast 5 times,each times default x,y of panels are above table.
each time i try to follow above 4 steps so i get x,y values for panels as below

Bottom line of all trying this..pattern is not fix when program runs...
I am trying all this in only swing so major problems solve in basic swings & db for only saving x,y at the end(at closing program instance) & set location at program start as per db. No further use of db.
Before few days ago i try all possibility with db values also but huge confusion...
So i quit that & do this.
Now i think 'logic code for avoiding overlapping of panels' is might be heart of my program...
2-3 times Michael Dunn told me how to do it..but in actual java codes no proper help..i am not expecting whole program rewrite by someone else..but if somewhere mistake please point out..or any clue!
if just copy-paste above code in previous code in particular location then whole scenario can be clear if anyone try to help me.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 498

Now i just use

in check_overlap_panel() function after where Flag_over = false;

By doing this now if i drag high array index panel on low array index panel then it avoid overlap on low array index panel.
eg. Array of 4 so panels are 0,1,2,3 now before using break i can not avoid following
if i drag panel 2 on 0th panel then it not avoid overlap

After using break it avoid...

But 1 draw back
Condition:-
Step 1. I drag 0th panel on 3rd, it avoid overlap & went to location x+30,y+30 as i written(but i write( x+30,y+30) in for loop it calculate n number of times where n= array index,if i declare outside of for loop that tempx=x+30,tempy =y+30 & in for loop setlocation by tempx,tempy then it not multiply by array index) (Here i use same code as i write before in above post for set location.
Step 2. I drag 1st panel on 3rd,it avoid overlap with 3rd & also 0th.
Step 3. I drag 2nd panel on 3rd,it avoid overlap with 3rd,0th & also 1st.
now i can see diagonal positions set by panels with no overlap...very nice.
Like

Now problem step 4 I drag 0th on 2nd panel then it avoid 2nd & try to setlocation ( x+30,y+30) but at that x,y location already 1st present & due to break statement it not further check with 1st panel.
Now can anyone give me clue? I try myself this much...little help needed...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
your code is way too complex to wade through.

The first code compiles OK, but the 2 changes later on don't compile,
so cannot see what the program does now.

this is a *really rough* draft of one way of doing it
- drag a panel through another, you'll see it 'skip' to a non-overlapping area (tweak required)
- drag, say, 5 panels alongside each other (left-toright) so the gap between
them is less than 25
- now drag a panel (from the left side) so it overlaps the first panel - it will
relocate to after the fifth panel


as i said earlier, it's rough, and you'd need to handle the boundaries etc,
but it has to be a lot less complex than having all those flags.

[EDIT]
code modified to stop the 'skip'
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 498

Thanks Michael Dunn,
By your all post i always get lot to learn & help.
your code is way too complex to wade through.

Yes really, But i just started coding java after huge break,so may be lots of things i forgot/lots of things i never read/use.
But your new code is really very small & less complex...i really wonder why i cant write that myself!
now i just done little change in this for few conditions
1. Frame always full screen ie Extended State.
2. for 1 i think, Frame should be bound with system resolution with some offset,so if user keep any values system's resolution,then also no problem & easy to keep Extended State.

above code written in buildGUI(),above for setting size of panel which inside frame which act as contentpane & following within 'for loop' for setting frame


3. array of panels should not put on any random location by default, its should be place in 1 row with little space between each other & near to 0,0 but not exact 0.So i use

here k & j are declare in class Testing.
4. Now i start to try something for...
you'd need to handle the boundaries etc,

i try inside

Now by above code my panels can avoid to go beyond left side of monitor, when i drag them forcefully to left side...they use

Now i achieve with help of your code
1. Panels avoid overlap each other.
2. Code is less complex.
3. Panels drag with mouse pointer always. ie. nice graphical look.
4. Panels not go beyond vision at left side.

Now remaining 3 directions
up,down & right.
I think i will achieve it soon,but if you find it way before me then i will be very glad to see it..
I never understand by flags why program get mess it self...use of flags it should be easiest way..but not!


[EDIT]
code modified to stop the 'skip'

where you update/edit i can not find it?
My full code is here:-
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632


no, no, no - get it out of mouseDragged()

let the user drag it where he wants, when he stops dragging is the *only* time you should relocate it.
the code should be in a separate method, handled similarly to how I'd earlier checkOverlap()

instead of checking top/bottom/left/right bounds, you might want to consider setting up a 'safe' rectangle.
i.e. you know the bounds when it's outside (say the screen width height is 1200 x 768), so, if
x >= 0, or x <= 1200-25 and
y >= 0, or y <= 768-25
that is the safe area.
create a rectangle(0,0,1175,743) and check if that rectangle contains the point of setLocation's x,y
if true, it's within bounds
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overlapping problem of jpanels