Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

The Empire | Java Game | Tutorial #1 - The Basics of Game Development in Java

[Image: 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]

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]

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]

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]

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]

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]

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]
Very nice tutorial, are you gonna add more parts to this? :)
Pretty basic, I actually find this ironic because I just picked up Java again yesterday and am looking into LibGDX. Is this tutorial going to use that by any chance?
Yes, take some time to read, you'll see that I mentioned the next parts coming soon enough, probably tomorrow or the day after. By the end I'll be aiming at teaching everyone to make basic games, like Snake, Tetris or Space invader etc.

@Phaxol: No, since this is the pure basics, I'll stick to pure java code and will not use any extra libraries.
Not really the basics of game development. You should go over and explain the game structure of a game first, not just show how to make a JFrame/JPanel as the first tutorial.
(Sep 21, 2013 09:56 AM)Sparta. Wrote:  Not really the basics of game development. You should go over and explain the game structure of a game first, not just show how to make a JFrame/JPanel as the first tutorial.

This tutorial was more aimed at people having no idea about Java GUI or the swing package. I thought it would not hurt if I chirped in a tutorial the very beginning showing how to display a basic window and explaining a bit of JFrame functions before starting to do the actual code. Thanks for your feedback though. I appreciate constructive criticism. I'm sure you'll like me coming tutorials :)
All you did was convert this tutorial to text
http://www.youtube.com/watch?v=PopdTUzizDA

I mean, I'm sure that text better for some people, and that this is the simplest way to do it, but in-case anyone wanted a video tutorial, that would be it.
(Sep 21, 2013 10:01 AM)Bonfire Wrote:  All you did was convert this tutorial to text
http://www.youtube.com/watch?v=PopdTUzizDA

I mean, I'm sure that text better for some people, and that this is the simplest way to do it, but in-case anyone wanted a video tutorial, that would be it.

That's actually plagiarizing, he didn't cite his sources in the slightest. I think somebody should tell xadamxdk about this.
(Sep 21, 2013 10:01 AM)Bonfire Wrote:  All you did was convert this tutorial to text
http://www.youtube.com/watch?v=PopdTUzizDA

I mean, I'm sure that text better for some people, and that this is the simplest way to do it, but in-case anyone wanted a video tutorial, that would be it.

Well you could say so but you see, this is the base code you use to create a base window and set some common properties to it so you'll find atleast 100+ links where 99% of the lines are present. So there are not much changes to make in it and I'm not surprised to see this video either. And yeah, some people might like it in video format a bit more. There is absolutely no other way you could display a window. It's like saying 'hey your syntax for creating the main function is similar to mine' to make it more generic.
(Sep 21, 2013 10:04 AM)Canonsx130 Wrote:  Well you could say so but you see, this is the base code you use to create a base window and set some common properties to it so you'll find atleast 100+ links where 99% of the lines are present. So there are not much changes to make in it and I'm not surprised to see this video either. And yeah, some people might like it in video format a bit more. There is absolutely no other way you could display a window. It's like saying 'hey your syntax for creating the main function is similar to mine' to make it more generic.

There definitely is the method of making a window by initiating one through LWJGL, rather than starting off with the slow and limited renderer given by Java. To be honest, it would be preferable to start off the whole game development series off hardware-accelerated graphic platforms so that it would become an ease in practice later on.
(Sep 21, 2013 10:04 AM)Canonsx130 Wrote:  Well you could say so but you see, this is the base code you use to create a base window and set some common properties to it so you'll find atleast 100+ links where 99% of the lines are present. So there are not much changes to make in it and I'm not surprised to see this video either. And yeah, some people might like it in video format a bit more. There is absolutely no other way you could display a window. It's like saying 'hey your syntax for creating the main function is similar to mine' to make it more generic.

Hence why I said "and that this is the simplest way to do it."

I understand that there might not be other ways to do it.

I don't want to throw this thread off-topic though.
Nice tutorial.
(Sep 21, 2013 10:12 AM)DarkShadowz Wrote:  There definitely is the method of making a window by initiating one through LWJGL, rather than starting off with the slow and limited renderer given by Java. To be honest, it would be preferable to start off the whole game development series off hardware-accelerated graphic platforms so that it would become an ease in practice later on.

But then that's not how I planned it for my tutorial. I agere LWJGL is much more effcient but this is the core basics. I really feel like I should stick to just java for now. Maybe after I finish this small series and complete couple of games like snake or tetris, I'll start a LWJGL series which might interest even more members :)
Very nice, detailed for starters wanting to make games in Java.


(Sep 21, 2013 10:01 AM)Bonfire Wrote:  All you did was convert this tutorial to text
http://www.youtube.com/watch?v=PopdTUzizDA

I mean, I'm sure that text better for some people, and that this is the simplest way to do it, but in-case anyone wanted a video tutorial, that would be it.

...that's not even close to the tutorial. That is the proper way to make a JFrame, so why the fuck is it a big deal? Just because it's on someone elses tutorial doesn't mean he copied it, even I know that people usually use JFrame like that.

(Sep 21, 2013 10:03 AM)Swirl Wrote:  That's actually plagiarizing, he didn't cite his sources in the slightest. I think somebody should tell xadamxdk about this.

He didn't copy it, stop making up bull shit. You make me want to prove everyone how much of a fucking idiot you are. 🤣
(Sep 21, 2013 01:38 PM)Sparta. Wrote:  ...that's not even close to the tutorial. That is the proper way to make a JFrame, so why the fuck is it a big deal? Just because it's on someone elses tutorial doesn't mean he copied it, even I know that people usually use JFrame like that.

Where did I say he copied it? I simply said he converted it to text, then thanked him for the nice tutorial. Now stop derailing this thread.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by magicpartySep 4, 2019 05:43 AM 4 281 Sep 4, 2019 05:43 AM
Last Post: newQuery
[Java] by Lexxi.Dec 7, 2016 01:02 PM 1 232 Dec 7, 2016 01:02 PM
Last Post: CAFEEFAC
[Java] by r33drelJan 14, 2016 05:54 PM 0 176 Jan 14, 2016 05:54 PM
Last Post: r33drel