-2
我一直在努力使一個類,其中有一個火箭移動執行,而另一個完全不同的類執行一個燃料棒減少。即使有線程,我的程序也會讓燃料棒達到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