単純なプラットフォームゲームを作成しようとしていますが、私はJPanelにそのキャラクターを配置しようとしています。そして私は問題になった。すでに配置されているタイル(草、空、その他)を動かすことなく、JPanelに文字を追加することはできません(文字は画像の入ったJLabelの形になっています)。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は、すべてのタイルIDを含むされます。それは、ほとんどが0の672の整数を含んでいます。
他のタイルを動かさずに特定の座標で文字を追加する方法を教えてもらえますか?
私は現在でそれをされて追加していどのよう:
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();
}
そして、私はそれを実行すると、それはこれで出力します(私の悪い芸術すみません)出力
画像:
ご不明な点がありましたら、お知らせください。