1

好的,所以我的问题是,在摇摆中,我怎么能制作两个相同的东西,而它们都具有相同的属性但又可以独立行动,例如,我正在开发一个城市建设者,当用户按下按钮时加一个石油发电站,这个发电站就会加到世界上,然而,只有一个。我怎样才能做到这一点,以便玩家可以无缝地制作同一建筑物,但它们都独立行动,例如,当我要添加同一建筑物的第二个时,第一个不会跟随鼠标。这是我当前的代码来帮助解释我的问题:

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

2 回答 2

1

您实际上需要创建 2 个具有相似属性的不同实例或尝试以下代码段:

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

并用building.isLockedInPlace来说如果你有一个下来。要拥有多个,您必须使用Object.clone();.

希望它有效并且编码愉快!

于 2013-05-18T22:43:02.823 回答
1

根据您的问题描述和您提供的代码,您似乎是Java 所围绕的面向对象编程 (OOP)的新手。如果您还没有这样做,我建议您再次阅读核心 Java 教程,了解对象及其使用。

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

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

这种结构是可扩展的,允许您轻松添加更多建筑物和建筑类型。您还可以更好地执行数据封装(您在这里根本没有这样做),因为每个具体的建筑类(如您的 ApartmentBlock 和 PowerPlant)也将是唯一关心他们显示的图像和其他数据构建的人只有他们真正需要知道的具体信息。

于 2013-05-18T22:45:49.230 回答