2D Animation of Recursive Backtracking Maze Solution Using Javafx Versus AWT and Swing
Total Page:16
File Type:pdf, Size:1020Kb
2020 International Conference on Computational Science and Computational Intelligence (CSCI) CSCI-ISSE Full/Regular Research Paper 2D Animation of Recursive Backtracking Maze Solution Using JavaFX Versus AWT and Swing Anil L. Pereira School of Science and Technology Georgia Gwinnett College Lawrenceville, GA, USA [email protected] Abstract—This paper describes software developed by the AWT and Swing have been used widely in legacy software author, that constructs a two-dimensional graphical maze and uses [8]. Even though, software developers claim that there is a steep animation to visualize the recursive backtracking maze solution. learning curve for JavaFX [9], applications using AWT and The software is implemented using three Java graphics Swing are being rewritten with JavaFX. This is because of technologies, Java Abstract Window Toolkit, Java Swing and JavaFX’s look and feel and other features mentioned above. JavaFX. The performance of these technologies is evaluated with Also, the performance is claimed to be better, due to its graphics respect to their graphics rendering frame rate and heap memory rendering pipeline for modern graphics processing units (GPUs) utilization. The scalability of their rendering performance is [10]. This pipeline is comprised of: compared with respect to maze size and resolution of the displayed • A hardware accelerated graphics pipeline called Prism maze. A substantial performance gap between the technologies is reported, and the underlying issues that cause it are discussed, • A windowing toolkit for Prism called Glass such as memory management by the Java virtual machine, and • A Java2D software pipeline for unsupported graphics hardware accelerated graphics rendering. hardware • High-level support for making rich graphics simple Keywords—graphics; animation; JavaFX; Swing; AWT with Effects and 2D and 3D transforms I. INTRODUCTION The software implemented by the author of this paper is comprised of four Java programs. One program called Abstract Window Toolkit (AWT) is the original platform- MakeMaze.java constructs a 2D graphical maze using animation dependent windowing, graphics, and user interface (UI) widget and a randomized version of the well-known depth first search toolkit for Java, first released by Sun Microsystems in 1995 [1, algorithm. The program stores the information about the maze’s 2]. Swing is a graphical user interface (GUI) widget toolkit for structure in a text file. The other three programs were Java and is part of Oracle Corporation's Java Foundation Classes implemented for the purpose of reconstructing the graphical (JFC). JFC is an Application Programming Interface (API) for maze after reading the information about its structure from the providing GUI for Java programs. Swing was originally text file, and then solving the maze graphically using animation developed by Netscape Communications Corporation and was and the well-known recursive backtracking algorithm. The first included as part of the Java Standard Edition release 1.2 in difference in the three programs is that they use different Java 1998. The Swing API mostly extends AWT, but unlike AWT graphics technologies. One of the programs is called components, Swing components are "lightweight". i.e., they are SolveMazeAWTSwing.java (implemented using AWT and not implemented by platform-specific code, instead, they are Swing) and the other two are called SolveMazeFXFill.java and written entirely in Java and are platform-independent [3, 4]. SolveMazeFXNodes.java (both implemented using JavaFX). JavaFX [5] is a software platform released by Sun SolveMazeAWTSwing.java uses AWT to draw the graphics Microsystems in 2008 for constructing and delivering desktop and Swing to construct the GUI frame on which graphics is applications, as well as rich Internet applications (RIAs) that can drawn. The space for the maze is logically divided into a grid of be used on a wide variety of devices. JavaFX became open- rectangular shaped cells. A cell is called a tile. source in 2011. In 2012, it became part of Oracle Corporation’s SolveMazeFXFill.java uses a graphics context object to directly Java Development Kit (JDK). JavaFX is replacing Swing paint each tile in the maze to the program memory, similar to because of several advantages. It is more lightweight, and has SolveMazeAWTSwing.java, but uses JavaFX libraries and Cascading Style Sheets (CSS) styling, FXML and Scene GPU-based hardware accelerated rendering. Builder. Scene Builder allows UI construction by dragging and SolveMazeFXNodes.java adds each rectangle shaped tile as a dropping controls from a palette. This information is saved as node to a scene graph and passes the graph to the GPU-based FXML, a special XML format. Oracle Corporation no longer hardware accelerated rendering pipeline. includes JavaFX with Java since JDK 11. In 2018, JavaFX was made part of OpenJDK under the OpenJFX project to hasten the The main contribution of this paper is the evaluation of the pace of its development [6, 7]. graphics rendering performance and scalability of Java Swing and AWT when compared to JavaFX, for the purpose of 2D animation. To the best of the author’s knowledge, there is no 978-1-7281-7624-6/20/$31.00 ©2020 IEEE 1787 DOI 10.1109/CSCI51800.2020.00330 work reported in the available peer-reviewed technical literature, The program produces a graphical representation of the maze that researches this specific topic and discusses the effects of through animation, when the Make Maze button is clicked on memory management by the Java virtual machine (JVM) and the GUI as shown in Figs. 1 - 5. Linked lists, stack and priority hardware accelerated graphics rendering on performance and queue [5, 12] are used to store elements of the maze during its scalability. However, there are several Web-based resources construction. Queues and linked lists are used to store elements such as blogs, documentation, articles, and question and answer of the discovered path(s). sites that cover this specific topic, but present no data to compare memory utilization of the three Java graphics technologies. In The MakeMaze.java program prompts the user to enter the this paper, the data for heap memory utilization of the three file name and the width and height of the maze (in pixels) to be technologies collected by the author is presented and analyzed. displayed. The user is also prompted for the number of logical Results show, that greater the heap memory utilization, greater rows and columns of a 2D array data structure. The program are the effects of JVM’s memory management tasks of memory constructs the 2D array and initializes the value at every position allocation and deallocation on graphics rendering performance in it to zero. Each position is logically indexed by the and scalability. The reasons for it are discussed in this paper. intersection of a row and column of the 2D array. The first row from the top is row 0 and the first column from the left is column The paper is organized as follows. Section II describes the 0. The value at a particular position in the 2D array corresponds implementation details of the four programs and how they use to the color of a tile in the maze at relatively the same position. the Java graphics technologies. Section III contains performance Zeros represent the tiles in the boundaries (walls) which are evaluation. Section IV contains conclusions and future work. colored black. II. SOFTWARE IMPLEMENTATION For the maze shown in Figs. 1 – 5, the user input was 20 for both the number of rows and columns of the 2D array. The A. Make Maze Program product of the number of rows and columns gives the maze size, The MakeMaze.java program constructs a maze using a i.e., the total number of tiles in the maze. Thus, the maze size is randomized version of the depth first search algorithm [11]. This 20 X 20 tiles. The maze is completely colored black initially, as algorithm is frequently implemented with a stack [5, 12]. As shown in Fig. 1, because every position in the array is initialized explained in the introduction above, the space for the maze is to zero. Figs. 2 - 5 show intermediate frames in the animation of logically divided into a grid of rectangular shaped cells, and a path construction in the maze after the Make Maze button is cell is called a tile. Beginning at a random tile, the program clicked. The program constructs the paths with ones. Ones selects a random neighboring tile to the left, top, right and represent the tiles in the paths. Tiles in the paths are colored light bottom that has not been visited. The program marks the new tile gray when first visited. They are colored white on backtracking. as visited, and pushes it onto the stack for the purpose of The program constructs n mazes using the array (the value backtracking [11]. The program continues this process until a tile of n is specified by the user), adds each maze to a linked list and with no unvisited neighbors is encountered. Such a tile is selects the one with the longest path area (greatest number of considered a dead-end. When a dead-end is encountered, the ones) using a priority queue and writes the ones and zeros row- program backtracks through the path it constructed until it by-row into a text file. The content of the text file is shown in reaches a tile with an unvisited neighbor, and continues the path Fig. 6. Each of the n mazes when constructed initially have the generation by visiting this unvisited tile, thus constructing a new same path length. To ensure different path lengths for each maze, junction. This process continues until every tile in the maze is zeros are randomly changed to ones before adding the maze to visited, causing the program to backtrack to the beginning tile the linked list.