Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAY 1-11: Threads and Graphics

9/16/2012

103 Comments

 
Welcome to the final lesson of Unit 1 of our Game Development Tutorial Series.
If you have stuck with me this far, you have taken a small step that will become a giant leap for your game development career.

Before we continue, I'd like to ask you once more to support Kilobolt! These lessons are offered to you free-of-charge, but it costs us real money from our pockets to maintain this website and our Dropbox. 

So if you could support us by:

1. Downloading TUMBL+ from the Play Store
2. Sending us a little donation
3. Liking us on Facebook
4. Linking to us on your blog or website

It would help us a lot! Really!

Or if you are unable to do any of these things, just tell your friends about us or put up a link to our site on your website, blog, or whatever! 

Thank you so much for being a Kilobolt supporter! 
We will continue to deliver high-quality content to you, and hopefully you guys will learn a lot from us!

Let's begin!
Picture
Lesson #1-21: Threads

So far in Java, we have followed a simple and general rule: when you press the Run button, the program runs line by line from top to bottom (unless it is looping).

When you start game development, however, you may realize that you require simultaneous processes for your game to work.

This is where a Thread comes in handy.

As the word thread may suggest, a thread is a single, lightweight process. When you have multiple threads, these processes are carried out simultaneously.

How do we create a thread?

The method is similar to how we would create a random object.

To create a random object, we used the code:

Random random = new Random();

To create a Thread, we do the following:

Thread thread = new Thread();

This creates a new Thread object called "thread."

Unlike random objects, however, creation of thread objects is a bit more lengthy process.

Threads require a built-in "run()" method, and we will incorporate it directly into the statement above. 

Example 1: Threads

1. We begin like so, adding braces to the end of the statement:

Thread thread = new Thread(){ };


2. These braces will contain the run method (which is, again, REQUIRED for a thread). I know this is new for you guys, so just try to apply your knowledge of hierarchy in code when you examine this next part:

Thread thread = new Thread(){ 
   public void run () {

   }

};


3. When you want this thread to start, you would type a built-in Java function (meaning that it is already defined by the language when used with a thread: .start();

thread.start();


4. When thread is started by .start(); it looks for the thread's run method and begins to run the lines of code there. At the moment, it is empty, so we will add a few lines of code to the run method:

Thread thread = new Thread(){ 
   public void run () {
      for (int i = 0; i < 10; i += 2){
         System.out.println("hello");
      }

   }

 }; 


5. And now when we execute the thread with: thread.start(); we will see:

Output:
hello
hello
hello
hello
hello


6. Now threads would be useless by themselves, so we will create another one. Here is what the full code will look like! To run this thread on your own eclipse, create a Class file called ThreadDemo and copy and paste.

 public class ThreadDemo {
public static void main(String args[]) {

// This is the first block of code
Thread thread = new Thread() {
public void run() {
for (int i = 0; i < 10; i += 2) {
System.out.println("hello this is thread one");
}
}

};

// This is the second block of code
Thread threadTwo = new Thread() {
public void run() {
for (int i = 0; i < 10; i += 2) {
System.out.println("hello this is thread two");
}
}

};

// These two statements are in the main method and begin the two
// threads.
// This is the third block of code
thread.start();

// This is the fourth block of code
threadTwo.start();
}

}


Let's now discuss step by step what happens when you run this code. Of course, as with all Java Programs, when this class is run, it will look for the main method. The main method contains 4 main blocks of code (indicated above with comments //).

The first one creates a Thread called thread and defines its run method. The second one creates a thread called threadTwo and defines its run method. The third one starts Thread thread, and the fourth one starts Thread threadTwo.


Output:
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two

Hold on. I mentioned that a thread allows simultaneous processes. Why didn't it alternate between thread one and thread two? 


We will discuss this in Unit 2. Stay tuned! :)
Lesson #1-22: Graphics

Nobody likes programs running on a shell like DOS (on second thought, I know a few people who enjoy showing off on Command Line). Most people like GUI's (graphical user interface) that they can interact with. Functionality is important, but interfaces are sometimes even more important.

We now begin our discussion of graphics. There is so much to talk about in graphics, so we will just touch upon a few statements that allow us to display graphics.

Note*: When I use the word graphics, I am referring to a digital image and not to game graphics, so keep that in mind!

In this lesson, we are first going to create a window, which can display images, and then try displaying some graphics on that.

To start, create a class called GraphicsDemo in Eclipse. This can be done by right clicking on the src folder of a project (in the package explorer to the left), selecting New >> Class, and typing GraphicsDemo for the name. You should then get this:

public class GraphicsDemo{

}


We now need to apply what we learned about Inheritance in the previous lesson.
To display images, we must extend the superclass JFrame like so:

public class GraphicsDemo extends JFrame{

}
 

Eclipse will give you an error saying that JFrame cannot be resolved. So you have to import it.

Shortcut: Ctrl + Shift + O
Alternate: Put your mouse over "JFrame," which will have a red underline, and import JFrame like so:
Picture

You will now see:
import javax.swing.JFrame;

public class GraphicsDemo extends JFrame{ 

}
 

One of the first things we talked about in Unit 1 for Game Development is that classes are blueprints for objects. We never really discussed how this works. 

In this lesson, we will use the GraphicsDemo class to create an object, much like we created a Thread object above.

To do so, we add a constructor to our class. A constructor is basically a set of instructions for creating an object:

 import javax.swing.JFrame;

public class GraphicsDemo extends JFrame {

// The constructor follows:
public GraphicsDemo() {

}

// All classes need a main method, so we create that here too!
public static void main(String args[]) {
// We will create a GraphicsDemo object in the main method like so:
// This should be familar, as we used this to create Random objects and
// Thread objects:
GraphicsDemo demo = new GraphicsDemo();

}

}

The above code, when executed, will look for the main method. This main method contains one statement:
      GraphicsDemo demo = new GraphicsDemo();

When this statement executes, you will be creating a GraphicsDemo object using the constructor (so named because it is used for construction of objects) of the GraphicsDemo class: and the name of this object will be demo.

At the moment, the constructor is empty:
   public GraphicsDemo(){
      


   } 

So when the GraphicsDemo object called demo is created, it will have no function. So we proceed by adding a few built-in statements that belong to the JFrame superclass (we can utilize these because we imported JFrame in the first line of code).

public GraphicsDemo(){
   setTitle("GraphicsDemo with Kilobolt");
   setSize(800,480);
   setVisible(true);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
}



The four statements within the constructor are self-explanatory. setTitle sets the title of the window when it is opened. The second statement setSize sets the resolution in pixels of the window. setVisible ensures that this window is visible when you create it. The final statement just allows the window to close properly and terminate the program.

We now add the constructor back into place:

 import javax.swing.JFrame;

public class GraphicsDemo extends JFrame {

// The constructor follows:
public GraphicsDemo() {
setTitle("GraphicsDemo with Kilobolt");
setSize(800, 480);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

// All classes need a main method, so we create that here too!
public static void main(String args[]) {
// We will create a GraphicsDemo object in the main method like so:
// This should be familar, as we used this to create Random objects and
// Thread objects:
GraphicsDemo demo = new GraphicsDemo();

}

}

Now if you run the code, you will see a window of size 800,480 (X,Y).
Picture

IMPORTANT! The origin (0,0) of the coordinate system on a monitor is the TOP LEFT not BOTTOM LEFT. This means that the Y values increase as you go down the screen!

To add images, we simply need to add one more method:

public void paint(Graphics g){

}



This method requires that you import Graphics, so...
Add import java.awt.Graphics; to the top of your code (remember that this just specifies to the compiler where it can find the Graphics class).

Now this paint method will draw whatever you ask it to draw on the window that you created. I will now teach you a few basic statements.

Note: The g. just denotes the fact that these statements (which are built-in methods of the Graphics class) are being executed on the object g. 

g.clearRect(int x, int y, int width, int height);  - Creates a filled rectangle with the current color (or default color if g.setColor() has not been called) with the top left corner at (0,0) with width witdh and height height.

g.setColor(Color c);      - Sets the current color of g as Color c.
g.drawString(String str, int x, int y);    - Draws whatever string (text) str at the point (x,y).
g.drawRect(int x, int y, int width, int height);    - Draws the outline of a rectangle beginning at (x,y) with width width and height height.

Let's add each of these to the paint method above.

 public void paint(Graphics g){

//This sets the color of g as Black.
g.setColor(Color.WHITE);

//The first statement creates the background rectangle on which the others are drawn.
g.fillRect(0,0,800,480);

//This sets the color of g as Blue.
g.setColor(Color.BLUE);

//This will draw the outline of a Blue rectangle, as the color of g when this is called is Blue.
g.drawRect(60, 200, 100, 250);

//This sets the color of g as Black.
g.setColor(Color.BLACK);

//This will display a black string.
g.drawString("My name is James", 200, 400);

}

We are using Color.BLUE and Color.Black from the Color superclass in the Java library, so we must import that too:

import java.awt.Color;

We add all this to the full class like below:


