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

Recommended Answers

All 12 Replies

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.

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

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.

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.

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]

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

class TeamGlyph {
  private Team team;
  private Rectangle2D glyph;
  // etc
}

and you can forward to any of those methods as needed.

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.

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

class TeamGlyph {
  private Team team;
  private Rectangle2D glyph;
  // etc
}

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.

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?

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?

Break it up into two pieces (horizontal distance and vertical distance). To calculate vertical distance, there are three possibilities:

1. Rectangle 1 and rectangle 2 overlap vertically (i.e. top of rec 1 > bottom of rec 2 and top of rec 2 > bottom of rec 1). In this case, vertical distance = 0.

2. Rectangle 1 is completely above rectangle 2. In this case, vertical distance is bottom of rec 1 minus top of rec 2.

3. Rectangle 2 is completely above rectangle 1. In this case, vertical distance is bottom of rec 2 minus top of rec 1.

Calculate the horizontal distance the same way, using left and right coordinates rather than top and bottom coordinates.

Calculate the distance the normal way (square root of vert. distance squared plus horiz. distance squared) and compare to 25.

commented: Patient & helpful as always +4

Distance to center would provide a single calculation to check against that only involves two points. It's arbitrary, really, how close you decide is "close enough" for the association for "A vs B". Given that you only have to know when a user a dragged them near one another, I wouldn't bother determining min distance checks per side.

commented: Good suggestion - & thanks for all the help +4

Thanks for the help guys. +rep for dealing with my questions :)

And it is almost solved at this point, the rectangles are moving around properly and I'm setting "matched" teams to white, unmatched to blue, which is also working correctly. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.