线程无法启动

时间:2013-09-29 14:05:34

标签: java multithreading

我试图沿着对角线移动一个星形图像。我正在使用Thread来尝试实现这一点。程序编译并显示图像,但是星星根本不会移动。我不认为线程正常启动。

非常感谢帮助

绘图班(董事会):

    //define host package
package star;

//import awt and swing drawing packages
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

//jpanel and other javax classes
import javax.swing.JPanel;
import javax.swing.ImageIcon;

//main board class
public class Board extends JPanel implements Runnable 
{
    //constructor
    Image star; //star image to hold image returned from directory
    int x, y; //co ordinates for translation of star image
    //delay constant
    private final int DELAY = 50;
    private Thread animator;

    public Board()
    {
       //set the background colour to black
        setBackground(Color.black);

        //image directory
        ImageIcon ii = new ImageIcon(this.getClass().getResource("star.png"));
        //retrieve image from directory
        star = ii.getImage();

        //paint in memory then screen to improve
        setDoubleBuffered(true);

        //set star co ords variables
        x = y = 10;
    }

    //initialize thread
    void AddNotify()
    {
        super.addNotify();
        //run method in this class
        animator = new Thread(this);
        animator.start();
    }

    //jpanel paintComponent() with abstract graphics object
    @Override public void paintComponent(Graphics comp)
    {
        //repaint screen due to animation
        super.paintComponent(comp);
        Graphics2D comp2d = (Graphics2D) comp;
        //draw the star
        //class should be notified of drawing
        comp2d.drawImage(star, x, y, this);

        //sync for linux systems
        Toolkit.getDefaultToolkit().sync();
        comp.dispose();
    }

    //set the coordinates for the star image
    public void cycle()
    {
        //move star
        x += 1;
        y += 1;

        //if top corner goes out of range
        if (y > 240)
        {
            x = -45;
            y = -45;
        }
        System.out.println("x: " + x + "y: " + y);
    }

    //action performed method. Event parameter from the timer
    public void run()
    {
       //beforeTime, timeDiff and sleep variables
       //long = 2 x integer
       long beforeTime, timeDiff, sleep;

       beforeTime = System.currentTimeMillis();

       //infinite loop
       while (true)
       {
           //cycle and add notify methods
           cycle();
           //call the paintComponent method
           repaint();

           //compute system time
           timeDiff = System.currentTimeMillis() - beforeTime;
           /*subtracting from delay keeps lag from cycle() & AddNotify() 
             methods unoticable.
             timeDiff will change with each loop cycle
           */ 
           sleep = DELAY - timeDiff;

            //compensate for a timeDiff > 50
            if (sleep < 0)
            {
                sleep = 2;
            }

           //sleep thread in exception
           try
           {
               Thread.sleep(sleep);
           }
           catch (InterruptedException ie)
           {
               System.out.println("Thread could not sleep: " + ie.getMessage());
           }

           //reset beforeTime time
           beforeTime = System.currentTimeMillis();
       }
    }
}

主java框架类:

    //import jframe
import javax.swing.JFrame;

//main class
public class Star extends JFrame
{
    //constructor
    public Star()
    {
        //title, resize, size, location etc.
        add(new Board2());
        setTitle("Star animation");
        setSize(240, 280);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    //class instance
    public static void main(String[] arguements)
    {
       new Star();
    }
}

很高兴根据要求澄清。

2 个答案:

答案 0 :(得分:1)

你永远不会调用AddNotify方法,这是你启动线程的地方。我猜你有一个错字,而你的意思是addNotify(注意小写)。

当重写方法时,添加@Override标记很有用,因为如果你覆盖的方法不存在,编译器会抱怨。

@Override
void addNotify() {
    super.addNotify();
    //run method in this class
    animator = new Thread(this);
    animator.start();
}

答案 1 :(得分:0)

在您的代码执行中,似乎没有调用您的Thread start调用。您正在AddNotify方法中启动该线程,但该方法未被调用。

此外,您的代码在此行中存在编译问题:

    add(new Board2());

没有课程Board2,而是您的班级名称为Board。我希望在这里粘贴代码时只是一个错误。