1

さて、私の質問は、スイングで、同じ属性を持ちながら独立して動作できる同じものを 2 つ作成するにはどうすればよいかということです。たとえば、私は都市建設に取り組んでいます。石油発電所を追加すると、発電所がワールドに追加されますが、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、あなたが1つダウンしているかどうかを言うのに使用します。複数ある場合は、 を使用する必要がありますObject.clone();

それがうまくいくことを願って、幸せなコーディングをしてください!

于 2013-05-18T22:43:02.823 に答える
1

問題の説明と提供されたコードに基づくと、Java が中心となるオブジェクト指向プログラミング (OOP)は初めてのようです。まだ行っていない場合は、オブジェクトとその使用法に関するコア Java チュートリアルをもう一度確認することをお勧めします。

現状では、1 つのファイルにすべてが含まれています: 発電所の属性、アパートのブロックなどです。プログラムは次のようになります。

  • City オブジェクトの 1 つまたはコレクションを含む Game クラス (既にある)。
  • Building オブジェクトのコレクションを含む City クラス。
  • Building クラスは、ApartmentBlock および PowerPlant クラスが拡張または実装する抽象クラスまたはインターフェイスにすることができます。

この構造は拡張可能で、建物や建物の種類を簡単に追加できます。また、データのカプセル化をより適切に実施することもできます(ここではまったく行っていません)。それぞれの具体的な建物クラス (ApartmentBlock や PowerPlant など) は、表示されている画像や他のデータ構築を気にする唯一のクラスでもあるためです。彼らだけが本当に知る必要がある特定の情報。

于 2013-05-18T22:45:49.230 に答える