 import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class GraphicsDemo extends JFrame {

// The constructor follows:
public GraphicsDemo() {
setTitle("GraphicsDemo with Kilobolt");
setSize(800, 480);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 800, 480);
g.setColor(Color.BLUE);
g.drawRect(60, 200, 100, 250);
g.setColor(Color.BLACK);
g.drawString("My name is James", 200, 400);
}

// All classes need a main method, so we create that here too!
public static void main(String args[]) {
// We will create a GraphicsDemo object in the main method like so:
// This should be familar, as we used this to create Random objects and
// Thread objects:
GraphicsDemo demo = new GraphicsDemo();

}

}

Running this results in:
Picture
And there, we have created our first "graphics."
Picture
Thank you so much for sticking with these tutorials! This concludes Unit 1. 
We've covered a lot of material, and I hope that this Unit was both informative and fun for you.

I hope to implement a slightly different style of teaching in Unit 2, where we will build our first game together. Please join me there!

And finally...
Picture
Go to Day 10: Inheritance, Interface
Go to Unit 2: Creating a Game
103 Comments
Zh0k
9/25/2012 04:48:12 pm

First, great job with this tutorials. They are easy to understand.

I just wanna tell you a few mistakes in this one.

At the end of the section Threads you have that:
"The third one starts Thread thread, and the fourth one starts Thread thread."
You forgot to put threadOne and threadTwo. I know that is easy to know which threat is launched in which section, but repair this will be good ;)

The other thing is to add one line in the code of Graphics.
When I try it, the background of the app was the same that it have behind. Adding the next line will solve that (and put the background grey).

g.clearRect(0, 0, 800, 480);

Thanks for your time :D

Reply
James C link
9/26/2012 12:26:22 am

Thanks for correcting my mistakes! Much appreciated.

Reply
NikOS
10/8/2012 12:59:14 am

Hi James.

First of all thank you for these tutorials.They are easy to follow
and very helpful.Just a quick question on this one.I cant see how
the paint method is called since it is not inside the main method?

thanks in advance

Reply
James C
10/8/2012 02:21:40 am

Great question. This is slightly complicated.

Basically, when we extend JFrame, we are taking attributes of JFrame and applying it to our Graphics demo class.

When we create an instance of this class, it automatically runs the paint method (because it extends JFrame).

So by writing:
GraphicsDemo demo = new GraphicsDemo();

Our demo GraphicsDemo object will run the paint method.

This will become a lot clearer in the next Unit!

Reply
David link
10/11/2012 04:45:53 pm

When I ran the code the screen was black with a blue rectangle. I removed this:

//g.fillRect(0,0,800,480);


to make it look like you're example.

Reply
Reece
11/17/2012 03:17:12 pm

Thanks so much. another great Tutorial

Reply
Richard
11/19/2012 01:38:02 am

Why does my background appear Black when your example appears White? Am I noobing up on this one?

Reply
Karl
12/7/2012 02:06:44 am

I'm not sure if he meant to have fillRect at the very start of this piece. The colour I think would default to white anyway without the fillRect, which is the colour he has on his output. I think this might instead meant to be clearRect.

Odd that it's making the background black though. My only thought is that the colour for g in this example is set to black by default. You can simply remove the fillRect line to produce the effect by the tutorial, or change the colour above the draw string to say, white.

Reply
numps
12/6/2012 06:38:42 am

Great teaching and easy to learn! Looking forward to the next unit!

Reply
SRISHTI GOEL
12/13/2012 11:23:24 pm

in thread part you wrote :
Thread thread=new Thread(){---code---}
but when i ma doing this it is asking me to put a semicolon after this ().????

Reply
James C.
12/13/2012 11:51:03 pm

Please take a screenshot. :) It's probably an error with curly brace placement.

Reply
SRISHTI GOEL
12/14/2012 12:03:23 am

this is my code:
1.public class TryingThreads {
2. public static void main(String args[]){
3. Thread thread=new Thread(){
4. for(int i=0;i<10;i++){
5. System.out.println("hie its thread 1");
6 .}
7. }
8. }
9. }

getting an error on line3.saying:Syntax error, insert ";" to complete BlockStatements

Reply
SRISHTI GOEL
12/14/2012 12:26:07 am

please help asap.

Reply
SRISHTI GOEL
12/14/2012 12:26:21 am

please help asap.:)

Reply
James C.
12/14/2012 05:46:04 am

Thread thread = new Thread() {
public void run() {
for (int i = 0; i < 10; i += 2) {
System.out.println("hello this is thread one");
}
}

}; << You are missing this just like Eclipse is telling you.

Kavi
7/10/2013 03:12:54 am

public void run(){} is Missing...

Correct Code is:

public class TryingThreads {
public static void main(String args[]){
Thread thread=new Thread(){
public void run(){
for(int i=0;i<10;i++){
System.out.println("hie its thread 1");
}
}
}
}
}

Reply
SoCo
10/4/2014 06:34:43 pm

I realize this is very old but maybe someone will find it usefull

you have to add semicolons to the closing bracket of the object like so
Thread thread2 = new Thread(){
public void run(){
for (int i =0 ; i <25; i++){
System.out.println("This is thread 2 running!");
}
}
}; <-------

Reply
Steve
12/14/2012 10:41:32 am

Great job with the tutorials so far. I'm learning a lot and can't wait to move onto Unit 2.

Reply
SRISHTI GOEL
12/14/2012 12:04:38 pm

no u dont understand.i am getting an error on line number 3.and i tried by putting semicolon where u told yet i ma getting the error.it is saying me to put a semi colon after Thread thread=new Thread();

Reply
James C
12/14/2012 01:03:23 pm

Email me your .java file.

jamescho7@kilobolt.com

Reply
Jermaine
1/6/2014 09:27:55 pm

First, you should try to figure these things out yourself, look at the errors its giving you, then check online if you still can't figure it out

Secondly, don't be whiny and beg for help, ask politely and explain that you've tried to figure it out and looked online.

These are very simple things, you should be able to figure it out with a bit of thinking. If you start writing your own code, you won't have anyone to ask for help.

Reply
ptesla
1/8/2013 05:00:52 am

Great tutorial. It has been helping a lot, even though I am a total beginner in this field.
Its not too simple, but still easy to understand.

I had the same problem like some of the others here: The backscreen for this graphics class was black. I wonder, if we imported that. Anyways, the code g.setColor(Color.WHITE); [in front of all the other g. things] sets the screen to white.

Thanks a lot for this tutorial. Keep it up

Reply
Quinn link
2/2/2013 01:16:19 pm

First of all, thank you very much for your WONDERFUL tutorial. Secondly, I cannot import Jframe, when I mouse over it when I try to extend it in the class there is no option to import Jframe, just to create a class called Jframe. Do you know how to fix this?

Reply
Quinn link
2/2/2013 01:18:42 pm

Haha, never mind. Found the option to import. Wasn't looking hard enough. Thanks again for the great tutorials. :D

Reply
Jay
12/7/2014 04:51:46 pm

I have the same problem, mind telling me how to fix this?

JP
2/3/2013 08:43:46 am

Thank you for the tutorials. After running your GraphicsDemo example, I get the same result as yours which is good, but when I want to leave the program, it freezes for few seconds before it exits, and right after the program is closed, my OS freezes for few seconds too (cannot move mouse or anything). I'm using a Mac if you're just wondering.

Reply
DT
2/4/2013 09:09:35 am

Great tutorial james. I've a little problem with thread's output. I've following all of your code and then at the first running, in my console shown :
Hello this is thread one
Hello this is thread one
Hello this is thread one
Hello this is thread one
Hello this is thread one
Hello this is thread two
Hello this is thread two
Hello this is thread two
Hello this is thread two
Hello this is thread two

and at the second running it change into :
Hello this is thread two
Hello this is thread two
Hello this is thread two
Hello this is thread two
Hello this is thread two
Hello this is thread one
Hello this is thread one
Hello this is thread one
Hello this is thread one
Hello this is thread one

Reply
KH
3/2/2013 04:56:51 am

It puts them out in random order, since it can't really do them at the same time.

Reply
Aron link
2/20/2013 04:15:02 am

Thank you very much for this amazing tutorial!

Reply
Kirit
3/21/2013 06:42:37 pm

please change the color of comment in example

Reply
Eisfreak7
3/27/2013 08:14:47 am

"//This sets the color of g as Black.
g.setColor(Color.WHITE);"
I'm pretty sure thats not true ;D

Reply
Peanut
3/28/2013 03:28:09 am

Really like the tutorials and haven't had any problems till now.
When I type "thread.start();" in Eclipse it says that "The method start() is undefined for the type Thread" so I don't know what to do know, would appreciate any help :)

Reply
manish kumar
4/2/2013 01:46:02 am

thank you very much for this amazing tutorial. Everything is so simply explained.

Reply
Matt
4/8/2013 06:45:42 pm

Very helpful, thanks!

Reply
Matt
4/28/2013 08:34:12 pm

(I am not the same Matt as above :) )

When I typed in the first thread example (I prefer to type rather than copy as it helps me learn), I mistook the following in your example:

"public static void main(String args[]) {"

... for the following that I have seen in other tutorials:

"public static void main(String[] args) {"

... (the array [] brackets are in a different place). The code still runs and I wouldn't have noticed the difference if I hadn't made a typo elsewhere and had to review line by line.

I was wondering, is there is any difference in how these two formats are run, and is it important?

thanks.

Reply
Craig
5/1/2013 06:45:17 pm

Matt, there is no difference. I'm still mostly a beginner, but I've done a lot of tutorials and I'm very detail-oriented and I ran into this same issue. I finally found a post about it somewhere. The [] means that its a string array, not just a single string. I'll let you read about arrays on your own if you don't know already. Anyway, for some reason, it can be (String[] args) or (String args[]).

Also, I didn't realize this for a while either, but "args" is not a keyword or anything. It's just like picking the name for any other variable. It has just become the standard name used for that string variable in the main method. Hope this helps...

Reply
Matt
5/14/2013 11:00:58 pm

Yeah, I appreciate that, cheers :)

Ken
5/30/2013 03:43:26 pm

I've followed your instructions but it wont work because because:
Access restriction: The type JFrame is not accessible due to restriction on required library C:\Program Files\Java\jre7\lib\rt.jar

I also got an access restriction for 'setDefaultCloseOperation'. I'm going to try and work around these, but I thought you might like to know in case all future visitors will encounter the same error.

Reply
mohsin
6/2/2013 03:20:47 pm

hey J.C, i have a doubt, why do we need a constructor to create a object, while in you're previous examples of randomization and threads u didn't use one.why is it so?. im lil confused...

Reply
mrfunky
6/4/2013 12:30:55 pm

hey thanks a lot about this. it is like a crash course in making games for android, yay!
just a thing.
"The serializable class WindowDemo does not declare a static final serialVersionUID field of type long"
Thaterror appears after public class WindowDemo extends JFrame
Plz help? Thanks in advance :)

Reply
Ramin
6/10/2013 08:37:54 am

I created the object called "demo", but i'm getting an error saying that "demo" is never used. is that right?
also, in my paint method, it says void is an invalid type for the paint variable. what does that mean?

Reply
Ryuk
7/1/2013 12:12:26 am

I have a question about thread's outputs. Why there is 5 of "hello this is thread one" and hello this is thread two" ? I'm missing something out there.If you can help me i would really appreciate it. Here is what i thought:
for the first time:
i is initialized to 0 and becomes 2 output is "hello this is thread one";
second time i becomes 4; output is hello this is thread one";
third time i becomes 6; output is hello this is thread one";
fourth time i becomes 8; output is hello this is thread one"
fifth time i becomes 10 and it terminates the loop since i must be lower than 10.
Only possible explanation is for the first time i=0 prints the statement then i becomes 2 and so on. Can you lighten me up here please :)

Reply
Matheus
7/9/2013 11:21:15 am

Man, first of all, thank you so much for the tutorials :)
But I'm having a trouble, when I use Inheritance I got a warning on the declaration of public class GraphicsDemo extends JFrame, and the new class does not appear on the running button..
What's happening?

thank you in advance

Reply
Adam
7/22/2013 06:33:27 am

I have been wondering something for awhile now and can't find much at all about it even in the java tutorials. maybe you could help clear this up for me. Why is it that, Thread thread = new Thread and same for others like Random random = new Random is written with a capital letter followed by the same word with a lower case letter. I guess I'm asking why Random random is written twice and the purpose of it.

Reply
Scott
12/23/2013 12:42:02 pm

The first Thread is just stating that a Thread is being created. The second is the name of the Thread.

It could be Thread (any other name you would like to use) = new Thread

Same with Random

Reply
Hansfishberg
7/26/2013 09:49:52 am

Just wanted to say a HUGE thanks for these Tutorials, you really have helped me get into programming :)

Reply
SQB
7/28/2013 10:25:58 am

this is a great tutorial i congrat to the people who made it.
i got a problem, when i try to import the JFrame it just appear "JFrame cannot be resolved to a type"
and doesnt let me import it
please help me!!!!!

Reply
SQB
8/4/2013 11:40:08 am

i already fix it XD

Reply
Stephen
8/6/2013 08:19:12 am

im getting the same error as peanut above...
The method start() is undefined for the type Thread
wondering if it has to do with the fact that im using version 1.6?

"When you want this thread to start, you would type a built-in Java function (meaning that it is already defined by the language when used with a thread: .start();

thread.start();"

Reply
Albert
8/6/2013 02:29:43 pm

Hi James!
So, i tried this code i wrote and left me an error. But seems i couldn't find which one went wrong. :/

Any help is appreciated :D

import javax.swing.JFrame;


public class GraphicsDemo extends JFrame{


public GraphicsDemo() {
setTitle("GraphicsDemo with Kilobolt");
setSize(800,480);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void main(String[] args) {

GraphicsDemo demo = new GraphicsDemo();

}

}

Reply
Kinle
8/14/2013 09:12:22 pm

Can you explain me how paint() method is executed

Reply
Kinle
8/14/2013 09:15:11 pm

I mean it is not called in the program. Then how it works please explain

Reply
Gaurang
8/14/2013 09:33:53 pm


public class Thread
{
public static void main(String[] args)
{
Thread ThreadOne=new Thread()
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("This is Thred One");
}
}
};

Thread ThreadTwo=new Thread()
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("This Is Thread Two");
}
}
};

ThreadOne.start();
ThreadTwo.start();


}
}

i wrote this code but after running it shows that the start function is not defined.
can you help me with this?

Reply
Patrick
9/7/2013 12:35:00 pm

I tried to copy and paste the Thread code into Eclipse, but it says that start(); is undefined. I've looked all over the net for an answer, and got nothing. Please reply. Thank you in advance.

Reply
Cat
9/20/2013 11:25:33 am

I had the same problem as Gaurang and Patrick. Since my almighty God didn't have the answer (Google), I messed around, trying to figure it out. Here's the solution (formatting is most likely a bit screwed up).

public class Thread {
public static void main(String[] args) {
Thread thread = new Thread() {
};

thread.start();
}

public void start() {
for (int i = 0; i < 10; i += 2) {
System.out.println("hello");

}

Thread threadTwo = new Thread() {
};

threadTwo.startt();

}

public void startt() {
for (int i = 0; i < 10; i += 2) {
System.out.println("hello this is thread two");
}
}
}

I've got it to work like that with no errors...so hopefully using it like that won't screw things up in the future =P

Reply
award winning package design link
11/6/2013 12:26:12 pm

Great blog post, however only a few of the points were actually treated really good,I definitely enjoyed what you had to say. Keep heading because you undoubtedly bring a new voice to this subject.

Reply
Ayaz
11/16/2013 10:59:06 am

Well, this tutorial very confusing! i can't follow the code structure like which comes after what! like the first class GraphicsDemo after that.... man this is confusing allot now! some one help me! :(

Reply
Thomas
12/1/2013 03:43:03 pm

For those getting the start(); is undefined error...

Check for your class Name. If it is "Thread", change it. Thread is a reserved keyword and this is causing the issue.

Reply
M Maqsood
12/16/2013 09:15:41 pm

An excellent method and style of teaching for new comers. Please tell me how Paint() method call will be interpreted. A method id invoked when a call of that method is made.??????

Reply
Graham
12/28/2013 11:52:23 pm

Thanks for the tutorial, enjoying a lot and have purchased Tumbl+ as thanks. Keep up the great work, will donate as I get further.

Reply
hassan
1/21/2014 08:33:24 pm

you are not calling paint() method any where ..... !!!!!?
is it Ok?

Reply
samy link
2/25/2014 11:14:50 am

you guys are awesome!!! :)

Reply
ajo link
2/25/2014 11:15:49 am

great tutorial
thanks

Reply
Simon
3/23/2014 05:46:08 am

Great Tutorial, thanks!

What does this error mean?

"The serializable class WindowDemo does not declare a static final serialVersionUID field of type long"

Thank you!

Reply
ChrisC
3/25/2014 11:33:30 pm

im getting the same issue as a couple other people about importing JFrame

package game;

public class GraphicsDemo extends JFrame {

}

thats my lines so far and i cant go further because the only options i get for JFrame are create class 'JFrame' and fix project setup

i tried right clicking and there where no import options there either, and i dont know what to look for or where its at if i use file>import

thanks in advance and im loving this tutorial, i made it here in a day, i haven't been off my computer since i started xD

Reply
ChrisC
3/26/2014 05:51:22 pm

got it, took a bit tho

Reply
jan
3/31/2014 04:53:44 am

ChrisC how did you fix it?

