【发布时间】:2017-05-21 04:52:51
【问题描述】:
我正在尝试创建一个简单的平台游戏,并且我正在尝试将角色放到 JPanel 上。我遇到了一个问题。如果不移动已放置的图块(草、天空等),我无法将角色添加到 JPanel(请注意,角色是包含图像图标的 JLabel 形式)。
我用来放置积木的代码是:
static void drawScreen() throws IOException {
panel.removeAll();
int tile = 0;
int line = 0;
for (int i = 0; i < t.length; i++, tile++) {
boolean tD = tile % 32 == 0;
if (tD) {
tile = 0;
line++;
}
if (t[i] == 0) {
File f = new File(sPath);
BufferedImage s = ImageIO.read(f);
JLabel l = new JLabel(new ImageIcon(s));
c.gridx = tile;
c.gridy = line;
c.insets = new Insets(0, 0, 0, 0);
panel.add(l, c);
}
if (t[i] == 1) {
File f = new File(gPath);
BufferedImage g = ImageIO.read(f);
JLabel l = new JLabel(new ImageIcon(g));
c.gridx = tile;
c.gridy = line;
c.insets = new Insets(0, 0, 0, 0);
panel.add(l, c);
}
if (t[i] == 2) {
File f = new File(dPath);
BufferedImage d = ImageIO.read(f);
JLabel l = new JLabel(new ImageIcon(d));
c.gridx = tile;
c.gridy = line;
c.insets = new Insets(0, 0, 0, 0);
panel.add(l, c);
}
}
frame.revalidate();
frame.repaint();
}
数组 t 包含所有的 tile id。它包含 672 个整数,大部分为 0。
谁能告诉我应该如何在不移动其他图块的情况下将角色添加到特定坐标。
我目前的添加方式是:
static void addChar() throws IOException {
File f = new File(cPath);
BufferedImage c1 = ImageIO.read(f);
BufferedImage c = runResize(c1, 50, 76);
JLabel l = new JLabel(new ImageIcon(c));
l.setOpaque(false);
panel.add(l);
frame.revalidate();
frame.repaint();
}
当我运行它时,它会输出:(请原谅我的糟糕艺术)
输出图像:
如果您有任何问题,请告诉我。
【问题讨论】:
标签: java