我试图用螺纹移动小应用程序中一球,但它的不动 [英] I am trying to move a ball in applet using thread but its not moving
问题描述
我想使用线程移动小应用程序中一球,但其不动。谁能帮我为新来applet,并继续游戏development..for参考这里是我的code
公共类球赛扩展JApplet的实现Runnable
{
INT X_POS = 50;
INT Y_POS = 100;
INT弧度= 10;
线程t; 公共无效的start()
{
super.start();
T =新的Thread(T);
t.start();
} 公共无效漆(图形G)
{
super.paint(G);
g.setColor(Color.red);
的setBackground(Color.BLACK);
g.drawOval(X_POS,Y_POS,2 *弧度,2 *弧度); 而(真)
{
X_POS ++; //的validate();
重绘(); 尝试
{
视频下载(100);
}
赶上(例外五)
{
e.printStackTrace();
} } //而结束
} //油漆结束()
}
有一个无限循环油漆
表示,该方法不是一个单一的传球就可以完成。
此外,你永远不应该叫视频下载(100)
在油漆
方法。此块 EDT
和降低性能。
而不是使用摇摆定时器的做更新和重新粉刷工作。另外我想子类的JComponent
和覆盖的paintComponent
。
I am trying to move a ball in applet using thread but its not moving. Can anyone help me out as m new to applet and proceeding for game development..for reference here is my code
public class ballGame extends JApplet implements Runnable
{
int x_pos=50;
int y_pos=100;
int rad=10;
Thread t;
public void start()
{
super.start();
t=new Thread("t");
t.start();
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.red);
setBackground(Color.BLACK);
g.drawOval(x_pos,y_pos,2*rad,2*rad);
while(true)
{
x_pos++;
//validate();
repaint();
try
{
Thread.sleep(100);
}
catch(Exception e)
{
e.printStackTrace();
}
}//end of while
}//end of paint()
}
Having an infinite loop in paint
means that not a single pass of the method can complete.
Also you should never call Thread.sleep(100)
in the paint
method. This blocks the EDT
and degrades performance.
Instead use a Swing Timer to do the update and repainting work. Also I would sub-class a JComponent
and override paintComponent
.
这篇关于我试图用螺纹移动小应用程序中一球,但它的不动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!