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);

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”); 


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 1024×768 pixel window, your code should look like this: setSize(1024,768)


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);


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. 


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.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *