[英]Thread won't start
我正在嘗試將對角線的星圖移動。 我正在使用一個線程來嘗試實現這一目標。 程序會編譯並顯示圖像,但是星星根本不會移動。 我認為線程無法正常啟動。
幫助將不勝感激
繪畫班(董事會):
//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();
}
}
很高興根據要求澄清。
您永遠不會調用AddNotify
方法,這是您啟動線程的地方。 我猜你有一個錯字,而你的意思是addNotify
(注意小寫)。
覆蓋方法時,添加@Override
標記很有用,因為如果要覆蓋的方法不存在,編譯器會抱怨。
@Override
void addNotify() {
super.addNotify();
//run method in this class
animator = new Thread(this);
animator.start();
}
看來您的Thread start
調用未在代碼執行中的任何地方調用。 您正在AddNotify
方法中啟動線程, AddNotify
調用該方法。
另外,您的代碼在此行上還有編譯問題:
add(new Board2());
沒有班級Board2
,而您的班級名稱是Board
。 我希望將代碼粘貼到此處只是一個錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.