[英]Painting a group of objects in a JPanel
I'm pretty new to Java and the GUI world.我对 Java 和 GUI 世界还很陌生。 Right now I'm trying to create a really basic space shooter.现在我正在尝试创建一个非常基本的太空射击游戏。 To create it I started creating a JFrame
, in which I've later on put a personal extension of a JPanel
called GamePanel
, on which I'm now trying to display all my components.为了创建它,我开始创建JFrame
,后来我在其中放置了一个名为GamePanel
的JPanel
的个人扩展,我现在正试图在其上显示我的所有组件。 Until here it's all pretty clear, the problem comes now: I have my GamePanel
in which I display my player, and on the KeyEvent
of pressing S the player should shoot the Bullets.直到这里一切都很清楚,现在问题来了:我有我的GamePanel
,我在其中显示我的玩家,并且在按下 S 的KeyEvent
上,玩家应该射击子弹。 I've managed the bullets as an Array, called Shooter[]
, of Bullet
Objects, created by myself this way:我将子弹作为一个名为Shooter[]
的Bullet
Objects 数组进行管理,由我自己以这种方式创建:
public class Bullet implements ActionListener{
Timer Time = new Timer(20, this);
private int BulletY = 430;
public int PlayerX;
public Rectangle Bound = new Rectangle();
public Bullet(int playerx) {
this.PlayerX = playerx;
Time.start();
}
public void draw(Graphics g){
g.setColor(Color.RED);
g.fillRect(PlayerX + 2, BulletY, 3, 10);
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
if (Time.isRunning()) {
BulletY = BulletY - 5;
Bound = new Rectangle (PlayerX + 2, BulletY, 3, 10);
}
}
}
I thought that calling the draw method in the GamePanel
's paint()
method would have allowed me to display both all the bullets shot and the player.我认为在GamePanel
的paint()
方法中调用 draw 方法可以让我同时显示所有射出的子弹和玩家。 What actually happens is that at the start it seems allright, but when I press S the player disappears and just one bullet is shot.实际发生的情况是,一开始看起来还不错,但是当我按下 S 时,玩家消失了,只射出一颗子弹。 Can you explain me why?你能解释一下为什么吗? This is how my paint()
method looks like:这就是我的paint()
方法的样子:
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 500, 500);
for(int i = 0; i < BulletsCounter; i++) {
Shooter[i].draw(g);
}
g.setColor(Color.RED);
g.fillRect(PlayerX, PlayerY, 20, 20);
//System.out.println("Here I should have painted the player...");
g.dispose();
}
BulletsCounter
is a counter I've created to avoid any NullPointerException
s in painting the whole array, it increases when S is pressed and so another bullet of the array is initialized and shot. BulletsCounter
是我创建的一个计数器,用于避免在绘制整个数组时出现任何NullPointerException
,当按下 S 时它会增加,因此数组的另一个子弹被初始化并射击。 Thank you for your patience, I'm new to the site, so warn me for any mistake.感谢您的耐心等待,我是该网站的新手,如有任何错误请警告我。
You've several significant problems, the biggest given first:您有几个重大问题,首先给出最大的问题:
Recommendations:建议:
super.paintComponent(g)
first thing in your override to allow for housekeeping painting并首先在您的覆盖中调用super.paintComponent(g)
以允许进行家务绘制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.