java - Randomly generating numbers at the click of a JButton when a new JPanel is called -


i learning java , have chosen project both fun, yet semi challenging. project based upon old 70s - 80s game once played kid, called dopewars.

this second attempt @ building game, first attempt made putting code within 1 .java file, successful point of having stop result getting lost within mountains of code.

my second attempt, tried break code logical sections, shared between many, small sized .java files. thought method alot easier handle have found has created many more problems else.

what should happen: when navigating through running application, user prompted select his/her player. default starting country, user can purchase/sell of fine products on offer. user can press 'travel' jbutton (xx), choose destination pressing lets england jbutton(teng), , shipped off chosen destination reap rewards of investment. user lucky enough find deal on product on offer @ new destination aswell.

my problem: ever try cannot travel new destination. when click country want go to, same country im @ pops again. set create new 'country' object when button pressed. also, i'd following: when user leaves england, travels germany, , returns england. englands randomly generated prices refresh first time in england. or vice versa germany.

all can think problem lies england object, once opened, needs closed. allowing new england object created when user next navigates england, , generating new random prices user either buy or sell at.

my attempts, based on theory, have been somehow destroy, dispose or remove england object on press of jbutton xx (travels button).

i ideally keep bulk of code in place, radical changes confuse hell out of me. have removed code have failed with. appreciated. in advance.

playerselect.java:

package ggg;  import java.awt.color; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import ggg.gui;  public class playerselect implements actionlistener{  jframe fr; jpanel playerselect; jbutton playerselect1, playerselect2, playerselect3, playerselect4, playerselect5, playerselect6; jlabel q, n1, n2, n3, n4, n5, n6;  public playerselect(){      fr = new jframe("dopewars 2014");     fr.setsize(580, 445);     fr.setlocation(10, 10);     fr.setlayout(null);     fr.setbackground(color.black);     fr.setlocationrelativeto(null);     fr.setvisible(true);      playerselect = new jpanel();     playerselect.setsize(650, 500);     playerselect.setlocation(0, 0);     playerselect.setlayout(null);     playerselect.setbackground(color.black);     playerselect.setvisible(true);      playerselect1 = new jbutton();     playerselect1.setsize(177, 135);     playerselect1.setlocation(10, 115);     playerselect1.setlayout(null);     playerselect1.setbackground(color.pink);     playerselect1.addactionlistener(this);      playerselect2 = new jbutton();     playerselect2.setsize(177, 135);     playerselect2.setlocation(197, 115);     playerselect2.setlayout(null);     playerselect2.setbackground(color.green);     playerselect2.addactionlistener(this);      playerselect3 = new jbutton();     playerselect3.setsize(177, 135);     playerselect3.setlocation(384, 115);     playerselect3.setlayout(null);     playerselect3.setbackground(color.yellow);     playerselect3.addactionlistener(this);      playerselect4 = new jbutton();     playerselect4.setsize(177, 135);     playerselect4.setlocation(10, 265);     playerselect4.setlayout(null);     playerselect4.setbackground(color.magenta);     playerselect4.addactionlistener(this);      playerselect5 = new jbutton();     playerselect5.setsize(177, 135);     playerselect5.setlocation(197, 265);     playerselect5.setlayout(null);     playerselect5.setbackground(color.red);     playerselect5.addactionlistener(this);      playerselect6 = new jbutton();     playerselect6.setsize(177, 135);     playerselect6.setlocation(384, 265);     playerselect6.setlayout(null);     playerselect6.setbackground(color.blue);     playerselect6.addactionlistener(this);      q = new jlabel("select player begin.");     q.setsize(600, 50);     q.setlocation(10, 50);     q.setforeground(color.white);     q.setfont(q.getfont().derivefont(18.0f));      n1 = new jlabel("paul");     n1.setsize(157, 25);     n1.setlocation(10, 10);     n1.setbackground(color.white);      n2 = new jlabel("richard");     n2.setsize(157, 25);     n2.setlocation(10, 10);     n2.setbackground(color.white);      n3 = new jlabel("lisa");     n3.setsize(157, 25);     n3.setlocation(10, 10);     n3.setbackground(color.white);      n4 = new jlabel("roger");     n4.setsize(157, 25);     n4.setlocation(10, 10);     n4.setbackground(color.white);      n5 = new jlabel("katherine");     n5.setsize(157, 25);     n5.setlocation(10, 10);     n5.setbackground(color.white);      n6 = new jlabel("harold");     n6.setsize(157, 25);     n6.setlocation(10, 10);     n6.setbackground(color.white);        playerselect1.add(n1);     playerselect2.add(n2);     playerselect3.add(n3);     playerselect4.add(n4);     playerselect5.add(n5);     playerselect6.add(n6);      playerselect.add(q);     playerselect.add(playerselect1);     playerselect.add(playerselect2);     playerselect.add(playerselect3);     playerselect.add(playerselect4);     playerselect.add(playerselect5);     playerselect.add(playerselect6);     fr.add(playerselect);  }  @override public void actionperformed(actionevent a) {          gui b = new gui();         england e = new england();          if (a.getsource() == playerselect1){                 b.img.setbackground(color.pink);                 b.img.add(n1);                 b.bg.add(e.england);                 fr.dispose();         }         if (a.getsource() == playerselect2){                 b.img.setbackground(color.green);                 b.img.add(n2);                 b.bg.add(e.england);                 fr.dispose();         }         if (a.getsource() == playerselect3){                 b.img.setbackground(color.yellow);                 b.img.add(n3);                 b.bg.add(e.england);                 fr.dispose();         }         if (a.getsource() == playerselect4){                 b.img.setbackground(color.magenta);                 b.img.add(n4);                 b.bg.add(e.england);                 fr.dispose();         }         if (a.getsource() == playerselect5){                 b.img.setbackground(color.red);                 b.img.add(n5);                 b.bg.add(e.england);                 fr.dispose();         }         if (a.getsource() == playerselect6){                 b.img.setbackground(color.blue);                 b.img.add(n6);                 b.bg.add(e.england);                 fr.dispose();         }         } } 

gui.java:

package ggg;  import java.awt.color; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import ggg.england; import ggg.germany; import ggg.engine;  public class gui extends engine implements actionlistener {      jframe f;     jpanel bg, s, img, t;     jlabel £, ca, co;     static jlabel cav, cov;     jbutton x, xx, y, z, bteng, btgerm;      public gui(){            f = new jframe("fff");             bg = new jpanel();            bg.setsize(650, 500);            bg.setbackground(color.darkgray);            bg.setlayout(null);            bg.setlocation(0, 0);             s = new jpanel();            s.setsize(180, 445);            s.setbackground(color.white);            s.setlayout(null);            s.setlocation(10, 10);             img = new jpanel();            img.setsize(160, 120);            img.setbackground(color.blue);            img.setlayout(null);            img.setlocation(10, 10);             x = new jbutton("travel");            x.setsize(136, 30);            x.setlocation(202, 10);            x.addactionlistener(this);             xx = new jbutton("travels");            xx.setsize(136, 30);            xx.setlocation(202, 10);            xx.addactionlistener(this);            xx.setvisible(false);             y = new jbutton("finance");            y.setsize(136, 30);            y.setlocation(349, 10);            y.addactionlistener(this);             z = new jbutton("investment");            z.setsize(136, 30);            z.setlocation(496, 10);            z.addactionlistener(this);                 // stats window            ca = new jlabel("cash:");            ca.setsize(70,25);            ca.setlocation(10, 145);             £ = new jlabel("£");            £.setsize(10, 25);            £.setlocation(80, 145);             cav = new jlabel();            cav.setsize(80, 25);            cav.setlocation(90, 145);            int aaa = engine.playercash;            string bbb = string.valueof(aaa);            cav.settext(bbb);             co = new jlabel("cocaine:");            co.setsize(70, 25);            co.setlocation(10, 190);             cov = new jlabel();            cov.setsize(70, 25);            cov.setlocation(80, 190);            int ccc = engine.playercoca;            string ddd = string.valueof(ccc);            cov.settext(ddd);             //travel window            t = new jpanel();            t.setsize(430, 403);            t.setbackground(color.white);            t.setlayout(null);            t.setlocation(202, 52);            t.setvisible(false);             bteng = new jbutton("england");            bteng.setsize(120, 25);            bteng.setlocation(10, 100);            bteng.addactionlistener(this);             btgerm = new jbutton("germany");            btgerm.setsize(120, 25);            btgerm.setlocation(140, 100);            btgerm.addactionlistener(this);             f.setsize(650, 500);            f.setlayout(null);            f.setlocationrelativeto(null);            f.setdefaultcloseoperation(jframe.exit_on_close);            f.setvisible(true);                // add component            s.add(img);            s.add(ca);            s.add(£);            s.add(cav);            s.add(co);            s.add(cov);             t.add(bteng);            t.add(btgerm);                       bg.add(s);            bg.add(x);            bg.add(xx);            bg.add(y);            bg.add(z);            bg.add(t);                   f.add(bg);     }          @override     public void actionperformed(actionevent a) {            england e = new england();            germany g = new germany();             if (a.getsource() == x){                t.setvisible(true);            }            if (a.getsource() == y){             }            if (a.getsource() == z){             }            if (a.getsource() == xx){                t.setvisible(true);                x.setvisible(true);                xx.setvisible(false);            }            if (a.getsource() == bteng){                bg.add(e.england);                bg.remove(g.germany);///////////                xx.setvisible(true);                x.setvisible(false);                t.setvisible(false);            }            if (a.getsource() == btgerm){                bg.add(g.germany);                bg.remove(e.england);                xx.setvisible(true);                x.setvisible(false);                t.setvisible(false);            }     } } 

engine.java:

package ggg;  import javax.swing.jlabel; import ggg.england; import ggg.gui;  public class engine {      public static int playercash = 120;     public static int playercoca = 0;      public int cocaprice;     public int cocaquantity;     jlabel cp, cq;     public int cpp, cqq;     public string cqqq;     public int total;     public int deduction;      public void buyeng(){                 england.error.settext(null);              // cocaprice             cp = new jlabel();             cp.settext(england.pr.gettext());             cpp = integer.parseint(cp.gettext());             cocaprice = cpp;              // cocaquantity             cq = new jlabel();             cq.settext(england.tf.gettext());             cqqq = cq.gettext();             cqq = integer.parseint(cqqq);             cocaquantity = cqq;                    // calculations             total = cocaprice * cocaquantity;                  if (playercash >= total){                  playercoca = playercoca + cocaquantity;                 string q = string.valueof(playercoca);                  playercash = playercash - total;                 string t = string.valueof(playercash);                  gui.cav.settext(t);                 gui.cov.settext(q);                 england.tf.settext(null);                 return;             }             else {                 england.tf.settext(null);                 england.error.settext("you don't have enough money!");             }             return;     }        public void selleng(){              if (playercoca > 0){                  england.error.settext(null);                  // cocaprice                 cp = new jlabel();                 cp.settext(england.pr.gettext());                 cpp = integer.parseint(cp.gettext());                 cocaprice = cpp;                  // cocaquantity                 cq = new jlabel();                 cq.settext(england.tf.gettext());                 cqqq = cq.gettext();                 cqq = integer.parseint(cqqq);                 cocaquantity = cqq;                        // calculations                 total = cocaprice * cocaquantity;                    playercoca = playercoca - cocaquantity;                 string q = string.valueof(playercoca);                  playercash = playercash + total;                 string t = string.valueof(playercash);                  gui.cav.settext(t);                 gui.cov.settext(q);                 england.tf.settext(null);                 return;             }                         else if (playercoca <= 0){                  england.tf.settext(null);                 england.error.settext("you don't have sell!");             }             return;     }        public void buygerm(){            germany.error.settext(null);          // cocaprice         cp = new jlabel();         cp.settext(germany.pr.gettext());         cpp = integer.parseint(cp.gettext());         cocaprice = cpp;          // cocaquantity         cq = new jlabel();         cq.settext(germany.tf.gettext());         cqqq = cq.gettext();         cqq = integer.parseint(cqqq);         cocaquantity = cqq;                // calculations         total = cocaprice * cocaquantity;              if (playercash >= total){              playercoca = playercoca + cocaquantity;             string q = string.valueof(playercoca);              playercash = playercash - total;             string t = string.valueof(playercash);              gui.cav.settext(t);             gui.cov.settext(q);             germany.tf.settext(null);             return;         }         else {             germany.tf.settext(null);             germany.error.settext("you don't have enough money!");         }         return;     }        public void sellgerm(){          if (playercoca > 0){              germany.error.settext(null);              // cocaprice             cp = new jlabel();             cp.settext(germany.pr.gettext());             cpp = integer.parseint(cp.gettext());             cocaprice = cpp;              // cocaquantity             cq = new jlabel();             cq.settext(germany.tf.gettext());             cqqq = cq.gettext();             cqq = integer.parseint(cqqq);             cocaquantity = cqq;                    // calculations             total = cocaprice * cocaquantity;                playercoca = playercoca - cocaquantity;             string q = string.valueof(playercoca);              playercash = playercash + total;             string t = string.valueof(playercash);              gui.cav.settext(t);             gui.cov.settext(q);             germany.tf.settext(null);             return;         }                     else if (playercoca <= 0){              germany.tf.settext(null);             germany.error.settext("you don't have sell!");         }         return;     }          public static void main(string[] args) {         new playerselect();     } } 

england.java:

package ggg;  import java.awt.color; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.random; import javax.swing.jbutton; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import ggg.engine;  public class england extends engine implements actionlistener {  jpanel england, imgeng;  jlabel co2, £co2; static jlabel pr; static jlabel error; static jtextfield tf; jbutton b1, b2;   public england(){      england = new jpanel();     england.setsize(430, 403);     england.setbackground(color.red);     england.setlayout(null);     england.setlocation(202, 52);     england.setvisible(true);      imgeng = new jpanel();     imgeng.setsize(410, 80);     imgeng.setbackground(color.blue);     imgeng.setlayout(null);     imgeng.setlocation(10, 10);      co2 = new jlabel("cocaine");     co2.setsize(70, 25);     co2.setlocation(10, 100);      £co2 = new jlabel("£");     £co2.setsize(10, 25);     £co2.setlocation(80, 100);      pr = new jlabel();     pr.setsize(60, 25);     pr.setlocation(90, 100);     random random1 = new random();     int x = random1.nextint(80) + 1;     string c = string.valueof(x);     pr.settext(c);      tf = new jtextfield();     tf.setsize(70, 25);     tf.setlocation(160, 100);         b1 = new jbutton("buy");     b1.setsize(70, 25);     b1.setlocation(270, 100);     b1.addactionlistener(this);      b2 = new jbutton("sell");     b2.setsize(70, 25);     b2.setlocation(350, 100);     b2.addactionlistener(this);        // error message     error = new jlabel();     error.setsize(200, 25);     error.setlocation(10, 40);        // add components     england.add(imgeng);     imgeng.add(error);      england.add(co2);     england.add(£co2);     england.add(pr);     england.add(tf);     england.add(b1);     england.add(b2);     }    @override public void actionperformed(actionevent e) {      if (e.getsource() == b1){         engine = new engine();         a.buyeng();     }     if (e.getsource() == b2){         engine = new engine();         a.selleng();     }    } } 

germany.java:

package ggg;  import java.awt.color; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.random; import javax.swing.jbutton; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextfield; import ggg.engine;  public class germany extends engine implements actionlistener {  jpanel germany, imggerm;  jlabel co2, £co2; static jlabel pr; static jlabel error; static jtextfield tf; jbutton b1, b2;   public germany(){      germany = new jpanel();     germany.setsize(430, 403);     germany.setbackground(color.blue);     germany.setlayout(null);     germany.setlocation(202, 52);     germany.setvisible(true);      imggerm = new jpanel();     imggerm.setsize(410, 80);     imggerm.setbackground(color.red);     imggerm.setlayout(null);     imggerm.setlocation(10, 10);      co2 = new jlabel("cocaine");     co2.setsize(70, 25);     co2.setlocation(10, 100);      £co2 = new jlabel("£");     £co2.setsize(10, 25);     £co2.setlocation(80, 100);      pr = new jlabel();     pr.setsize(60, 25);     pr.setlocation(90, 100);     random random1 = new random();     int x = random1.nextint(80) + 1;     string c = string.valueof(x);     pr.settext(c);      tf = new jtextfield();     tf.setsize(70, 25);     tf.setlocation(160, 100);         b1 = new jbutton("buy");     b1.setsize(70, 25);     b1.setlocation(270, 100);     b1.addactionlistener(this);      b2 = new jbutton("sell");     b2.setsize(70, 25);     b2.setlocation(350, 100);     b2.addactionlistener(this);        // error message     error = new jlabel();     error.setsize(200, 25);     error.setlocation(10, 40);        // add components     germany.add(imggerm);     imggerm.add(error);      germany.add(co2);     germany.add(£co2);     germany.add(pr);     germany.add(tf);     germany.add(b1);     germany.add(b2);     }    @override public void actionperformed(actionevent e) {      if (e.getsource() == b1){         engine = new engine();         a.buygerm();     }     if (e.getsource() == b2){         engine = new engine();         a.sellgerm();     }    } } 

"my problem: ever try cannot travel new destination. when click country want go to, same country im @ pops again."

i think what's going on you're missing whole idea of extending class does. in no way makes them reference of same inherited objects. in order happen, reference need pass of same object.

what see doing create new engine() every time button pressed. makes perfect sense why you're getting same country popping up.

what recommend id maybe using cardlayout. since england , germany objects seem consist of jpanel components , logic, can make classes extends jpanel. add panels main panel cardlayout. when want go different countries, change panel of cardlayout. see this example foe how use cardlayout can go through how use cardlayout tutorial


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -