Skip to main content

Java Game Programming For Beginners #1: Creating a Canvas OR Screen for Your Game

Most of us love to play games on their computers but wouldn't it be fun if we could program our own game in Java. So I'm gonna show you how to make your game using Java programming.




Making a game in java will require very good programming skill, so before reading this post make sure you at least know the basic concept and practical knowledge on how to make a java program.


Game Programming is the vast concept and it cannot be written in a single post so I will be posting each important step in different posts and explain to them so that you will get a clear understanding of each step.

So as the title says, in this post, I'm gonna show you how to Create a canvas which is basically the Window inside which your game runs. I will be uploading more posts on game programming using JAVA and will place all the links in this post. So make sure you bookmark this page.


Note: You will need an IDE like Netbeans for running your program.Also, I will firstly be writing the full code, so that you can just copy paste and try if it's working. Then I will take parts of the code and explain it briefly.


So here's the code:


//importing the swing package...

import javax.swing.JFrame;

// importing Graphics in Application Window Toolkit

import java.awt.Graphics;

//creating class
public class javagame extends JFrame
{

public javagame()
{
// Setting the title and other required parameters...
setTitle ("Game Window");
setSize (500,500);
setVisible (true);
setResizable (false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
}

public void paint (Graphics g)
{

g.drawString("Hello World!!", 75, 75);

}
public static void main (String args [])throws Exception
{
new javagame();
}
}

Understanding the Code: 



So hope the code works fine on your PC and by now you hopefully you must have got a working window with Hello world printed on it. Now I'm going to explain what each line of code means.

Moving on to the first line of code (import java.swing.JFrame;) , it means we are importing or loading the required modules and functions which are predefined in Java ie, in simple words we are telling the Java compiler to load Javax.swing . Javax.swing package is needed to create GUI (Graphical User Interface) apps using JAVA. The next line (import java.awt.Graphics;) is also like that of the above but instead of JFrame we are importing Graphics package which is needed for drawing or painting data inside the Window.


Next, we are creating a class javagame which extends JFrame. Inside the class, we initialized a constructor (a method with the same name as that of class) which gets called automatically during object initialization. The constructor [javagame()] sets the title, size of the windows etc. 

Now let's move on to the most important pieces of code in the game making:


Constructor, javagame()


setTitle ()

 This line of code as the might have guessed is used to set the title of the game Window, it takes a string input.  You must specify the name of the title inside brackets like setTitle ("Game Window"); 

setSize() 


This is also a pretty straight forward command, this command is used to initialize or set the size of your game window. It takes an integer value. Size should be in pixel not in cm or inches. For example, if you want 1024x768 pixel window, your code should look like this: setSize(1024,768)


setVisible()  


This command is used to specify whether the window is visible or not. It takes a 


 Boolean value ie, true or false. Since we are making a game, we, of course, need the window to be visible (who can play on an invisible window :)) So we should set   the value to true, setVisible(true);

setResizable()


This line of code is used to specify whether they can be re-sized your not. It also takes a Boolean value. In most cases, programmers leave this as false. 


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  


This is used to specify what the Window, we created does when we try to close it. Though the window closes even if we still omit this line of code, the program won't terminate and still continues to run, causing unnecessary memory leaks etc. So this is the most important line of code. By adding this line of code to the source, the window, and the program both terminates correctly.

void paint(Graphics g)


This method is used to draw or paint the data on the screen, it consists of "Graphics g" parameter obtained from the java.awt.Graphics library. 

g.drawString ()


This line of code is used to draw or write a string on the screen. It takes String, int, int values. The string values are the value to be displayed on the screen.The other two integer values are x,y coordinates which determine the position where the String value is displayed.

for example : g.drawString("Welcome To TechnoRivals",100,120) ; displays Welcome To TechnoRivals in 100th position in x-axis and 120th position in y-axis.

static void main (String args[ ])

This is the main method which gets called during program run time, ie according to java, main is the first method that gets called.No program is a standalone application without the main method.

new javagame()

This is the object of the class called javagame. 



So that's all for now, Make sure you bookmark this page as I will post the links of all the future Java Game Programming tutorial in this post. 



If you loved my post, why not pause a moment to say thanks. If you have any suggestion for improvements, don't forget to leave a comment.

Comments

Popular posts from this blog

Akinator The Genie: In-depth Review of a Mind Reader Game

For a while, I was searching for a good app that can read our mind. No, I am not talking about an app which simply tells what we are thinking, but this app asks us to imagine a person (Real or fictional) and asks us some questions about the person. We should answer them correctly, and within 20 questions it will guess the person correctly. I am talking about ' Akinator The Genie ' from the company Elokence . We can say that this game is a success by looking at its downloads. It has over 50 million downloads and a rating of 4.3/5 . Akinator the Genie Many have tried to understand the algorithm of Akinator the Genie because it's algorithm is one of a kind, and that makes it magical. Rhett Allain  of Wired  has come up with a theory of how the game works, you can check out the article here . I have personally used this app a number of times, and every single time it gives me the correct answer. I was surprised when it gave me the right answers even when I t...

10+ Run Commands Every Windows User Should Know

Microsoft Windows is the first preference of every regular PC user, as its easy to operate and it implements lots of features in it. Well, Run commands are one of the best useful tools you can think about. Run commands are the best way to quickly access any app or task in Windows. By the way, most power Windows users use Run commands to open simple apps like notepad to the advanced apps like Group Policy editor. Note: press “Win + R” on your keyboard to open the Run dialog box, and enter any of the below commands to access the respective tool. So, in this article, I am going to share 10+ most useful Run commands every Windows user should know. 1. calc You guessed it right, the " calc" run command opens up the calculator app. Just enter " calc " in the run dialog box, and immediately calculator will open up. calc 2.control The control panel is used for managing all the system settings and programs, and we need to access it from time to time ...

Top 8 Websites To Download Software's For Your PC

When it comes to downloading software's from the internet many of us go directly thinking about viruses. Some say the internet is full of viruses , but is it really true? In my opinion there are good sites as well as bad ones in the internet.Sites that can turn your PC into a garbage and the ones which unlocks the full potential of your PC. Every PC new or old, good or bad is of no use without software's. Even the OS is a piece of software.  Downloading software's has always become a problem for beginners or novices. Some of them has unlimited internet connection but don't know where to download the software's without it containing any adware or spywares.  So folks, here are the  TOP 10 WEBSITES FROM WHERE YOU CAN DOWNLOAD FREE SOFTWARE'S : Note : The best place to download software's is directly from the author or owners website. 1. FileHippo By far the bes...