gpt4 book ai didi

java - 如何从java中的同一位置创建多个对象

转载 作者:行者123 更新时间:2023-11-30 04:42:35 25 4
gpt4 key购买 nike

我正在尝试制作一款塔防游戏,但在制作时遇到了麻烦,因此当我单击底部的其中一个塔按钮并拖动到游戏区域时,它会创建一个新的对象塔。我尝试过使用数组列表,但每次我拖动以创建新塔时,前一个塔都会被删除,而新塔仍会绘制在屏幕上。

有几个类(class),所以我只会发布我认为相关的类(class)。如果你需要其他的我可以把它们放上来。提前抱歉我的帖子太长。

这是处理事件的类 包addison;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class Controls extends JPanel implements MouseListener, MouseMotionListener, ActionListener {

Timer timer;

public static int x; //mouse's x pos
public static int y; //mouse's y pos
public static int iteration = 0;

public static String button = ""; //identifies pressed button

public static boolean pressed = false; //true if a button was pressed and not a random space
public static boolean created = false;

public static ArrayList<Tower> tower = new ArrayList<Tower>(); //arraylist of tower objects

public Controls() {

timer = new Timer(5, this); //creates a 5ms timer
timer.start(); //starts timer
addMouseListener(this);
addMouseMotionListener(this);
setFocusable(true);
}

public void paintComponent(Graphics g) {

super.paintComponent(g);

Map.paint(g); //draws map
Hud.paint(g); //draws HUD
TextDisplay.paint(g); //displays text
}

public void actionPerformed(ActionEvent e) {

repaint(); //redraws graphics every 5ms
}

@Override
public void mouseClicked(MouseEvent e) {


}

@Override
public void mouseEntered(MouseEvent e) {


}

@Override
public void mouseExited(MouseEvent e) {


}

@Override
public void mousePressed(MouseEvent e) {

if (Hud.getButton()) {

pressed = true;
} else {

pressed = false;
}

System.out.println(pressed);
}

@Override
public void mouseReleased(MouseEvent e) {

x = e.getX(); //gets mouse's x pos
y = e.getY(); //gets mouse's y pos

if (pressed) { // if the button pressed was gun man

tower.add(iteration, new Tower(TextDisplay.description, x, y, 100, 100, 25)); //add a new tower object to the end of the arraylist

System.out.println(tower.get(0).x);

created = true;
pressed = false;
}

iteration++;
}

@Override
public void mouseDragged(MouseEvent e) {


}

@Override
public void mouseMoved(MouseEvent e) {

x = e.getX(); //get mouse's x pos
y = e.getY(); //get mouse;s y pos

if (Hud.getButton()) {

TextDisplay.hovering = true;
} else {

TextDisplay.hovering = false;
}
}
}

package addison;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

这是绘制所有内容的类 公共(public)类 map {

public static Rectangle mouse; //location of cursor

public Map() {

}

public static void paint(Graphics g) {

g.setColor(new Color(255, 0, 0)); //makes mouse hit box transparent
mouse = new Rectangle(Controls.x, Controls.y, 5, 5); //create mouse hit box
g.fillRect(mouse.x, mouse.y, mouse.width, mouse.height); //draw mouse hit box


g.drawRect(0, 0, 800, 450); //play area

g.drawRect(0, 0, 800, 500); //options area

if (Controls.created) {

g.fillRect(Controls.tower.get(Controls.iteration).x, Controls.tower.get(0).y, 50, 50);
}
}
}

这是按钮所在的类: 包addison;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Hud {

public static Rectangle gunman;
public static Rectangle laserTower;
public static Rectangle rocketLauncher;
public static Rectangle emBomb;
public static Rectangle soundGun;

public Hud() {

}

public static void paint(Graphics g) {

gunman = new Rectangle(0, 450, 50, 50); //create gun man button
g.fillRect(gunman.x, gunman.y, gunman.width, gunman.height); //draw gun man button

g.setColor(Color.BLUE);
laserTower = new Rectangle(50, 450, 50, 50); //create laser tower button
g.fillRect(laserTower.x, laserTower.y, laserTower.width, laserTower.height); //draw laser tower button

g.setColor(Color.CYAN);
rocketLauncher = new Rectangle(100, 450, 50, 50); //create rocket launcher tower
g.fillRect(rocketLauncher.x, rocketLauncher.y, rocketLauncher.width, rocketLauncher.height); //draw rocket launcher button

g.setColor(Color.DARK_GRAY);
emBomb = new Rectangle(150, 450, 50, 50); //creates em bomb button
g.fillRect(emBomb.x, emBomb.y, emBomb.width, emBomb.height); //draw em bomb button

g.setColor(Color.GREEN);
soundGun = new Rectangle(200, 450, 50, 50); //create sound gun button
g.fillRect(soundGun.x, soundGun.y, soundGun.width, soundGun.height); //draw sound gun button
}

public static boolean getButton() {

if(Map.mouse.intersects(Hud.gunman)) {

TextDisplay.description = "Gunman";
return true;
} else if (Map.mouse.intersects(Hud.laserTower)) {

TextDisplay.description = "Laser Tower";
return true;
} else if (Map.mouse.intersects(Hud.rocketLauncher)) {

TextDisplay.description = "Rocket Launcher";
return true;
} else if (Map.mouse.intersects(Hud.emBomb)) {

TextDisplay.description = "E.M. Bomb";
return true;
} else if (Map.mouse.intersects(Hud.soundGun)) {

TextDisplay.description = "Sound Gun";
return true;
} else {

TextDisplay.description = "";
return false;
}
}
}

这是生成 Tower 对象的类: 包addison;

public class Tower {

public static String type = ""; //type of tower e.g. gunman or laser tower
public static int x = 0;
public static int y = 0;
public static int range = 0; //tower range
public static int speed = 0; //tower speed
public static int sRange = 0; //tower's shrapnel range

public Tower(String a, int b, int c, int d, int e, int f) {

type = a;
x = b;
y = c;
range = d;
speed = e;
sRange = f;
}
}

谢谢。

最佳答案

因为您仅在以下行的最后一次迭代中绘制塔:

g.fillRect(Controls.tower.get(Controls.iteration).x, Controls.tower.get(0).y, 50, 50);

您需要在绘制函数中绘制所有塔楼

关于java - 如何从java中的同一位置创建多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11911136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com