2D objects question

Thread Solved

Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

2D objects question

 
0
  #1
May 6th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 55
Reputation: LevelSix is an unknown quantity at this point 
Solved Threads: 3
LevelSix's Avatar
LevelSix LevelSix is offline Offline
Junior Poster in Training

Re: 2D objects question

 
0
  #2
May 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: 2D objects question

 
0
  #3
May 6th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: 2D objects question

 
0
  #4
May 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: 2D objects question

 
0
  #5
May 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: 2D objects question

 
0
  #6
May 6th, 2009
Originally Posted by BestJewSinceJC View Post
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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,444
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: 2D objects question

 
0
  #7
May 6th, 2009
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: 2D objects question

 
0
  #8
May 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: 2D objects question

 
0
  #9
May 6th, 2009
Originally Posted by Ezzaral View Post
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,574
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: 2D objects question

 
0
  #10
May 6th, 2009
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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC