• 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)>
      • 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
  • Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Donate
  • Facebook
  • Twitter

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

09/16/2012

72 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
 


Comments

Zh0k
09/25/2012 11:48pm

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
09/26/2012 7:26am

Thanks for correcting my mistakes! Much appreciated.

Reply
NikOS
10/08/2012 7:59am

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/08/2012 9:21am

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 11:45pm

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 11:17pm

Thanks so much. another great Tutorial

Reply
Richard
11/19/2012 9:38am

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

Reply
Karl
12/07/2012 10:06am

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/06/2012 2:38pm

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

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

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/14/2012 7:51am

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

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

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 8:26am

please help asap.

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

please help asap.:)

Reply
James C.
12/14/2012 1:46pm

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
07/10/2013 10:12am

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
Steve
12/14/2012 6:41pm

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 8:04pm

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 9:03pm

Email me your .java file.

jamescho7@kilobolt.com

Reply
Jermaine
01/07/2014 5:27am

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
01/08/2013 1:00pm

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
02/02/2013 9:16pm

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
02/02/2013 9:18pm

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

Reply
JP
02/03/2013 4:43pm

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
02/04/2013 5:09pm

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
03/02/2013 12:56pm

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

Reply
Aron link
02/20/2013 12:15pm

Thank you very much for this amazing tutorial!

Reply
Kirit
03/22/2013 1:42am

please change the color of comment in example

Reply
Eisfreak7
03/27/2013 3:14pm

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

Reply
Peanut
03/28/2013 10:28am

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
04/02/2013 8:46am

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

Reply
Matt
04/09/2013 1:45am

Very helpful, thanks!

Reply
Matt
04/29/2013 3:34am

(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
05/02/2013 1:45am

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
05/15/2013 6:00am

Yeah, I appreciate that, cheers :)

Ken
05/30/2013 10:43pm

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
06/02/2013 10:20pm

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
06/04/2013 7:30pm

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
06/10/2013 3:37pm

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
07/01/2013 7:12am

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
07/09/2013 6:21pm

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
07/22/2013 1:33pm

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 8:42pm

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
07/26/2013 4:49pm

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

Reply
SQB
07/28/2013 5:25pm

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
08/04/2013 6:40pm

i already fix it XD

Reply
Stephen
08/06/2013 3:19pm

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
08/06/2013 9:29pm

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
08/15/2013 4:12am

Can you explain me how paint() method is executed

Reply
Kinle
08/15/2013 4:15am

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

Reply
Gaurang
08/15/2013 4:33am


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
09/07/2013 7:35pm

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
09/20/2013 6:25pm

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/06/2013 8:26pm

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 6:59pm

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/01/2013 11:43pm

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/17/2013 5:15am

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/29/2013 7:52am

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
01/22/2014 4:33am

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

Reply
samy link
02/25/2014 7:14pm

you guys are awesome!!! :)

Reply
ajo link
02/25/2014 7:15pm

great tutorial
thanks

Reply
Simon
03/23/2014 12:46pm

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
03/26/2014 6:33am

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
03/27/2014 12:51am

got it, took a bit tho

Reply
jan
03/31/2014 11:53am

ChrisC how did you fix it?

jan
03/31/2014 12:04pm

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

ChrisC
03/31/2014 12:17pm

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

jan
03/31/2014 12:38pm

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
03/31/2014 8:22pm

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
04/01/2014 2:49pm

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
04/04/2014 8:43am

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
04/22/2014 11:28am

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



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.