Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 与特定的if语句斗争_Java_If Statement_Jframe_Keylistener - Fatal编程技术网

Java 与特定的if语句斗争

Java 与特定的if语句斗争,java,if-statement,jframe,keylistener,Java,If Statement,Jframe,Keylistener,嗨,我正在开发一个程序,该程序将使用keylistener来移动图像,到目前为止,我拥有一切,但如果语句创建边界来阻止图像移动到某些我不希望的地方。我不知道如何为我的图像创建边界 到目前为止,我已经创建了阻止他们离开页面外部的 public void paintComponent(Graphics g) { int row = 0; int col = 0; int x, y; int colMax = 29;

嗨,我正在开发一个程序,该程序将使用keylistener来移动图像,到目前为止,我拥有一切,但如果语句创建边界来阻止图像移动到某些我不希望的地方。我不知道如何为我的图像创建边界

到目前为止,我已经创建了阻止他们离开页面外部的

    public void paintComponent(Graphics g)
    {
        int row = 0;
        int col = 0;
        int x, y;
        int colMax = 29;
        int rowMax = 41;

        super.paintComponent(g);
        ImageIcon ground = new ImageIcon("Ground.jpg");
        ImageIcon wall = new ImageIcon("WallTest0000.jpg");
        ImageIcon Hero = new ImageIcon("Herp0000.jpg");

        for(col = 0; col <= colMax; col++)
        {
            for(row = 0; row <= rowMax; row++)
            {
                x = row * 30;
                y = col * 30;
                wall.paintIcon(this, g, x, y);
            }
        }

        for(col = 1; col <= 6; col++)
        {
            for(row = 1; row <= 6; row++)
            {
                x = row * 30;
                y = col * 30;
                ground.paintIcon(this, g, x, y);
            }
        }
        for(row = 7; row <= 20; row++)
        {
            x = row * 30;
            y = 150;
            ground.paintIcon(this, g, x, y);
        }
        for(col = 4; col <= 8; col++)
        {
            for(row = 21;  row <= 33; row++)
            {
                x = row * 30;
                y = col * 30;
                ground.paintIcon(this, g, x, y);
            }
        }
        for(col =  6; col <= 14; col++)
        {
            x = 450;
            y = col * 30;
            ground.paintIcon(this, g, x, y);
        }
        for(col = 9; col <= 23; col++)
        {
            x = 810;
            y = col * 30;
            ground.paintIcon(this, g, x, y);
        }
        for(row = 6; row <= 26; row++)
        {
            x = row * 30;
            y = 450;
            ground.paintIcon(this, g, x, y);
        }
        for(col = 16; col <= 24; col++)
        {
            x = 180;
            y = col * 30;
            ground.paintIcon(this, g, x, y);
        }
        for(row = 7; row <= 21; row++)
        {
            x = row * 30;
            y = 720;
            ground.paintIcon(this, g, x, y);
        }
        for(row = 28; row <= 32; row++)
        {
            x = row * 30;
            y = 690;
            ground.paintIcon(this, g, x, y);
        }
        for(col = 23; col <= 28; col++)
        {
            for(row = 33; row <= 40; row++)
            {
                x = row * 30;
                y = col * 30;
                ground.paintIcon(this, g, x, y);
            }
        }
        Hero.paintIcon(this, g, X, Y)
公共组件(图形g)
{
int行=0;
int col=0;
int x,y;
int colMax=29;
int rowMax=41;
超级组件(g);
ImageIcon地面=新的ImageIcon(“ground.jpg”);
ImageIcon墙=新的ImageIcon(“WallTest0000.jpg”);
ImageIcon Hero=新的ImageIcon(“Herp0000.jpg”);

对于(col=0;col每次重新绘制GUI时,您的代码都会从文件中读取3个新的图像图标,这大大降低了您的绘制方法的速度,这是不必要的——为什么?为什么不简单地在图像中读取一次,比如在构造器中读取一次呢?另外,为什么不将它们作为BuffereImage(同样,仅读取一次)读取并绘制图像,而不是使用从未用作图标的图像图标?至于您要问的主要问题,您可能希望对此进行澄清,因为我没有看到太多关于边界需要使用什么的代码或信息。您还发布了许多与您的问题无关的代码。另外,请使用键绑定你的问题是测试对象的交集,即精灵的逻辑表示与边框/墙/门/等的逻辑表示。你需要将代码的GUI部分与逻辑部分分开,然后尝试处理isolat中的逻辑部分ion。我的问题是,我目前有一个空盒子可以移动,我不能走出盒子,但我可以进入盒子内我想去的任何地方。我想限制我在房间和连接房间的走廊内移动盒子的能力。我发布的包含ground.paintIcon的图形代码是我想作为可导航点。例如:当它有一个for循环用于从1到6的列,另一个for循环用于1到6的行,创建了一个180x180像素的框,它连接到一个180x150像素的30像素走廊,我希望能够这样做,以便有人可以“其他人”是你的朋友。特别是如果(xkapow){不同的叫声}。
     public void actionPerformed(ActionEvent e)

    {

        if(X < 30)
        {
            velX = 0;
            X = 30;
        }
        if(X > 1200)
        {
            velX = 0;
            X = 1200;
        }

        if(Y < 30)
        {
            velY = 0;
            Y = 30;
        }
        if(Y > 840)
        {
            velY = 0;
            Y = 840;
        }
        X = X + velX;
        Y = Y +velY;
        repaint();
    }