0

そこで、簡単なパックマンのクローンを作成しようとして、画像などを追加する段階になりました。これまでのところ、trackball.pngとborder.pngの
trackball.png2つの画像があります。 だから私は以下のコードを実行します、そしてこれは私が得るものです: そしてコンソール出力: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 rowsします。20 columnsfor200

画面全体をカバーするには、それに応じて調整する必要がありますarena[][]

PS: 最大と最大の場所(225,475)が原因です。(i*25+","+j*25)i=9j=19

于 2013-03-07T05:13:30.330 に答える