java线程问题~~
效果是显示做自由落体的小球在Frame中上下运动到达底部弹回。不损失速度~~就是能量守恒~~下边是我的程序importjava.awt.*;importjava.awt....
效果是显示做自由落体的小球在Frame中上下运动
到达底部弹回。不损失速度~~就是能量守恒~~
下边是我的程序
import java.awt.*;
import java.awt.event.*;
public class Theball extends Frame implements Runnable {
int height=0;
long t=0;
Thread newt=new Thread(this);
Theball() {
setSize(300, 300);
setVisible(true);
newt.start();
repaint();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public synchronized void paint(Graphics g)
{
g.drawOval(150, height, 10, 10);
}
public void run()
{
while(true)
{
while(height<300)
{
height=(int)(5*t/1000*t/1000);
t+=50;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {System.out.println("线程问题");
}
}
t=0;int v=(int)Math.sqrt(2*10*height);
while(height>0)
{
height=(int)(300-(v*t/1000-5*t/1000*t/1000));
t+=50;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {System.out.println("线程问题");
}
}
t=0;
}
}
public static void main(String args[])
{
Theball tb=new Theball();
}
}
结果显示的小球好像是速度不对?
难道是线程之间在两个while之间切换吗?
但是我感觉上它是一个线程~~只要sleep一个线程就会都停的
求教高手~~~~~ 展开
到达底部弹回。不损失速度~~就是能量守恒~~
下边是我的程序
import java.awt.*;
import java.awt.event.*;
public class Theball extends Frame implements Runnable {
int height=0;
long t=0;
Thread newt=new Thread(this);
Theball() {
setSize(300, 300);
setVisible(true);
newt.start();
repaint();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public synchronized void paint(Graphics g)
{
g.drawOval(150, height, 10, 10);
}
public void run()
{
while(true)
{
while(height<300)
{
height=(int)(5*t/1000*t/1000);
t+=50;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {System.out.println("线程问题");
}
}
t=0;int v=(int)Math.sqrt(2*10*height);
while(height>0)
{
height=(int)(300-(v*t/1000-5*t/1000*t/1000));
t+=50;
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {System.out.println("线程问题");
}
}
t=0;
}
}
public static void main(String args[])
{
Theball tb=new Theball();
}
}
结果显示的小球好像是速度不对?
难道是线程之间在两个while之间切换吗?
但是我感觉上它是一个线程~~只要sleep一个线程就会都停的
求教高手~~~~~ 展开
展开全部
class Theball extends Frame {
int height = 0;
int t = 0;
Theball() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
final Panel panel = new Panel() {
@Override
public void paint(Graphics g) {
super.paint(g);
t++;
g.drawOval(150, height, 10, 10);
}
};
panel.setSize(300, 300);
add(panel);
setSize(300, 300);
setVisible(true);
int delay = 25; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int size = panel.getSize().height-10;
height = t % size;
if ((t / size) % 2 != 0) {
height = size - height;
}
panel.repaint();
}
};
Timer timer = new Timer(delay, taskPerformer);
timer.setDelay(25);
timer.setRepeats(true);
timer.start();
}
public static void main(String args[]) {
Theball tb = new Theball();
}
}
最好别用线程,线程的启动不是严格准时的,我这里的定时器是个没有开启线程的定时器。效果比你的要好。
int height = 0;
int t = 0;
Theball() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
final Panel panel = new Panel() {
@Override
public void paint(Graphics g) {
super.paint(g);
t++;
g.drawOval(150, height, 10, 10);
}
};
panel.setSize(300, 300);
add(panel);
setSize(300, 300);
setVisible(true);
int delay = 25; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int size = panel.getSize().height-10;
height = t % size;
if ((t / size) % 2 != 0) {
height = size - height;
}
panel.repaint();
}
};
Timer timer = new Timer(delay, taskPerformer);
timer.setDelay(25);
timer.setRepeats(true);
timer.start();
}
public static void main(String args[]) {
Theball tb = new Theball();
}
}
最好别用线程,线程的启动不是严格准时的,我这里的定时器是个没有开启线程的定时器。效果比你的要好。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询