0

所以我试图制作一个简单的 PacMan 克隆,我已经到了添加图像等的阶段。我有两个图像(到目前为止),它们是:
轨迹球.png边框.png,trackball.png 和border.png。
所以我运行下面的代码new oicMan();,这就是我得到
我得到了什么
的: 控制台输出:

.
.
.
Placing image border on coordinate (225,250)
Placing image border on coordinate (225,275)
Placing image border on coordinate (225,300)
Placing image border on coordinate (225,325)
Placing image border on coordinate (225,350)
Placing image border on coordinate (225,375)
Placing image border on coordinate (225,400)
Placing image border on coordinate (225,425)
Placing image border on coordinate (225,450)
Placing image border on coordinate (225,475)

所以它似乎停在 x=225。谁能告诉我为什么?for我设置图像的循环有问题吗?谢谢(主要是,请告诉我为什么它没有完全绘画。)

import javax.swing.*;
import java.awt.*;

public class oicMan extends JFrame
{
Container container;
/*
####################
# ##               #
# ## ############# #
# ## ############# #
# ## #             #
# ## # ##### ##### #
# ##   ##### ##### #
# ## # ##### ##### #
#    #             #
####################
 */
    String arena[][] =
    {
    {"#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#"},
    {"#"," ","#","#"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","#"},
    {"#"," ","#","#"," ","#","#","#","#","#","#","#","#","#","#","#","#","#"," ","#"},
    {"#"," ","#","#"," ","#","#","#","#","#","#","#","#","#","#","#","#","#"," ","#"},
    {"#"," ","#","#"," ","#"," "," "," "," "," "," "," "," "," "," "," "," "," ","#"},
    {"#"," ","#","#"," ","#"," ","#","#","#","#","#"," ","#","#","#","#","#"," ","#"},
    {"#"," ","#","#"," "," "," ","#","#","#","#","#"," ","#","#","#","#","#"," ","#"},
    {"#"," ","#","#"," ","#"," ","#","#","#","#","#"," ","#","#","#","#","#"," ","#"},
    {"#"," "," "," "," ","#"," "," "," "," "," "," "," "," "," "," "," "," "," ","#"},
    {"#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#"},
    };

    public oicMan()
    {
        super("oicMan");
        setSize(500, 250);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        container = getContentPane();
        container.setLayout(null);
        container.setBackground(Color.black);

        for(int i = 0; i < arena.length; i++)
        {
            for(int j = 0; j < arena[0].length; j++)
            {
                JLabel label = null;
                if(arena[ i][ j].equals("#"))
                {
                    label = new JLabel(new ImageIcon("border.png"));
                    label.setName("border");
                }
                else
                {
                    label = new JLabel(new ImageIcon("trackball.png"));
                    label.setName("track");
                }

                container.add(label);
                label.setBounds(i*25,j*25,25,25);
                System.out.println("Placing image "+label.getName()+" on coordinate ("+i*25+","+j*25+")");
            }
        }
        repaint();
        container.validate();
        setContentPane(container);
    }
}
4

2 回答 2

0

当您调用 label.setBounds(i*25,j*25,25,25); 时,似乎您可能已经颠倒了行和列 我认为将那行代码更改为 label.setBounds(j*25,i*25,25,25); 应该解决问题。JLabel setBounds 方法的参数如下: JLabel: setBounds(int x, int y, int width, int height)

正如这篇 java2s 文章所见

这意味着当您认为您正在设置 JLabel 的 y 值时,您实际上是在设置 x 值,反之亦然。

一个很好的建议是始终阅读您可能正在使用的任何库的文档。

不过,我总是很乐意提供帮助。

干杯,希望这有助于并解决您的问题!

于 2013-03-07T05:13:19.113 回答
0

问题出在你的arena[][]. 它具有10 rowsand 20 columns,这实际上意味着您的for循环将仅针对200块运行。

要覆盖整个屏幕,您需要相应地调整您的屏幕arena[][]

PS: (225,475)是因为(i*25+","+j*25)最大i=9j=19最大的位置。

于 2013-03-07T05:13:30.330 回答