permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes multiple solution for same result. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "multiple solution for same result." Watch "multiple solution for same result." New topic
Author

multiple solution for same result.

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 492

I & my friend doing one program, 'many small Jpanels can drag-drop in one Big JPanel'.
I am just very bad pretender of java programmer & he is very well set java programmer. My work is as, he is highly professional so not had time to spend such program so, i find out ways & show him.
I spend almost 3-4 months to achieve what we want, he spend only half days(few hours) after reading my code.
Then he show me how to write code smartly.
By doing this exercise, i learn
1. how to write code smartly.
2. How to think smartly when developing logic.
3. Gain confidence for to solve more difficult java problem.

But in my mind question remains why not at firstly i can do smartly. or what i done is waste of time?

Here you might be think all this is only theoretical question not related to java or swing...
For reference I am attaching class Dragger which i develop & he edited
1. Class Dragger which i developed


2ndly he Edited class Dragger as below:-

In my code i check each & very possibility which i think it will occur when we use program. But his code also works as needed.
So my moral is go down...Any guidance regarding this. Why not my code is smart or where i done mistake?


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

Joined: Jun 09, 2003
Posts: 4632
> Why not my code is smart or where i done mistake?

I've explained (in some detail) on numerous occasions that all you need to do
is to create a 'safe' rectangle (very similar to your friend's code) and then check
the position in mouseReleased *not* in mouseDragged i.e. when the dragging has
stopped.

Do you actually know how often mouseDragged fires during a drag?
Put in a System.out.println(..) and you'll find it's a significant number,
so mouseDragged will be continually going through those if/else's - all
for nothing.

To put it bluntly, you've butchered the dragger class.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 492

Thanks Michael Dunn,
I've explained (in some detail) on numerous occasions that all you need to do
is to create a 'safe' rectangle (very similar to your friend's code) and then check
the position in mouseReleased *not* in mouseDragged i.e. when the dragging has
stopped.

I tried to use Rectangle to check overlap or not to go beyond visible boundaries, but when no of small panel increases & when database involve then program get very slow.
In simple swing program never give slow performance but 'with db program give very slow performance.
Do you actually know how often mouseDragged fires during a drag?
Put in a System.out.println(..) and you'll find it's a significant number,
so mouseDragged will be continually going through those if/else's - all
for nothing.

Definitely by putting if-else conditions in mouse drag event, it runs multiple time(some time more than 1000) when in actually when we drag panel one position to another.
But if we check condition (panel should not go out side the visible boundaries) in mouse release then visibly we can see panel go out side the boundaries & then come inside,
Because when we drag mouse beyond visible boundaries that time we not stopping particular panel before cross boundaries, but when we mouse release then immediately if-else works & we see panel come inside visible range. This all look like very unprofessional, simple if we drag any icon in desktop in any operating system, it never seen that icon go out side the visible area.
To put it bluntly, you've butchered the dragger class.


By this forum i learn many thing but, then also i can not implement because i think,i may be slow learner...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: multiple solution for same result.