Sep 21, 2013 09:41 AM
(This post was last modified: Sep 21, 2013 09:43 AM by Canonsx130.)
![[Image: K4JFMBv.gif]](https://i.imgur.com/K4JFMBv.gif)
This tutorial is aimed at programmers who can understand and interpret basic java codes, construct basic programs and have a very fluent and solid idea of all the basic concepts of java. If you are not one of them, it is highly recommended you take bit of your time to learn the basics and then look at this thread. It is expected that you understand the code as you take it down and not just write it all down, compile and have your game. I'll try and explain every single line as detailed as possible. We will start out with the basics and by the end, create couple of very basic java games.
![[Image: GjZ13tc.png]](https://i.imgur.com/GjZ13tc.png)
Before starting to create the actual game, make sure you have the workplace setup. I use Eclipse and it is preferred you use it too. However it is upto you to decide any IDE you are comfortable with. Create the project and create a class giving whatever name you like. And you should be somewhere around here right now:
![[Image: U2AjtbI.png]](https://i.imgur.com/U2AjtbI.png)
Now that you have made the class, quickly type this out on Eclipse. Yeah this is the first piece of code you are ever going to write for making the game! Don't worry, I'll be explaining everything. And yeah, I'm a sneaky bastard, I didn't want you to copy paste everything so I decided to take a screenshot of the code 🤭
![[Image: q2Nh5OK.png]](https://i.imgur.com/q2Nh5OK.png)
Code
public class Prototype extends JFrame {Now what the helll is this line. Well what we did is first create the class giving the name and then we extended JFrame. So what is extends and what is JFrame. Well JFrame is another class, like our Prototype class. It exists already and we just do not see it. Now using extends JFrame will allow our poor class named Prototype to use the functions and use any other functionality the JFrame class had, in our class. So yeah, it kinda helps a lot of time of you from creating a new JFrame class.
Code
public Prototype() {What we just made with the above line is called a 'constructor'. As you might know already(yeah you're smart), constructor is more like a function, but that anything inside it is automatically initialized during the run. So you start the program and BAM, things inside it gets initiated. Just know that anything inside this stuff, gets automatically assigned when the program is run. There is nothing external to be done on it to wake up or apply the values to our program. That's a simple enough explanation which will suffice for now.
Code
add(new BoardClass());Now BoardClass is another class that we will make in a moment. BoardClass is going to be a class essentially like a board, or a panel to display our actual contents. And using the add keyword will set the BoardClass to the center of our JFrame component. So we have the JFrame and the board or the panel is set to the center of it to display anything which is needed.
Code
setDefaultCloseOperation(EXIT_ON_CLOSE);This is a vital line of code you'll probably use in every GUI program you make. This code will allow you to close the program using the X button in the top right corner. Else, the process will just sit there and you will not be able to cloes it out. So yeah, using this line of code will set the default operation on clicking the close button to exit out from the running program.
Code
setTitle("ProtoType");This is quite easy and I assume you have guessed what this does already. As easy and explanatory it is, it simply sets the title of our window to ProtoType. Just like you are seeing the thread title on your browser tab right now, whatever title we specify will be displayed on top of the window.
Code
setLocationRelativeTo(null);This is another simple line of code. Using this line, we simply center to window with the screen. You do not want the window to appear at one corner and annoy you right? Yeah, so we use this line of code to make sure when you run the program, the window appears in the dead center of your window, clean and neat!
Code
setSize(500, 260);Come on, use your brain kids! 10 points to anyone who can guess what this does. Yeah! Well done. You got it damn right. This line simply sets the size of our window to 500px in length and 260 in height. You can obviously change this value to any value you want depending on the program you want to make. It does not actually matter what size you choose as of now since we have not started to make the actual game.
Code
setVisible(true);Without this line of code, your entire code is pretty useless. This code displays the actual window on screen. If you set the visibility of the window to false, you'll obviously see nothing when you run the program. So make sure you never forget this line of code while writing a GUI-based program or nothing will appear on your screen. To repeat again, this simply makes the window visible on your screen.
Code
setResizable(false);Another self-explanatory line of code. This line of code simply makes the window you made non-resizable. Unless you want others to use their mouse to punish your window by scrolling and disfiguring it, you should probably use this little function. So setting it to false will make sure none of those cruel people can come out with their mouses and torture your little game window by deforming it.
Code
public static void main(String[] args) {This is probably like the first thing you learned in java, the main function. The main function runs first wherever it is within the program. Without the main function, the program will not run.
Code
new Prototype();And finally we simply create an object for the class using the new keyword. Notice that we are creating the object explicitly, hence the usage of the keyword new.
![[Image: ym0DY5i.png]](https://i.imgur.com/ym0DY5i.png)
Like we've said earlier, now we will need to create the BoardClass class. Why we are doing this? to display our actual stuff. This class will act as a panel to display anything we need. When we go about creating the game, it will happen inside this board, just line any other board, say for a chess game. Without this board where the game takes place, you can't do anything. So now we'll see the code first and then the explanation. First start of by creating a new class in the same package and name it BoardClass.
![[Image: jR7mUMz.png]](https://i.imgur.com/jR7mUMz.png)
Code
import javax.swing.JPanel;JPanel like we have told is a container to hold our actual game. So like we imported JFrame earlier, we have imported JPanel into our class from the javax package so that we can use it for good use. Please note that JPanel is inside javax, within the swing package and hence we included swing in between.
Code
public class BoardClass extends JPanel {We simply defined the class with the proper name and extended the JPanel class so that we'll be able to use the JPanel functionalities in our class, if needed.
Code
public BoardClass() {Again, this is a constructor. Notice that constructors will always have the same name of the class it is in. In this case since there is nothing else to initialize, there is no need to enter anything inside the constructor.
![[Image: TSHOfP2.png]](https://i.imgur.com/TSHOfP2.png)
With this, we conclude our first tutorial. In the next tutorial, we will display basic shapes and images onto our panel. If you liked this thread, please do leave comments which are highly appreciated. Now it's my favorite part. Let's make sure all the hardwork we did pays off. Do not wait another second, run down the cliff and click the run button to see the wonderful piece of art, a true souvenir, your first ever Java GUI 🎉
![[Image: JDD7Zxi.png]](https://i.imgur.com/JDD7Zxi.png)


