1

我正在尝试对角移动星形图像。我正在使用线程来尝试实现这一目标。程序编译并显示图像,但星星根本不会移动。我认为线程没有正确启动。

帮助将不胜感激

绘图类(板):

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

很高兴应要求澄清。

4

2 回答 2

1

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

重写方法时,添加标签很有用@Override,因为如果您要重写的方法不存在,编译器会报错。

@Override
void addNotify() {
    super.addNotify();
    //run method in this class
    animator = new Thread(this);
    animator.start();
}
于 2013-09-29T14:09:51.310 回答
0

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

您的代码在这一行也存在编译问题:

    add(new Board2());

没有类Board2,而是你的类名Board。我希望在此处粘贴代码时这只是一个错误。

于 2013-09-29T14:10:07.733 回答