更轻松地将切片添加到JFrame

时间:2015-03-26 14:50:32

标签: java jframe

我正在为我的地形生成器的JFrame添加一些图块,它看起来像这样:

        for (double[] row : Terrain.tileData) {
        for (double d : row) {
            if (d >= 10000D) {
                add(new Tile(0, tileSize));
            } else if (d >= 9500D) {
                add(new Tile(1, tileSize));
            } else if (d >= 9000D) {
                add(new Tile(2, tileSize));
            } else if (d >= 8500D) {
                add(new Tile(3, tileSize));
            } else if (d >= 8000D) {
                add(new Tile(4, tileSize));
            } else if (d >= 7500D) {
                add(new Tile(5, tileSize));
            } else if (d >= 7000D) {
                add(new Tile(6, tileSize));
            } else if (d >= 6500D) {
                add(new Tile(7, tileSize));
            } else if (d >= 6000D) {
                add(new Tile(8, tileSize));
            } else if (d >= 5500D) {
                add(new Tile(9, tileSize));
            } else if (d >= 5000D) {
                add(new Tile(10, tileSize));
            } else if (d >= 4500D) {
                add(new Tile(11, tileSize));
            } else if (d >= 4000D) {
                add(new Tile(12, tileSize));
            } else if (d >= 3500D) {
                add(new Tile(13, tileSize));
            } else if (d >= 3000D) {
                add(new Tile(14, tileSize));
            } else if (d >= 2500D) {
                add(new Tile(15, tileSize));
            } else if (d >= 2000D) {
                add(new Tile(16, tileSize));
            } else if (d >= 1500D) {
                add(new Tile(17, tileSize));
            } else if (d >= 1000D) {
                add(new Tile(18, tileSize));
            } else if (d >= 500D) {
                add(new Tile(19, tileSize));
            } else {
                add(new Tile(20, tileSize));
            }
        }
    }

我的tile类看起来像这样:

    public Tile(int color, int tileSize) {
    setVisible(true);
    setOpaque(true);
    setPreferredSize(new Dimension(tileSize, tileSize));
    if (color == 0) {
        setBackground(new Color(255, 255, 255));
    } else if (color == 1) {
        setBackground(new Color(138, 132, 125));
    } else if (color == 2) {
        setBackground(new Color(105, 100, 94));
    } else if (color == 3) {
        setBackground(new Color(77, 71, 64));
    } else if (color == 4) {
        setBackground(new Color(36, 32, 27));
    } else if (color == 5) {
        setBackground(new Color(69, 53, 42));
    } else if (color == 6) {
        setBackground(new Color(82, 48, 4));
    } else if (color == 7) {
        setBackground(new Color(17, 82, 4));
    } else if (color == 8) {
        setBackground(new Color(24, 115, 6));
    } else if (color == 9) {
        setBackground(new Color(56, 143, 39));
    } else if (color == 10) {
        setBackground(new Color(51, 156, 30));
    } else if (color == 11) {
        setBackground(new Color(29, 156, 3));
    } else if (color == 12) {
        setBackground(new Color(37, 201, 4));
    } else if (color == 13) {
        setBackground(new Color(67, 237, 33));
    } else if (color == 14) {
        setBackground(new Color(87, 85, 0));
    } else if (color == 15) {
        setBackground(new Color(176, 141, 2));
    } else if (color == 16) {
        setBackground(new Color(232, 195, 39));
    } else if (color == 17) {
        setBackground(new Color(224, 200, 101));
    } else if (color == 18) {
        setBackground(new Color(79, 67, 222));
    } else if (color == 19) {
        setBackground(new Color(36, 18, 227));
    } else if (color == 20) {
        setBackground(new Color(15, 0, 179));
    }
}

有更简单的方法吗?另外,有没有办法让pack()更快?

1 个答案:

答案 0 :(得分:1)

对于tilegeneration:

for(double d : row){
    înt v = 20 - d / 500;
    if(v < 0)
        v = 0;

    add(new Tile(v , tileSize));
}

将颜色存储在数组中并按如下方式读取:

Color[] colors = new Color[]{}
setBackgroundColor(colors[color]);