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