我一直试图创造一个类,它有一个火箭移动执行,而另一个完全不同的类执行一个减少的燃料棒。即使使用螺纹加工,我的程序也会使燃料棒达到0,然后火箭就会执行。 这是我火箭的代码:
import javax.swing.*;
import java.awt.*;
public class Rocket extends JFrame
{
ImageIcon Alien, BackGnd, Blast, Count1, Count2, Count3, Count4, Count5, Laser;
ImageIcon RocketFall, RocketRight, RocketUp, RocketStill, RocketLand, Explosion, RocketBroke;
Thread healthBar, Rocketeer;
int i = 150;
DebugMonitor dM = new DebugMonitor ();
public Rocket ()
{
super ("The Rocket Program");
setSize (900, 526);
Toolkit dir = Toolkit.getDefaultToolkit ();
Alien = new ImageIcon (dir.getImage ("Alien.png"));
BackGnd = new ImageIcon (dir.getImage ("Background.png"));
Blast = new ImageIcon (dir.getImage ("Blastoff.png"));
Count1 = new ImageIcon (dir.getImage ("CountDown1.png"));
Count2 = new ImageIcon (dir.getImage ("CountDown2.png"));
Count3 = new ImageIcon (dir.getImage ("CountDown3.png"));
Count4 = new ImageIcon (dir.getImage ("CountDown4.png"));
Count5 = new ImageIcon (dir.getImage ("CountDown5.png"));
Laser = new ImageIcon (dir.getImage ("Laser.png"));
RocketFall = new ImageIcon (dir.getImage ("RocketFall.png"));
RocketRight = new ImageIcon (dir.getImage ("RocketTurned.png"));
RocketUp = new ImageIcon (dir.getImage ("RocketMove.png"));
RocketStill = new ImageIcon (dir.getImage ("RocketStill.png"));
RocketLand = new ImageIcon (dir.getImage ("RocketLand.png"));
Explosion = new ImageIcon (dir.getImage ("Explosion.png"));
RocketBroke = new ImageIcon (dir.getImage ("RocketBroken.png"));
healthBar = new Thread (dM);
healthBar.start ();
setVisible (true);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void paint (Graphics g)
{
count (g);
fast (g);
// endProgram ();
}
public void endProgram ()
{
try
{
Thread.sleep (5000);
//dM.endProgram = true;
System.exit (0);
}
catch (Exception e)
{
}
}
public void count (Graphics g)
{
try
{
BackGnd.paintIcon (this, g, 0, 0);
RocketStill.paintIcon (this, g, 5, 440);
Count5.paintIcon (this, g, 400, 400);
Thread.sleep (1000);
BackGnd.paintIcon (this, g, 0, 0);
RocketStill.paintIcon (this, g, 5, 440);
Count4.paintIcon (this, g, 400, 400);
Thread.sleep (1000);
BackGnd.paintIcon (this, g, 0, 0);
RocketStill.paintIcon (this, g, 5, 440);
Count3.paintIcon (this, g, 400, 400);
Thread.sleep (1000);
BackGnd.paintIcon (this, g, 0, 0);
RocketStill.paintIcon (this, g, 5, 440);
Count2.paintIcon (this, g, 400, 400);
Thread.sleep (1000);
BackGnd.paintIcon (this, g, 0, 0);
RocketStill.paintIcon (this, g, 5, 440);
Count1.paintIcon (this, g, 400, 400);
Thread.sleep (1000);
BackGnd.paintIcon (this, g, 0, 0);
RocketStill.paintIcon (this, g, 5, 440);
Blast.paintIcon (this, g, 400, 400);
Thread.sleep (600);
}
catch (Exception a)
{
}
}
public void slow (Graphics g)
{
for (int i = 440 ; i > 30 ; i -= 5)
{
try
{
RocketUp.paintIcon (this, g, 5, i);
Thread.sleep (50);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
}
for (int i = 5, x = 340 ; i < 800 ; i += 5, x -= 5)
{
try
{
RocketRight.paintIcon (this, g, i, 35);
Thread.sleep (50);
System.out.println (i + " " + x);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
Alien.paintIcon (this, g, 400, 460);
Laser.paintIcon (this, g, 420, x - 30);
}
for (int i = 35 ; i < 400 ; i += 5)
{
try
{
RocketLand.paintIcon (this, g, 830, i);
Thread.sleep (50);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
Alien.paintIcon (this, g, 400, 460);
}
} //Method
public void medium (Graphics g)
{
for (int i = 440 ; i > 30 ; i -= 10)
{
try
{
RocketUp.paintIcon (this, g, 5, i);
Thread.sleep (50);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
}
for (int i = 5, x = 340, z = 0 ; z == 0 ; i += 10, x -= 10)
{
try
{
RocketRight.paintIcon (this, g, i, 35);
Thread.sleep (50);
System.out.println (i + " " + x);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
Alien.paintIcon (this, g, 400, 460);
Laser.paintIcon (this, g, 420, x + 50);
if (x == 30)
{
z = 1;
}
}
for (int i = 35, x = -50 ; i < 400 ; i += 10, x += 3)
{
try
{
RocketFall.paintIcon (this, g, 440 + x, i);
Thread.sleep (50);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
Alien.paintIcon (this, g, 400, 460);
}
RocketBroke.paintIcon (this, g, 500, 450);
} //Method
public void fast (Graphics g)
{
for (int i = 440 ; i > 30 ; i -= 15)
{
try
{
RocketUp.paintIcon (this, g, 5, i);
Thread.sleep (50);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
}
for (int i = 5, x = 340, z = 0 ; z == 0 ; i += 10, x -= 15)
{
try
{
RocketRight.paintIcon (this, g, i, 35);
Thread.sleep (50);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
Alien.paintIcon (this, g, 400, 460);
Laser.paintIcon (this, g, 420, x + 50);
if (x < 30)
{
z = 1;
}
}
for (int i = 35, x = -50 ; i < 400 ; i += 10, x += 8)
{
try
{
RocketFall.paintIcon (this, g, 300 + x, i);
Thread.sleep (50);
}
catch (Exception a)
{
}
BackGnd.paintIcon (this, g, 0, 0);
Alien.paintIcon (this, g, 400, 460);
}
RocketBroke.paintIcon (this, g, 500, 450);
} //Method
public static void main (String[] args)
{
new Rocket ();
// Place your code here
} // main method
} // Rocket class
这是我的燃料棒的代码:
import javax.swing.*;
import java.awt.*;
public class DebugMonitor extends JFrame implements Runnable
{
int i = 150;
boolean endProgram = false;
public DebugMonitor ()
{
super ("Debug Monitor");
setLocation (1000, 500);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setSize (151, 31);
setVisible (true);
}
public void run (Graphics g)
{
for (; i >= 0 ; i--)
{
try
{
Thread.sleep (40);
g.setColor (Color.BLACK);
g.fillRect (0, 0, 150, 30);
g.setColor (Color.YELLOW);
g.fillRect (0, 0, i, 30);
Thread.sleep (20);
}
catch (Exception e)
{
}
}
}
// public static void main (String[] args)
// {
/// new DebugMonitor ();
//} // main method
} // DebugMonitor class
答案 0 :(得分:1)
我认为两个线程不是一个好的解决方案。线程应尽可能以无关执行任务,因为未定义时序。这里的任务非常密切相关。使用一个线程同时更新燃料和火箭会更容易。