943,749 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 794
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
May 6th, 2009
0

2D objects question

Expand Post »
I have a list of teams, it can be any length. Now, for each team, I want to create a 2D box that says the team's name in the box. And I want to match these teams up against each other. So basically I want to be able to drag these boxes around, and if I drop them next to each other, I want it to say "Versus" between the two boxes. I'm pressed for time here as I somehow managed to finish my project, then realize I'd left out a requirement. So the easiest solution is preferred even if it isn't the most elegant. Here is what I'm thinking, if anybody has better ideas please suggest:

1. Make a Box class that represents a Box - instance variables would just be it's x, y coordinates and the Team name that is supposed to show up in the Box.
2. Make an ArrayList of boxes.
3. If the mouse is pressed on a certain window (the one where the boxes are), call the repaint method while the mouse is held down.
4. In the repaint method, go through the ArrayList of boxes, checking to see if the coordinates where the mouse current is are the coordinates of any of the boxes. If so, set the Box's coordinates to wherever the mouse currently is, and redraw the box.
5. I'd also have some method that checks the distance between the boxes after each repaint, and if the boxes are within a certain distance of each other, it would put "Versus" between them.

questions - does this way of doing it suck? and what would be the best way of making the team's name appear inside the box and move with the box?

thanks
Similar Threads
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
May 6th, 2009
0

Re: 2D objects question

As far as the name part goes, you could have your box constructor also take a string variable, which is the team name. Then you could use the drawText method to draw the team name, linking the coordinates to the x, y of the box.
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
May 6th, 2009
0

Re: 2D objects question

Thats what I just said lol. A team name is, of course, a String. I guess I'll start trying to implement this and hope that it is the right way to do it
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
May 6th, 2009
0

Re: 2D objects question

There's more than one way to do it and it may depend on the exact spec. I'm assuming it's like the NFL with the NFC versus the AFC or whatever. I like the idea of three separate vertical panels if this is the idea. The NFC team boxes are in the left panel, the AFC team boxes are in the right panel and the word "Versus" is in the middle panel if two boxes are "next to" each other, however you choose to define that (I like the criteria of checking coordinates or distances between boxes within a certain tolerance).

Dragged boxes stay within their panel (i.e. NFC boxes stay in NFC panel). In mousePressed, check whether the coordinate is inside of a box. If so, that's the selected box and you'll drag it in mouseDragged. In mouseReleased, that's when you check whether the just-released box is now "next to" another box from the other panel. If so, display "Versus" in the middle panel in between the two boxes. If not, display nothing in the middle panel.

I say don't check coordinates for every repaint. Check them in mousePressed and in mouseReleased. Run through your ArrayList as you planned to and check coordinates for each element till you find one that fits the criteria. Have a boxSelected variable that's either null or that is the selected box.
Last edited by VernonDozier; May 6th, 2009 at 7:33 pm. Reason: Fixed grammar and added to post.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,372 posts
since Jan 2008
May 6th, 2009
0

Re: 2D objects question

All I need is a JFrame filled with boxes. To start with, the boxes can be in any place in the JFrame. Then the user can move them wherever, and if they are put next to each other, it should say versus. How can I calculate initial positions (to put the boxes in) so that I'll be able to see all of the boxes??

edit: not sure if that made sense. But all I'm doing is putting the boxes in certain spots in the frame. Then if they are moved next to each other by the user, the word versus should appear.
Last edited by BestJewSinceJC; May 6th, 2009 at 7:51 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
May 6th, 2009
0

Re: 2D objects question

All I need is a JFrame filled with boxes. To start with, the boxes can be in any place in the JFrame. Then the user can move them wherever, and if they are put next to each other, it should say versus. How can I calculate initial positions (to put the boxes in) so that I'll be able to see all of the boxes??
Depends on how crowded the JFrame is. Figure out how tall a box is and the height of the JFrame, as well as how many teams there are. Figure out the height of the JFrame divided by the number of teams there are. If that's more than the height of each box, then there's no vertical overlap (there's room for all of them on top of each other), so evenly space them vertically (i.e. if JFrame height = 500, # of teams = 10, then each team is 500/10 = 50 below the team above it).

So in the above example, team 1's upper left box coordinate is (0,0), team 2's is (0, 50), team 3's is (0, 100), etc.

If there are too many teams to do that without overlapping vertically, then you'll have to get more fancy, I suppose, and not have them all initially all the way to the left.

[edit]
I'm getting some bizarre editing problems. What's being previewed is not what's showing up and the corrections aren't getting saved.
[/edit]
Last edited by VernonDozier; May 6th, 2009 at 8:15 pm. Reason: Fixed grammar. Thought I had done so already?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,372 posts
since Jan 2008
May 6th, 2009
0

Re: 2D objects question

In the same vein you are already discussing, keep in mind that you don't have to code all of the coordinate stuff yourself either. You can make a small wrapper to manage that glyph that just contains the other objects you need to work with
Java Syntax (Toggle Plain Text)
  1. class TeamGlyph {
  2. private Team team;
  3. private Rectangle2D glyph;
  4. // etc
  5. }
and you can forward to any of those methods as needed.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
May 6th, 2009
0

Re: 2D objects question

Hmm. My post editing isn't working. I keep trying to fix my grammar mistakes on the last post and it isn't changing. If I accidentally end up with 50 posts on this thread, sorry.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is online now Online
5,372 posts
since Jan 2008
May 6th, 2009
0

Re: 2D objects question

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
In the same vein you are already discussing, keep in mind that you don't have to code all of the coordinate stuff yourself either. You can make a small wrapper to manage that glyph that just contains the other objects you need to work with
Java Syntax (Toggle Plain Text)
  1. class TeamGlyph {
  2. private Team team;
  3. private Rectangle2D glyph;
  4. // etc
  5. }
and you can forward to any of those methods as needed.
I have no idea what that means, unless you're saying that I should make a class that contains the rectangle and the team, and use that to move around etc. But I don't see how that would save me any time or how it has an advantage over what I'm currently doing, which is having a rectangle class w/ coordinates & the team that should be displayed inside of it.

oh, and Vernon, I figured it out already, sorry, should have updated this post more quickly. I'm just using modulus to decide when to reset the x coordinate, and incrementing x and y while drawing the rectangles.
Last edited by BestJewSinceJC; May 6th, 2009 at 8:22 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
May 6th, 2009
0

Re: 2D objects question

Hmm... been working on this for awhile, I'm trying to code a method that will tell me whether two of the rectangles are within 25 pixels of each other. Distance formula will not work because there are (4 sides per rectangle)^2 = 16 different ways they could be that close to each other. Is there any easier way?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Working with new frames
Next Thread in Java Forum Timeline: Spliting an Image in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC