11
22/**
3- * This class is used to test the quadtree. It contains the drawing canvas and is
3+ * <p> This class is used to test the quadtree. It contains the drawing canvas and is
44 * responsible for animating and rendering the scene along with the quadtree. The
55 * main method is here as well.
66 *
77 * @author Arash J. Farmand
8- * @version 3.12
9- * @date 2019-12-02
8+ * @version 3.15
9+ * @date 2019-12-04
1010 * @since 2019-11-24
1111 */
1212import java .awt .event .*;
@@ -229,7 +229,7 @@ public void keyPressed(KeyEvent e) {
229229 plus_5_AABB_Button = new JButton ("AABB (+5)" );
230230 plus_5_AABB_Button .setBackground (Color .ORANGE );
231231 plus_5_AABB_Button .addActionListener ((ActionEvent e ) -> {
232- add_AABB (5 );
232+ add_AABBs (5 );
233233 update_Frame_Title ();
234234 });
235235 plus_5_AABB_Button .setSize (100 , 30 );
@@ -240,7 +240,7 @@ public void keyPressed(KeyEvent e) {
240240 plus_20_AABB_Button = new JButton ("AABB (+20)" );
241241 plus_20_AABB_Button .setBackground (Color .ORANGE );
242242 plus_20_AABB_Button .addActionListener ((ActionEvent e ) -> {
243- add_AABB (20 );
243+ add_AABBs (20 );
244244 update_Frame_Title ();
245245 });
246246 plus_20_AABB_Button .setSize (100 , 30 );
@@ -293,7 +293,7 @@ public void keyPressed(KeyEvent e) {
293293 menu_Bar .add (shrink_AABBs_Button );
294294
295295 // Initialize the Quadtree using this JPanel for size measurements
296- quadtree = new Quadtree (aabbs , 0 , 0 , wdth , hght , max_Tree_Depth , square_Quadtree );
296+ quadtree = new Quadtree (aabbs , wdth , hght , max_Tree_Depth , square_Quadtree );
297297
298298 try {
299299 UIManager .setLookAndFeel ("javax.swing.plaf.nimbus.NimbusLookAndFeel" );
@@ -306,9 +306,11 @@ public void keyPressed(KeyEvent e) {
306306 }
307307
308308 /*******************************************************************************
309- * Create and add a new randomly generated AABB to the simulation. *
309+ * <p>Create and add "n" new randomly generated AABBs to the simulation.</p>
310+ *
311+ * @param n The number of new AABBs to add to the simulation
310312 ******************************************************************************/
311- private void add_AABB (int n ) {
313+ private void add_AABBs (int n ) {
312314 number_Of_AABBs += n ;
313315 AABB [] temp_AABBs = new AABB [number_Of_AABBs ];
314316
@@ -327,7 +329,9 @@ private void add_AABB(int n) {
327329 }
328330
329331 /*******************************************************************************
330- * Increase the width and height of all AABBs by a factor "s". *
332+ * <p>Increase the width and height of all AABBs by a factor "s".</p>
333+ *
334+ * @param s A scalar value for which to grow AABBs
331335 ******************************************************************************/
332336 private void grow_AABBs (float s ) {
333337 for (AABB aabb : aabbs ) {
@@ -336,8 +340,10 @@ private void grow_AABBs(float s) {
336340 }
337341
338342 /*******************************************************************************
339- * Paint the quadtree and all AABBs based on the current location of the AABBs *
340- * on the screen along with any messages that may be present. *
343+ * <p>Paint the quadtree and all AABBs based on the current location of the
344+ * AABBs on the screen along with any messages that may be present.</p>
345+ *
346+ * @param g The graphics object to paint to
341347 ******************************************************************************/
342348 public void paint (Graphics g ) {
343349 super .paint (g );
@@ -347,8 +353,10 @@ public void paint(Graphics g) {
347353 }
348354
349355 /*******************************************************************************
350- * Paint the quadtree and all AABBs based on the current location of the *
351- * AABBs on the screen. *
356+ * <p>Paint the quadtree and all AABBs based on the current location of the
357+ * AABBs on the screen.</p>
358+ *
359+ * @param g The graphics object to paint to
352360 * ****************************************************************************/
353361 private void paint_AABBs (Graphics g ) {
354362 for (AABB aabb : aabbs ) {
@@ -362,7 +370,9 @@ private void paint_AABBs(Graphics g) {
362370 }
363371
364372 /*******************************************************************************
365- * Paint any required messages in the middle of the screen. *
373+ * <p>Paint any required messages in the middle of the screen.</p>
374+ *
375+ * @param g The graphics object to paint to
366376 ******************************************************************************/
367377 private void paint_Messages (Graphics g ) {
368378 if (PAUSED ) {
@@ -376,13 +386,18 @@ private void paint_Messages(Graphics g) {
376386 }
377387
378388 /*******************************************************************************
379- * Paint each Quadnode that contains at least 1 object. *
389+ * <p>Paint each Quadnode that contains at least 1 object.</p>
390+ *
391+ * @param g The graphics object to paint to
392+ * @param node The node to paint
380393 ******************************************************************************/
381394 private void paint_Quadnode (Graphics g , Quadtree .Quadnode node ) {
382395 if (node != null ) {
383- if (! node .aabbs .isEmpty () ) {
396+ if (node .aabbs .size () > 1 ) {
384397 g .setColor (quadtree_Color );
385- g .drawRect (node .x1 , node .y1 , node .x2 - node .x1 , node .y2 - node .y1 );
398+ //g.drawRect(node.x1, node.y1, node.x2 - node.x1, node.y2 - node.y1);
399+ g .drawLine (node .cntr_x , node .y1 , node .cntr_x , node .y2 );
400+ g .drawLine (node .x1 , node .cntr_y , node .x2 , node .cntr_y );
386401 }
387402
388403 paint_Quadnode (g , node .nw );
@@ -393,27 +408,26 @@ private void paint_Quadnode(Graphics g, Quadtree.Quadnode node) {
393408 }
394409
395410 /*******************************************************************************
396- * Paint the Quadtree by telling each node within its hierarchy to paint *
397- * itself to the given graphics object. *
411+ * <p>Paint the Quadtree by telling each node within its hierarchy to paint
412+ * itself to the given graphics object.</p>
413+ *
414+ * @param g The graphics object to paint to
398415 ******************************************************************************/
399416 private void paint_Quadtree (Graphics g ) {
400417 paint_Quadnode (g , quadtree .root );
401418 }
402419
403- /**
404- * *****************************************************************************
405- * Pause the simulation. *
406- * ***************************************************************************
407- */
420+ /*******************************************************************************
421+ * <p>Pause the simulation.</p>
422+ ******************************************************************************/
408423 public static void PAUSE () {
409424 PAUSED = true ;
410425 }
411426
412- /**
413- * *****************************************************************************
414- * This method will remove the last AABB in the "aabbs" array from the * simulation. *
415- *****************************************************************************
416- */
427+ /*******************************************************************************
428+ * <p>This method will remove the last AABB in the "aabbs" array from the
429+ * simulation.</p>
430+ ******************************************************************************/
417431 private void remove_AABB () {
418432 if (number_Of_AABBs > 0 ) {
419433 AABB [] temp_AABBs = new AABB [--number_Of_AABBs ];
@@ -424,50 +438,44 @@ private void remove_AABB() {
424438 }
425439 }
426440
427- /**
428- * *****************************************************************************
429- * This method will remove the last AABB in the aabbs array from the * simulation. *
430- *****************************************************************************
431- */
441+ /*******************************************************************************
442+ * <p>This method will remove the last AABB in the aabbs array from the
443+ * simulation.</p>
444+ ******************************************************************************/
432445 private void remove_All_AABBs () {
433446 number_Of_AABBs = 0 ;
434447 aabbs = new AABB [0 ];
435- quadtree = new Quadtree (aabbs , 0 , 0 , wdth , hght , max_Tree_Depth , square_Quadtree );
448+ quadtree = new Quadtree (aabbs , wdth , hght , max_Tree_Depth , square_Quadtree );
436449 }
437450
438- /**
439- * *****************************************************************************
440- * Resume the simulation. *
441- * ***************************************************************************
442- */
451+ /*******************************************************************************
452+ * <p>Resume the simulation.</p>
453+ * ****************************************************************************/
443454 public static void RESUME () {
444455 PAUSED = false ;
445456 }
446457
447- /**
448- * *****************************************************************************
449- * Toggle between whether this simulation should have a square or rectangular * Quadtree. A rectangular
450- * Quadtree will fit the window component it lies in * whereas a square Quadtree will have a portion of the
451- * bottom extending * beyond the bottom of the window component. The Quadtree is rebuilt after * this
452- * change. *
453- * ***************************************************************************
454- */
458+ /*******************************************************************************
459+ * <p>Toggle between whether this simulation should have a square or
460+ * rectangular Quadtree. A rectangular Quadtree will fit the window component
461+ * it lies in whereas a square Quadtree will have a portion of the bottom
462+ * extending beyond the bottom of the window component. The Quadtree is
463+ * rebuilt after this change.</p>
464+ * ****************************************************************************/
455465 public void toggle_Square_Quadtree () {
456466 square_Quadtree = !square_Quadtree ;
457467 if (square_Quadtree ) {
458468 toggle_Square_Button .setText ("<html>Rectangulate<br/>Quadtree</html>" );
459469 } else {
460470 toggle_Square_Button .setText ("<html>Square<br/>Quadtree</html>" );
461471 }
462- quadtree = new Quadtree ( aabbs , 0 , 0 , wdth , hght , max_Tree_Depth , square_Quadtree );
472+ quadtree . reshape ( wdth , hght , square_Quadtree );
463473 }
464474
465- /**
466- * *****************************************************************************
467- * Update the information in the title of the frame based on the current * state of the Quadtree and AABBs.
468- * *
469- *****************************************************************************
470- */
475+ /*******************************************************************************
476+ * <p>Update the information in the title of the frame based on the current
477+ * state of the Quadtree and AABBs.</p>
478+ ******************************************************************************/
471479 private void update_Frame_Title () {
472480 String shape = square_Quadtree ? "Square" : "Rectangular" ;
473481 frame .setTitle ("Quadtree / AABB Simulation"
@@ -477,12 +485,11 @@ private void update_Frame_Title() {
477485 + " |" );
478486 }
479487
480- /**
481- * *****************************************************************************
482- * Continuously loop rendering the screen until exit. This method will update * the square's locations,
483- * update the Quadtree and refresh the screen by * calling "repaint()". *
484- *****************************************************************************
485- */
488+ /*******************************************************************************
489+ * <p>Continuously loop rendering the screen until exit. This method will
490+ * update the square's locations, update the Quadtree and refresh the screen
491+ * by calling "repaint()".</p>
492+ ******************************************************************************/
486493 private void update_Scene () {
487494 while (true ) {
488495 if (!PAUSED ) {
@@ -502,13 +509,12 @@ private void update_Scene() {
502509 }
503510 }
504511
505- /**
506- * *****************************************************************************
507- * Iterate through the AABBs array and tell them all to update their * locations. This method will move the
508- * AABBs within the bounds defined by * wdth and hght. If the AABB is moving out of bounds, reverse dx or
509- * dy as * needed so that the AABB travales away from the boundaries. *
510- * ***************************************************************************
511- */
512+ /*******************************************************************************
513+ * <p>Iterate through the AABBs array and tell them all to update their
514+ * locations. This method will move the AABBs within the bounds defined by
515+ * wdth and hght. If the AABB is moving out of bounds, reverse dx or dy as
516+ * needed so that the AABB travels away from the boundaries.</p>
517+ ******************************************************************************/
512518 private void update_AABB_locations () {
513519 for (AABB aabb : aabbs ) {
514520 if (aabb .x2 + aabb .dx >= wdth || aabb .x1 + aabb .dx <= 0 ) {
@@ -521,11 +527,9 @@ private void update_AABB_locations() {
521527 }
522528 }
523529
524- /**
525- * *****************************************************************************
526- * main method *
527- *****************************************************************************
528- */
530+ /*******************************************************************************
531+ * <p>main method</p>
532+ ******************************************************************************/
529533 public static void main (String [] args ) {
530534 new Tester ();
531535 }
0 commit comments