我正在编写塔防游戏,我需要将加载了缓冲图像的JPanel列表放到JFrame上。我使用for循环将JPanel放到JFrame上,但是,当我运行程序时,只显示列表中的最后一个JPanel。我已经检查过以确保图像没有被放置在另一个上,并且它们被放置在三个不同的位置。
这是我的类的代码,它创建了一个JFrame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class GameScreen extends JPanel
implements MouseListener {
private ArrayList< JPanel > images;
private TDGame currentGame;
private JFrame frame;
private static final int width = 700;
private static final int length = 700;
public void mouseExited(MouseEvent e) {
this.repaint();
}
public void mouseEntered(MouseEvent e) {
this.repaint();
}
public void mouseReleased(MouseEvent e) {
this.repaint();
}
public void mousePressed(MouseEvent e) {
this.repaint();
}
public void mouseClicked(MouseEvent e) {
this.repaint();
}
public GameScreen() {
super();
images = new ArrayList<JPanel>();
currentGame = new TDGame();
frame = new JFrame("Tower Defense");
frame.setSize(width, length);
frame.setVisible(true);
frame.pack();
frame.getContentPane().add(this,BorderLayout.CENTER);
currentGame.EnemyForce.add(currentGame.EnemyForce.size(),
new BasicEnemy(0, 0, LoadImage()));
currentGame.EnemyForce.add(currentGame.EnemyForce.size(),
new BasicEnemy(250, 250, LoadImage2()));
currentGame.EnemyForce.add(currentGame.EnemyForce.size(),
new BasicEnemy(0, 0, LoadImage1()));
this.repaint();
}
public BufferedImage LoadImage() {
BufferedImage img = null;
try {
img = ImageIO.read(new File("Saturn-Intruder.png"));
} catch (IOException e) {
}
return img;
}
public BufferedImage LoadImage1() {
BufferedImage img = null;
try {
img = ImageIO.read(new File("Level1-TD.jpeg"));
} catch (IOException e) {
}
return img;
}
public BufferedImage LoadImage2() {
BufferedImage img = null;
try {
img = ImageIO.read(new File("BlackBird-Intruder.jpeg"));
} catch (IOException e) {
}
return img;
}
@Override
protected void paintComponent(Graphics g) {
for(int i = 0; i < currentGame.Defense.size(); i++) {
frame.add(currentGame.Defense.get(i));
}
for(int i = 0; i < currentGame.EnemyForce.size(); i++) {
frame.add( currentGame.EnemyForce.get(i));
}
for(int i = 0; i < currentGame.ShotsFired.size(); i++) {
frame.add(currentGame.ShotsFired.get(i));
}
}
}
以下是放置在JFrame上的JPanel的代码:
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
public class BasicEnemy extends Entity {
JPanel ePanel;
Graphics g;
public BasicEnemy(double x, double y, BufferedImage image) {
super(x, y, image);
ePanel = new JPanel();
ePanel.setVisible(true);
}
protected void paintComponent(Graphics g) {
g.drawImage(getImage(), (int)(getXCoord()),
(int)(getYCoord()), null);
}
}
我如何解决这个问题,以便放置所有图像,而不仅仅是一个?
答案 0 :(得分:1)
我不知道为什么你的游戏只显示一张图片。但随着这款游戏的进一步发展,我强烈建议您不要在自己的JPanel中添加每个图像。
我会对您的源代码做些什么来使其发挥作用。
将GameScreen.paintComponent中的所有for循环更改为
for(...){currentGame.EnemyForce.get(i).paintComponent(g); }
在frame.add(this);
之前添加frame.setVisible(true);
如果你要使用Swing使用图形,你永远不应该在JCompondents中保存作为游戏一部分的单个元素。您应该使用GameScreen作为“画布”,并在paintComponent中使用Graphics对象绘制所有游戏内容。