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