jan
3/31/2014 05:04:40 am

Ok got it ;) . Right Click KiloboltTutorialSeries-->Properties-->Remove JRE library--> Add the same libray and then import JFrame

ChrisC
3/31/2014 05:17:48 am

importing the default library like he said, what confuses me is why the "default" library isnt really the default lol

MissWise
7/1/2014 12:56:53 am

Thanks for explaining this! I was having the same problems!

jan
3/31/2014 05:38:05 am

Ok great tutorial! But there is one thing, it would be nice to clear is up. Why does the paint method do something? It is never called (?invoked?), is it? I wrote a testclass and if i just put a method without calling it, it doesn' t do anything.
Thank you

Reply
Hayk
3/31/2014 01:22:31 pm

Hi. For the graphics demo, when I hover my mouse over JFrame, there's only two quick fixes available: " Create class JFrame" and "Fix Project Setup". I've created a class called GraphicsDemo, and added "extends JFrame" to "public class GraphicsDemo". Please help.

Reply
Jim link
4/1/2014 07:49:49 am

I just copied and past your code but its not working on my Eclipse:

public class Thread {
public static void main(String args[]){
// This is the first block of code
Thread thread = new Thread() {
public void run() {
for (int i = 0; i < 10; i += 2) {
System.out.println("hello this is thread one");
}
}

};

// This is the second block of code
Thread threadTwo = new Thread() {
public void run() {
for (int i = 0; i < 10; i += 2) {
System.out.println("hello this is thread two");
}
}

};

// These two statements are in the main method and begin the two
// threads.
// This is the third block of code
thread.start();

// This is the fourth block of code
threadTwo.start();

}

}

Error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method start() is undefined for the type Thread
The method start() is undefined for the type Thread

at Thread.main(Thread.java:26)


I would appreciate if you can help with this since I'm stuck in Threads :(
Ease my pain buddy.

Reply
ashish
4/4/2014 01:43:01 am

hey....grt wrk ...i want to ask that when i am running thread program.. a warning is coming that "local variable is never used" and program is not running..dnt know what to do..plz help :/

Reply
Hafez
4/22/2014 04:28:10 am

You are my hero! thanks for good tutorial.
a question:
we defined method paint but we never called it in our main method but when I compile the paint will take place. how can this happen?

Reply
antonio
9/22/2014 09:22:29 am

maybe the JFrame we have extended takes care of that?

Reply
khalil
5/1/2014 08:51:09 am

How do I know which color is for which part, for e.g you have three g.set Color(); I figured it out after I manipulated but I mean is not clear which one is for which?

Reply
SJV
5/28/2014 07:50:47 am

Hey I'm having a problem importing the JFrame. When you do it you have three options, and I only have the bottome two. I am using the newest eclipse and I am wondering if thats the problem. Any help would be appreciated

Reply
Hitesh
5/30/2014 04:33:19 pm

Hi sir,
im having problem can understand please help me
public class GraphicsDemo extends JFrame
{
}
im getting error on GraphicsDemo when im putting my cursor on this word im getting "The serializable class GraphicsDemo does not declare a static final serialVersionUID field of type long" error
and cant save my file and showing yellow triangle with ( ! ) mark

Please help me .. why im getting that error ..

Thanks a lot ..

Reply
Joe
6/21/2014 10:18:38 am

I imported the "javax.swing.JFrame;" and there are still errors. can any one help me plz?

Reply
khushi link
7/5/2014 08:16:30 pm

hello sir,
i will be grade full to u...
but i am getting problem..i cant understand last program of unit first ( how to called paint function)

Reply
Siddhant Reddy
7/22/2014 02:08:22 am

so u ask us to import JFRAME but it gives an error about some long type UID and gives the output as a blank screen.same as what u have shown.
exact error says. -the serializable class GraphicsDemo does not declare a static final serialVersionUID field of type long

Reply
ben
7/27/2014 10:35:38 am

sir i having problem using JFrame or to import JFrame
is there any problem with my installation pls help t.y.

Reply
Simar Singh
8/21/2014 01:28:27 pm

is JFrame a library "class" , that is why we are not creating it while using it as a superclass....??

Reply
antonio
9/22/2014 09:10:44 am

in your threads example output you show a linear result, where thread1 completes then thread2 completes, however, my output was different, in fact, it has random outputs, it rarely ends up like yours.

i've reduced iterations and changed output text, but here it is:
Thread one (1)
Thread two (1)
Thread one (2)
Thread one (3)
Thread two (2)
Thread two (3)

