Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2008, 01:23 PM
Member
 
Join Date: Nov 2007
Posts: 17
bozovilla is on a distinguished road
Having problems with Thread
I am unable to get the Thread working in my code which is used to do a count down from 25 to 0. From my testing using a System.out.println: Only font and paint appear on the command console. CAn anyone help?

Here is my code:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Spaceship extends JFrame{

private JPanel displayP, radarP, rowsP, speedMeterP, messageP;//, controlsP;
private JLabel display, radar, rows, speedMeter, message;

//private JButton jbtnCake1, jbtnCake2, jbtnCake3,
// jbtnCoffee1, jbtnCoffee2, jbtnCoffee3;

//private JTextArea jta;
Count counter= new Count();

public static void main(String[] args){

Spaceship f=new Spaceship();
f.setVisible(true);
}//end of main

public Spaceship()
{

setSize(500,500);
setTitle("Spaceship");
setDefaultCloseOperation(EXIT_ON_CLOSE);
//getContentPane().setLayout(new BorderLayout());

Container container = getContentPane();
container.setLayout(new BorderLayout());

displayP=new JPanel();

display = new JLabel("display");
displayP.add(display);
container.add(displayP, BorderLayout.NORTH);

radarP=new JPanel();
radar= new JLabel("radar");
radarP.add(radar);
container.add(radarP, BorderLayout.WEST);

rowsP =new JPanel();
rows= new JLabel("rows");
rowsP.add(rows);
container.add(rowsP, BorderLayout.CENTER);

speedMeterP =new JPanel();
speedMeter= new JLabel("speedmeter");
speedMeterP.add(speedMeter);
container.add(speedMeterP, BorderLayout.EAST);

messageP =new JPanel();
message= new JLabel("message");
messageP.add(message);
container.add(counter, BorderLayout.SOUTH);

}//end of constructor
}

class Count extends JPanel implements Runnable {

Thread t;int i=0;
Font font;

public Count()
{
System.out.println("font");

font=new Font("TimesNewRoman",font.ITALIC,28);

}

public void start()
{System.out.println("start");

t=new Thread(this);
t.start();

}

public void run()
{
System.out.println("run");

for(i=25;i>=0;i--)//countdown
{
try
{
Thread.sleep(1000);
System.out.println(i);
}
catch(Exception e)
{
System.out.println(e);
}
}

repaint();

}//end of run

public void paint(Graphics g)
{
System.out.println("paint");
g.setColor(Color.red);//COUNTDOWN STOP
g.setFont(font);
g.drawString(""+i,50,50);

}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-14-2008, 04:29 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Quote:
Only font and paint appear on the command console. CAn anyone help?
What should appear? Can you explain what you expect to see?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-14-2008, 06:59 PM
Member
 
Join Date: Nov 2007
Posts: 17
bozovilla is on a distinguished road
Do you see that I have a thread to start and run? If the program actually runs them, it should appear font, start, run ant paint on the console. Thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-14-2008, 07:32 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
What does appear? Copy the output of the program here. It would help if you put it all in one post. One part of the post says what is output and another later part what you want to see. Having them together would make it easier to see your problem.

Where do you call the methods that are supposed to output what you want to see?

Last edited by Norm : 10-14-2008 at 07:34 PM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-16-2008, 01:36 AM
Member
 
Join Date: Oct 2008
Posts: 4
pedjacar is on a distinguished road
Hmmm, you never start your thread...

Put
Code:
counter.start()
after (for example)
Code:
container.add(counter, BorderLayout.SOUTH);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how high-priority thread allow other thread rameshkr Threads and Synchronization 3 09-10-2008 05:16 PM
passing a value from parent thread to child thread sachinj13 Threads and Synchronization 7 09-07-2008 11:06 PM
data from the main/GUI thread to another runnin thread... cornercuttin Threads and Synchronization 2 04-24-2008 12:30 AM
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 09:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 11:31 AM


All times are GMT +3. The time now is 08:02 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org