[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]
[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1581620501711{background-color: #f6f6f6 !important;}”]
GUI (Graphical User Interface) is what makes the interaction between the human and the computer possible, often which is visually appealing to the user in order to be able to easily control the computer. Java has the built in package JFrame that provides methods for GUI
[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]
GUI (Graphical User Interface) is used to facilitate contact between humans and computers. It can be in the form of, for example, pictures, graphic buttons, or sound indicators. A little more formally, we can say that a GUI displays objects that convey information and represent actions that can be taken by the user. Typically, the objects change color, size, or visibility when the user interacts with them, for example, when we press a button.
As you probably realize, most of what we use on the computer has some kind of GUI to facilitate our use. For example, think of all the buttons and functions that are available in Microsoft Word and Excel.[/vc_column_text][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/2″][vc_empty_space height=”40px”][vc_column_text]The GUI is what the user sees, in this case the game plan. The code is “invisible to the user” but runs in the background.[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_single_image image=”17376″ img_size=”medium” onclick=”link_image” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”40px”][vc_column_text]
Java has built-in classes provided in JDK (Java Development Kit) to create graphical user interfaces in a relatively simple way. Writing your classes that make graphics (and “reinvent the wheel”) is a tough, time-consuming, and complicated task.
Currently, there are three Java APIs for graphical programming
However, we should mention that most AWT components have become somewhat outdated and have been replaced with newer Swing components. We will continue to use Swing as we build the GUI for our game.[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”10px”][vc_column_text]
Furthermore, when it comes to choosing between Swing and Java FX, there is no clear answer, rather that it depends on the situation. Some differences are, for example, when it comes to the number of components, Swing has a more extensive set. JavaFX has a large number of components available, but less than what Swing provides. Additionally, Java FX has a slightly more advanced user interface and customizable components compared to Swing, which uses a more standardized design.
In conclusion, you can say that Swing is the default tool for Java developers to create GUI. This is probably due mainly to the more extensive library of components and that it simply existed longer, which is why we have chosen to use Swing in our example.
To read a deeper analysis about the differences between Swing and Java FX[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]
When programming our game, some game frame is needed, a GUI that the user should use to interact with the game. A common way to create an image frame in java is to use java’s built-in class, JFrame.
Below are some useful commands when working with JFrame.
Starting with declaring the object frame and gives the title of the game
JFrame frame = new JFrame("Title");
Second, Connects the object game to the GUI
frame.add(game);
Third, make the GUI the “right” size (the same size as the game)
frame.pack();
Further, if you close the window –> turn off the program
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Make it impossible to resize the window
frame.setResizable(false);
Position the GUI in the center of the screen
frame.setLocationRelativeTo(null);
Make the window visible to the user
frame.setVisible(true);
To conclude, let’s look at all of the code:
JFrame frame = new JFrame("Title");
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
More information and a summary of the methods available in JFrame can be found on the Oracle Website[/vc_column_text][vc_empty_space height=”30px”][vc_column_text]
[/vc_column_text][vc_empty_space][/vc_column][/vc_row]