or:
Thread one (1)
Thread one (2)
Thread one (3)
Thread two (1)
Thread two (2)
Thread two (3)

or:
Thread one (1)
Thread two (1)
Thread two (2)
Thread two (3)
Thread one (2)
Thread one (3)

is this ok? or was it supposed to always be like your example?
Thread one (1)
Thread one (2)
Thread one (3)
Thread two (1)
Thread two (2)
Thread two (3)

thanks.

Reply
Zaid
1/26/2015 01:43:56 am

It is fully ok .
Moreover the real purpose of thread is to run them simultaneously

Reply
Dave
10/14/2014 04:36:44 am

Thank you James. This whole unit has covered ground I learned a while back on C# XNA but you described it all far better than other tutorials I had gone through and I now understand inheritance and else if which I didn't properly before. Thank you. Hope you can keep up the top work

Reply
Erwin
10/31/2014 09:21:22 am

Why does the windows stay open?
It's build and then the code ends.
There is no while loop keeping it running.

Reply
Mahesh
8/24/2016 10:53:55 am

The code ends after you hit close button (because of JFrame)

Reply
Ownicus
12/7/2014 08:51:07 am

If anyone is having problems with the JFrame import follow this:

http://stackoverflow.com/questions/23209125/unable-to-import-javax-swing-jframe

Thanks a ton for these tutorials, so far they've been a great help moving into Java development. :)

Reply
1337ingDisorder
12/17/2014 02:02:03 pm

This question was asked earlier in the comments by someone but doesn't seem to have been answered.

In the Graphics tutorial, why does the paint() method run without being called?

The main() method calls the constructor but it doesn't call the paint() method, nor does the constructor call the paint() method, yet the painting still gets done. Why is that?

Reply
Akylbek
12/21/2014 04:22:37 pm

Look for the comment of Kames C on 10/08/2012 9:21am

Reply
Akylbek
12/21/2014 04:23:30 pm

Sorry, *James C

1337ingDisorder
12/21/2014 04:49:04 pm

Ah, thanks!

f
12/21/2014 06:06:49 pm

Reply
Snoopy
3/27/2015 04:07:20 pm

hey there!
Shouldnt we be using the canvas class coz thats where the paint method comes from am i right?I have come across various pieces of code on the net and n all of them,the canvas class methods have been inherited.Btw,Your tutorials are beyond awesome and soo easy to understand.Thanx man !

Reply
Astemac
5/5/2015 10:12:37 pm

James Cho very very thank you for your free of cost tutorials which have helped me a lot in learning java..... :)

I just wanna ask that why had we used the main method at the last instead of using at the first in "Graphic" example???

Reply
aseraph
5/7/2015 10:23:50 am

Hi James,
My code is not working. It keep pop up with the same error:
"The method start() is undefined for the type Thread"

This is my code, it keeps highlight the both 'start' line:

import java.net.*;
import java.io.*;

public class Thread {
public static void main(String args[]){
Thread threadOne = new Thread(){
public void run()
{
for (int i = 0; i < 10; i+=2){
System.out.println("This is Thread one.");
}
}
};
Thread threadTwo = new Thread(){
public void run(){
for (int i = 0; i < 10; i+=2){
System.out.println("This is Thread two.");
}
}
};
threadOne.start();
threadTwo.start();
}

}

Reply
Sanju
8/5/2016 12:55:40 am

Hi, Question about thread. I have copied and pasted same thread code as given(the one with 4parts). But everytime I run it it gives me diffrent results!!! why so? Please help :( Here is the output.

RUN 1
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two
RUN 2
hello this is thread one
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
RUN3
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread one
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two
hello this is thread two

Reply
aka bhai
10/19/2016 10:19:07 am

i am getting this error on word "GraphicsDemo" .

("The serializable class GraphicsDemo does not declare a static final serialVersionUID field of type long")


so plz tell its solution......
thanks for all knowledge you are providing through this website..
its a great help to me..:D

Reply
Satyam
3/6/2017 09:13:55 pm

First of all thank you very much these lessons;
i had one doubt that u never called paint method;
thanks in advance

Reply
64PBRB
6/18/2017 12:15:09 am

"GUI's" shouldn't have an apostrophe... You should know grammar is important, especially in programming.

Reply
Pranay
5/13/2018 02:04:30 am

i am not able to write a single project on eclipse it says that it is an implicit super constructor error i must define a constructor.
i am using jdk 8.

Reply



Leave a Reply.

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge. 


© 2014 Kilobolt, LLC. All rights reserved.