我怎么可能在java中创建两个相同的东西

时间:2013-05-18 22:17:18

标签: java object builder city

好吧,所以我的问题是,在摇摆中,我怎么能做出两个相同的东西,虽然它们都具有相同的属性,但却可以独立行动,例如,我正在做一个城市建设者,当用户按下时一个按钮添加一个油电站,电站将添加到世界,但是,只有一个。我怎么能做到这一点,玩家可以制作同一建筑物的无缝数量,但他们都独立行动,例如当我去添加同一建筑物的第二个时,第一个不会跟随鼠标。 继承了我目前的代码,以帮助解释我的问题:

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;
}
 }

2 个答案:

答案 0 :(得分:1)

您实际上需要创建具有相似属性的2个不同实例,或者尝试使用此代码段:

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

并使用building.isLockedInPlace说明你是否有一个失败。 要拥有多个,您必须使用Object.clone();

希望它有效且编码愉快!

答案 1 :(得分:1)

根据您的问题描述和您提供的代码,您似乎不熟悉Java所围绕的object-oriented programming (OOP)。如果您还没有这样做,我建议您再次查看the core Java tutorials,了解对象及其用途。

目前,您已将所有内容都包含在一个文件中:您的发电厂,公寓楼的属性等。您的程序应如下所示:

  • 包含一个或一组City对象的Game类(您已经拥有)。
  • 包含建筑物对象集合的城市类。
  • Building类可以是抽象的,也可以是您的ApartmentBlock和PowerPlant类将扩展或实现的接口。

此结构具有可扩展性,可让您轻松添加更多建筑物和建筑类型。你也可以更好地执行data encapsulation(这里你根本没有做),因为每个具体的建筑类(如你的ApartmentBlock和PowerPlant)也是唯一关心他们正在展示什么图像的人,以及其他数据构建只有他们真正需要知道的特定信息。