2013-05-18 48 views
1

好吧,所以我的問題是,在擺動中,我怎麼能做兩個同樣的事情,雖然他們都具有相同的屬性,但可以獨立行事,例如,我正在一個城市建設者工作,當用戶按下按鈕添加一個石油發電站時,電站將被添加到世界上,但是,只有一個。我怎麼能這樣做,使玩家可以做一個無縫的同一建築物,但他們都獨立行事,例如,當我去添加第二個同樣的建築,第一個不會跟隨鼠標。 繼承人我當前的代碼,以幫助解釋我的問題:我怎麼可能在java中創建兩個相同的東西

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 A 
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 A 
    ImageIcon apartmentBlockI = new ImageIcon("C:/Programs/Eclipse/eclipse/CityCenterBeta/bin/apartment block.jpg"); 
    apartmentBlockA = apartmentBlockI.getImage(); 



    //Set up game 
     addKeyListener(new AL()); 
     addMouseListener(new Mouse()); 
     init(); 




} 


private Image dbImage; 
private Graphics dbg; 


public static void main(String[] args) { 
    new Game(); 
} 



//When the program runs, thins are 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; 
} 
} 
+3

克隆它,這就是一個克隆。 –

+0

Object.clone(),你在問什麼?實現你的類是可複製的,並且做方法實現複製,因爲你想和你在這裏 –

回答

1

實際上,你需要創建2種不同的情況下,與同類性質或試試這個片斷:

Object building = new Object(); 
building.isLockedInPlace = false; 

並使用building.isLockedInPlace地說,如果你有完成了一個。 要有多個,您將不得不使用Object.clone();

希望它工作和快樂的編碼!

1

根據您的問題描述和您提供的代碼,看起來您是object-oriented programming (OOP)的新手,它是Java圍繞的。如果你還沒有這樣做,我建議再次通過the core Java tutorials,關於對象和它們的使用。在一個文件中

因爲它的立場,已包含了一切:您的電廠的屬性,你的公寓樓,等你的程序應該是什麼樣子的是以下幾點:

  • 一個遊戲類(您已經擁有),其中包含一個或一組City對象。
  • 城市類,其中包含建築物對象的集合。
  • Building類可以是抽象的或接口,您的ApartmentBlock和PowerPlant類將擴展或實現。

這個結構是可擴展的,並允許您輕鬆地添加更多的建築物和建築物類型。你也可以更好地執行data encapsulation(這裏你根本沒有這樣做),因爲每個具體的建築類(如你的ApartmentBlock和PowerPlant)也將是唯一關心他們顯示的圖像和其他數據構建特定的只有他們真的需要知道的信息。

相關問題