【问题标题】:How can I make my code more efficient by using an array如何通过使用数组使我的代码更高效
【发布时间】:2014-06-09 22:59:43
【问题描述】:

我是一名初级程序员,我想让我的代码更高效。我将如何从 PIECE1 - PIECE 8 最终整数中创建一个数组,以及从 image1 - image8 中创建一个数组。此外,我如何使用所述数组调用 g.drawImage 一次而不是 8 次。

public class GraphicalBoard extends Board {
    final int PIECE1 = 1;
    final int PIECE2 = 2;
    final int PIECE3 = 3;
    final int PIECE4 = 4;
    final int PIECE5 = 5;
    final int PIECE6 = 6;
    final int PIECE7 = 7;
    final int PIECE8 = 8;
    final int NO_OF_PIECES = 8;
    Image image1, image2, image3, image4, image5, image6, image7, image8;

    final int SQUARE_SIZE = 200; // The length (in pixels) of the side of each

    public GraphicalBoard() {
            super();
            loadResources();
    }

    private void loadResources() {
            image1 = new ImageIcon("picOne.gif").getImage();
            image2 = new ImageIcon("picTwo.gif").getImage();
            image3 = new ImageIcon("picThree.gif").getImage();
            image4 = new ImageIcon("picFour.gif").getImage();
            image5 = new ImageIcon("picFive.gif").getImage();
            image6 = new ImageIcon("picSix.gif").getImage();
            image7 = new ImageIcon("picSeven.gif").getImage();
            image8 = new ImageIcon("picEight.gif").getImage();
    }

    public void draw(Graphics g) {

            // Draw the board with current pieces.
            for (int row = 0; row < board.length; row++)
                    for (int column = 0; column < board[row].length; column++) {
                            // Find the x and y positions for each row and column.
                            int xPos = column * SQUARE_SIZE;
                            int yPos = row * SQUARE_SIZE;
                            if (board[row][column] == PIECE1)
                                    g.drawImage(image1, xPos, yPos, null);
                            else if (board[row][column] == PIECE2)
                                    g.drawImage(image2, xPos, yPos, null);
                            else if (board[row][column] == PIECE3)
                                    g.drawImage(image3, xPos, yPos, null);
                            else if (board[row][column] == PIECE4)
                                    g.drawImage(image4, xPos, yPos, null);
                            else if (board[row][column] == PIECE5)
                                    g.drawImage(image5, xPos, yPos, null);
                            else if (board[row][column] == PIECE6)
                                    g.drawImage(image6, xPos, yPos, null);
                            else if (board[row][column] == PIECE7)
                                    g.drawImage(image7, xPos, yPos, null);
                            else if (board[row][column] == PIECE8)
                                    g.drawImage(image8, xPos, yPos, null);

                    }
    }

}

【问题讨论】:

    标签: arrays image graphics


    【解决方案1】:

    使用数组可能不会使您的代码更高效,但会更小,并且希望可读性更好。 关于PIECES - 你可以像这样初始化一个数组:

    final int PIECES[] = new int[] {1,2,3,4,5,6,7,8};
    

    我还会将图像文件命名为不同的名称。而不是picOne.gif 使用pic1.gif 等。然后你可以这样做:

    Image[] images = new Image[NO_OF_PIECES];
    for(int i = 0; i < NO_OF_PIECES; i++)
       images[i] = new ImageIcon("pic"+(i+1)+".gif").getImage();
    

    board 变量是什么? 请注意,获取 n 维数组的长度可能有点棘手。 See here

    在您的 draw 方法中,我会使用 switch-case 而不是 else-if 以获得更好的可读性。 还有计算:

    int yPos = row * SQUARE_SIZE;

    可以在第一个循环体中移动,但我想编译器足够聪明,可以解决这个问题。

    【讨论】:

    • 我的电路板是一个 3x3 阵列。是否有可能只调用一次而不是 8 次 draw 方法中的数组。当我尝试在 draw 方法中调用 images[i] 数组时,我也会收到错误消息。这是我尝试过的:
    • 如果你想要第一张图片使用 images[0] 等。如果信息只是当前列和行,为什么还需要板子?
    • board 类具有特定行/列中实际数字的信息。这是一个幻灯片益智游戏,因此板上的数据会发生变化。这是我尝试过的,但我似乎遇到了一个错误,因为我找不到 PIECES 的索引,而且我似乎无法调用 images[]:int unknownPiece = board[row][column] ;int indexOfPiece = PIECES.indexOf(unknownPiece);g.drawImage(images[indexOfPiece], xPos, yPos, null);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2013-06-20
    • 2019-07-28
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多