Хорошо, так что мой вопрос заключается в том, как я могу сделать две одинаковые вещи, пока они оба имеют одни и те же атрибуты, но могут действовать независимо, например, я работаю над строителем города, когда использование нажимает кнопку для добавления масляной электростанции, электростанция будет добавлена в мир, однако есть только одна. как я мог сделать это так, чтобы игрок мог сделать бесшовное количество одного и того же здания, но все они действуют независимо, например, когда я иду добавить вторую часть того же здания, первая не будет следовать за мышью. Heres мой текущий код, чтобы помочь объяснить мой вопрос:Как я мог бы создать две вещи из 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;
}
}
Клонировать его и это клон. –
Object.clone(), это то, что вы просите? Реализуйте свой класс как Clonable, и выполните реализацию метода для копирования, как хотите, и здесь u go –