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…
// importing Graphics in Application Window Toolkit
public class javagame extends JFrame
// Setting the title and other required parameters…
setTitle (“Game Window”);
public void paint (Graphics g)
g.drawString(“Hello World!!”, 75, 75);
public static void main (String args )throws Exception
Understanding the Code:
Now let’s move on to the most important pieces of code in the game making:
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.
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.
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.