| | |
2D objects question
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 1,574
Reputation:
Solved Threads: 199
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
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
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
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.
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.
•
•
Join Date: Sep 2008
Posts: 1,574
Reputation:
Solved Threads: 199
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.
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.
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
•
•
•
•
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??
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?
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 and you can forward to any of those methods as needed.
Java Syntax (Toggle Plain Text)
class TeamGlyph { private Team team; private Rectangle2D glyph; // etc }
•
•
Join Date: Sep 2008
Posts: 1,574
Reputation:
Solved Threads: 199
•
•
•
•
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 withand you can forward to any of those methods as needed.Java Syntax (Toggle Plain Text)
class TeamGlyph { private Team team; private Rectangle2D glyph; // etc }
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.
•
•
Join Date: Sep 2008
Posts: 1,574
Reputation:
Solved Threads: 199
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?
![]() |
Similar Threads
- Using .equals to compare objects Question (Java)
- Get objects contained into a web page (Visual Basic 4 / 5 / 6)
- Linked List & Objects (C++)
- Ad Aware SE Negligiable Objects question (Viruses, Spyware and other Nasties)
- array of objects memory allocation (C++)
- A question about 'const' (C++)
- Question about Updater.exe (Viruses, Spyware and other Nasties)
- Working with objects of different Classes (Java)
Other Threads in the Java Forum
- Previous Thread: Working with new frames
- Next Thread: Spliting an Image in java
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






