object - How could I possibly create two of the same thing in java -


okay, question is, in swing, how make 2 of same thing while both have same attributes yet can act independently, example, working on city builder, when uses presses button add oil power station, power station added world, however, there one. how make player make seamless amount of same building yet act independently, e.g when go add second of same building first 1 won't follow mouse. heres current code explain issue:

import java.awt.color; import java.awt.graphics; import java.awt.image; import java.awt.event.keyadapter; import java.awt.event.keyevent; import java.awt.event.mouseadapter; import java.awt.event.mouseevent;  import javax.swing.imageicon; import javax.swing.jframe;    public class game extends jframe{    public image map; public image utilbutton; public image resbutton; public image oilplantbox; public image apartmentblockabox;  //building img public image oilpowerstation; public image apartmentblocka;  //util selects boolean showutil = false; boolean utilselect = false;  //residential selects boolean showres = false; boolean resselect = false;  //oil power station boolean showoppbox = true; boolean checkoilpowerplant = false; boolean drawoilplant = false; boolean setpowerstation = false; boolean placeoilpowerplant = true; int oilpowerstationxx = 0; int oilpowerstationy = 0;  //apartment block boolean showaba = true; boolean checkapartmentblocka = false; boolean drawapartmentblocka = false; boolean setapartmentblocka = false; boolean placeapartmentblocka = true; int apartmentblockax = 0; int apartmentblockay = 0;    int x; int y;   public int power = 0; int jobs = 0;   public game(){      //load images:     imageicon mapi = new imageicon("c:/programs/eclipse/eclipse/citycenterbeta/bin/map.jpg");     map = mapi.getimage();     imageicon utilbuttoni = new imageicon("c:/programs/eclipse/eclipse/citycenterbeta/bin/util.jpg");     utilbutton = utilbuttoni.getimage();     imageicon resbuttoni = new imageicon("c:/programs/eclipse/eclipse/citycenterbeta/bin/res.jpg");     resbutton = resbuttoni.getimage();     imageicon opb = new imageicon("c:/programs/eclipse/eclipse/citycenterbeta/bin/oilplantbox.png");     oilplantbox = opb.getimage();     imageicon abab = new imageicon("c:/programs/eclipse/eclipse/citycenterbeta/bin/apartmentablock.jpg");     apartmentblockabox = abab.getimage();      //building images      //oil power station     imageicon oilplanti = new imageicon("c:/programs/eclipse/eclipse/citycenterbeta/bin/oilplant.jpg");     oilpowerstation = oilplanti.getimage();      //apartment block     imageicon apartmentblocki = new imageicon("c:/programs/eclipse/eclipse/citycenterbeta/bin/apartment block.jpg");     apartmentblocka = apartmentblocki.getimage();        //set game       addkeylistener(new al());        addmouselistener(new mouse());        init();     }   private image dbimage; private graphics dbg;   public static void main(string[] args) {   new game(); }    //when program runs, thins initialised here public void init(){      windowmanager(); }    public void paintcomponent(graphics g){      g.drawimage(map,0,0,null);     g.drawimage(utilbutton,20,100,null);     g.drawimage(resbutton,20,200,null);       if(showutil == true){         if(showoppbox == true){         g.drawimage(oilplantbox,190,130,null);         }     }     if(showres == true){         if(showaba == true){             g.drawimage(apartmentblockabox,190,130,null);         }     }      if(drawoilplant == true){         g.drawimage(oilpowerstation,oilpowerstationxx,oilpowerstationy,null);         if(checkoilpowerplant == true){         setpowerstation = true;         }      if(drawapartmentblocka == true){         g.drawimage(apartmentblocka,apartmentblockax,apartmentblockay,null);         if(checkapartmentblocka == true){             setapartmentblocka = true;         }     }     }          repaint(); }  public void paint(graphics g){      dbimage = createimage(getwidth(), getheight());     dbg = dbimage.getgraphics();     paintcomponent(dbg);     g.drawimage(dbimage,0,0,this);     }  public void windowmanager(){        jframe f = new jframe();       settitle("city center");       setvisible(true);       setresizable(false);       setbackground(color.black);       setdefaultcloseoperation(exit_on_close);       setextendedstate(f.maximized_both);       setundecorated(true);   }   public class al extends keyadapter{      public void keypressed(keyevent e){         int keycode = e.getkeycode();         if(keycode == e.vk_enter){             if(setpowerstation == true)                 placeoilpowerplant = false;                 checkoilpowerplant = false;                 setpowerstation = false;                          showutil = false;                 utilselect = false;                 showoppbox = false;                 oilplantatt();                 system.out.println(jobs + " job openings");                 system.out.println(power + "mw");                 }         if(setapartmentblocka == true){             placeapartmentblocka = false;             checkapartmentblocka = false;             setapartmentblocka = false;             showres = false;             resselect = false;             showaba = false;             apartmentblockatt();         }      }     public void keyreleased(keyevent e){       }   }  public class mouse extends mouseadapter {  public void mousepressed(mouseevent e) {      x = e.getx();      y = e.gety();      //pressed utilies button      if((x > 20) && (x < 120) && (y > 100) && (y < 200) && (showutil == false))          {             showutil = true;             utilselect = true;                   showres = false;             resselect = false; }      //pressed residential button      if((x > 20) && (x < 120) && (y > 200) && (y < 300) && (showres == false)){         showres = true;         resselect = true;         showutil = false;         utilselect = false;      }       if((x > 190) && (x < 265) && (y > 130) && (y < 192)){          if(resselect == true){              drawapartmentblocka = true;              if(placeapartmentblocka == true){                  checkapartmentblocka = true;              }          }          if(utilselect == true){              drawoilplant = true;                 if(placeoilpoerplant == true){                 checkoilpowerplant = true;           }        }     }     if(setpowerstation == true){         oilpowerstationxx = x;         oilpowerstationy = y;     }else{         oilpowerstationxx = oilpowerstationxx;         oilpowerstationy = oilpowerstationy;     }     if(setapartmentblocka == true){         apartmentblockax = x;         apartmentblockay = y;     }else{         apartmentblockax = apartmentblockax;         apartmentblockay = apartmentblockay;     }  }     }             public void oilplantatt(){  jobs = jobs + 150; power = power + 1000; } public void apartmentblockatt(){ boolean work = false; if(power > 0){     work = true; } if(work == true){ jobs = jobs - 300; power = power - 100; }  } 

you need create 2 different instances similar properties or try snippet:

object building = new object(); building.islockedinplace = false; 

and use building.islockedinplace if have 1 down. have multiple, have use object.clone();.

hope works , happy coding!


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -