aspose file tools*
The moose likes Java in General and the fly likes Funky issue with intersection and rectangles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Funky issue with intersection and rectangles" Watch "Funky issue with intersection and rectangles" New topic
Author

Funky issue with intersection and rectangles

Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356









Okay so basically whenever it is enabled it should snap to the shelf if it's 10px above the shelf. Everything works fine on 1 shelf, but when I move over to the other shelves if for some reason sticks to it, and when I left up, left, or right it will shoot the height of the rectangle to ridiculously big(the more of I move the mouse, the larger it gets). I don't know why or how since my height isn't changing anywhere.... At first when I checked out the debugger in Netbeans it showed that my face's x,y,w,h weren't even set so I did instead of the getX/getY must be inherited from the Rectangles, so when I checked it out sure enough the height is being changed, why I do not know...


Any help would be appreciated, thanks,

~JO

EDIT: changed

to

But now it will only snap to the first or last shelf(depending if I start from shelf[j].length and do j--, or start from 0 and j++)... Not sure why it changed the height, and I'm not sure if it will continue to do so... Not sure why it's not accepting any other shelf either from that 1 change...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Jay Orsaw wrote:Any help would be appreciated, thanks,

First, could you please edit your code and break up those enormously long lines.
1. It makes your code very difficult to read.
2. It screws up the windowing software here.

Thanks

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Winston Gutkowski wrote:
Jay Orsaw wrote:Any help would be appreciated, thanks,

First, could you please edit your code and break up those enormously long lines.
1. It makes your code very difficult to read.
2. It screws up the windowing software here.

Thanks

Winston


Well Idk how a 40 line code snippet will break the windowing software/ hard to read, when I've seen a lot more before. 2 I split it up, good luck trying to read that one method now.... 3. I have seen no issue myself, are you on a mobile device? That would not be my issue.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

There's a computing principle called "DRY" -- "Don't Repeat Yourself".

In that code you have "face[currentRect]" numerous times. As far as I can see currentRect doesn't change, so every time you evaluate that expression you're going to get the same result. So don't keep evaluating it, just do that once and assign the value to a variable. And then use that variable repeatedly instead. Like so:



And later:



That should make your code easier to read, which is also important when you're asking other people to read it.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Paul Clapham wrote:There's a computing principle called "DRY" -- "Don't Repeat Yourself".

In that code you have "face[currentRect]" numerous times. As far as I can see currentRect doesn't change, so every time you evaluate that expression you're going to get the same result. So don't keep evaluating it, just do that once and assign the value to a variable. And then use that variable repeatedly instead. Like so:



And later:



That should make your code easier to read, which is also important when you're asking other people to read it.


currentRect wont change during the mouseDragged, but will change after, so in my mouseDragged method I should do this?




Seems like that makes a lot of sense, never thought about it like that... Would that also effect performance in a positive way slightly?


EDIT: Still having issues and need to use and when doing so I can only snap to the first shelf.... Is there a reason it's not looping the shelves? I still get confirmation from my "kaw4" in the output, but no snapping...

EDIT2: I had to revert my code to earlier, because the last shelf I used to use always worked, and then didn't, so now all of them snap YAY! My only issue is why do I need to put and not why would my height be changed..... WTF!!! :(

with this extra else statement it would only do 1 shelf for some reason...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Jay Orsaw wrote:Would that also effect performance in a positive way slightly?


Yeah, it might run a couple of milliseconds faster. But generally it isn't worth making a change just to save a couple of milliseconds when responding to a mouse click.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Paul Clapham wrote:
Jay Orsaw wrote:Would that also effect performance in a positive way slightly?


Yeah, it might run a couple of milliseconds faster. But generally it isn't worth making a change just to save a couple of milliseconds when responding to a mouse click.


But you did say it's better to do so since I'm only using "currentFace" once though right, instead of face[currentRect]?

Also any idea about the height issue?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Jay Orsaw wrote:But you did say it's better to do so since I'm only using "currentFace" once though right, instead of face[currentRect]?

It's mostly to do with making code readable. Any fool can write code that works - even quite clever code - but the mark of a good programmer is to write code that other people can easily read and understand. If you don't, your programs will likely become Kleenex very quickly.

There was a very good page here by Brian Goetz about writing 'dumb code', but unfortunately, the java.sun.com pages don't seem to be responding any more. A Google might find you another copy though; it's well worth a read.

Winston
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

Are you calling me a fool? :p just kidding.... the link works ill take a look at it when i get home, and yeah you need to show your code is readable and that others can understand it especially in a working situation as a group. In class today they were talking about the programming competiton on saturday and how one kid was really smart, but no one could understand his code. It was so bad he couldnt understand it and had to redo it LOL seriously thats awful.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Funky issue with intersection and rectangles