挣扎于特定的if语句

时间:2014-05-08 16:20:03

标签: java if-statement jframe keylistener

您好我正在开发一个程序,它将使用一个keylistener来移动图像,到目前为止,除了if语句创建边界以阻止图像进入某些我不想要的地方之外,我还有其他所有内容。我不知道如何为我的图像创建边界。

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

    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)

这是我目前的图形方法,它将我30x30的图像描绘成30个,为某人创建一个小迷宫进行导航。对不起,如果您想要参考,我没有图片。

我的动作改变方法:

     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();
    }

我的JFrame设置为1290,900并且围绕整个事物有30x30的墙壁然后我将它发送回去的数量少于我可以移动的实际数量30但是现在我不知所措关于如何制作盒子和走廊的陈述,所以非常感谢任何帮助!

0 个答案:

没有答案