java - Can't seem to make these two classes run at once. Using Threads -
i've been trying make 1 class, has rocket moving execute while different class executes fuel bar decreases. threading, program makes fuel bar 0, rocket executes. here code rocket:
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 = 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) { (int = 440 ; > 30 ; -= 5) { try { rocketup.painticon (this, g, 5, i); thread.sleep (50); } catch (exception a) { } backgnd.painticon (this, g, 0, 0); } (int = 5, x = 340 ; < 800 ; += 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); } (int = 35 ; < 400 ; += 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) { (int = 440 ; > 30 ; -= 10) { try { rocketup.painticon (this, g, 5, i); thread.sleep (50); } catch (exception a) { } backgnd.painticon (this, g, 0, 0); } (int = 5, x = 340, z = 0 ; z == 0 ; += 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; } } (int = 35, x = -50 ; < 400 ; += 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) { (int = 440 ; > 30 ; -= 15) { try { rocketup.painticon (this, g, 5, i); thread.sleep (50); } catch (exception a) { } backgnd.painticon (this, g, 0, 0); } (int = 5, x = 340, z = 0 ; z == 0 ; += 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; } } (int = 35, x = -50 ; < 400 ; += 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 code here } // main method } // rocket class here code fuel bar:
import javax.swing.*; import java.awt.*; public class debugmonitor extends jframe implements runnable { int = 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) { (; >= 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
i think 2 threads not solution. threads should execute tasks unrelated possible, because timing not defined. here tasks closely related. easier use 1 thread update both fuel , rocket @ same time.
Comments
Post a Comment