-1

我正在尝试创建一个加载 52 张牌的小程序,将它们洗牌(使用随机数生成器),然后在两排五张中显示牌组的前 10 张牌。这是我的代码,但它似乎没有打印任何图像。

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;

public class cards extends Applet {
Image c1;
Image c2;
Image c3;
Image c4;
Image c5;
Image c6;
Image c7;
Image c8;
Image c9;
Image c10;
Image cj;
Image cq;
Image ck;
Image h1;
Image h2;
Image h3;
Image h4;
Image h5;
Image h6;
Image h7;
Image h8;
Image h9;
Image h10;
Image hj;
Image hq;
Image hk;
Image d1;
Image d2;
Image d3;
Image d4;
Image d5;
Image d6;
Image d7;
Image d8;
Image d9;
Image d10;
Image dj;
Image dq;
Image dk;
Image s1;
Image s2;
Image s3;
Image s4;
Image s5;
Image s6;
Image s7;
Image s8;
Image s9;
Image s10;
Image sj;
Image sq;
Image sk;
    public void init(){
    this.setBackground( Color.blue );
    c1 = getImage ( getDocumentBase (), "cards/c1.gif" );
        c2 = getImage ( getDocumentBase (), "cards/c2.gif" );
        c3 = getImage ( getDocumentBase (), "cards/c3.gif" );
        c4 = getImage ( getDocumentBase (), "cards/c4.gif" );
        c5 = getImage ( getDocumentBase (), "cards/c5.gif" );
        c6 = getImage ( getDocumentBase (), "cards/c6.gif" );
        c7 = getImage ( getDocumentBase (), "cards/c7.gif" );
        c8 = getImage ( getDocumentBase (), "cards/c8.gif" );
        c9 = getImage ( getDocumentBase (), "cards/c9.gif" );
        c10 = getImage ( getDocumentBase (), "cards/c10.gif" );
        cj = getImage ( getDocumentBase (), "cards/cj.gif" );
        cq = getImage ( getDocumentBase (), "cards/cq.gif" );
        ck = getImage ( getDocumentBase (), "cards/ck.gif" );
        h1 = getImage ( getDocumentBase (), "cards/h1.gif" );
        h2 = getImage ( getDocumentBase (), "cards/h2.gif" );
        h3 = getImage ( getDocumentBase (), "cards/h3.gif" );
        h4 = getImage ( getDocumentBase (), "cards/h4.gif" );
        h5 = getImage ( getDocumentBase (), "cards/h5.gif" );
        h6 = getImage ( getDocumentBase (), "cards/h6.gif" );
        h7 = getImage ( getDocumentBase (), "cards/h7.gif" );
        h8 = getImage ( getDocumentBase (), "cards/h8.gif" );
        h9 = getImage ( getDocumentBase (), "cards/h9.gif" );
        h10 = getImage ( getDocumentBase (), "cards/h10.gif" );
        hj = getImage ( getDocumentBase (), "cards/hj.gif" );
        hq = getImage ( getDocumentBase (), "cards/hq.gif" );
        hk = getImage ( getDocumentBase (), "cards/hk.gif" );
        d1 = getImage ( getDocumentBase (), "cards/d1.gif" );
        d2 = getImage ( getDocumentBase (), "cards/d2.gif" );
        d3 = getImage ( getDocumentBase (), "cards/d3.gif" );
        d4 = getImage ( getDocumentBase (), "cards/d4.gif" );
        d5 = getImage ( getDocumentBase (), "cards/d5.gif" );
        d6 = getImage ( getDocumentBase (), "cards/d6.gif" );
        d7 = getImage ( getDocumentBase (), "cards/d7.gif" );
        d8 = getImage ( getDocumentBase (), "cards/d8.gif" );
        d9 = getImage ( getDocumentBase (), "cards/d9.gif" );
        d10 = getImage ( getDocumentBase (), "cards/d10.gif" );
        dj = getImage ( getDocumentBase (), "cards/dj.gif" );
        dq = getImage ( getDocumentBase (), "cards/dq.gif" );
        dk = getImage ( getDocumentBase (), "cards/dk.gif" );
        s1 = getImage ( getDocumentBase (), "cards/s1.gif" );
        s2 = getImage ( getDocumentBase (), "cards/s2.gif" );
        s3 = getImage ( getDocumentBase (), "cards/s3.gif" );
        s4 = getImage ( getDocumentBase (), "cards/s4.gif" );
        s5 = getImage ( getDocumentBase (), "cards/s5.gif" );
        s6 = getImage ( getDocumentBase (), "cards/s6.gif" );
        s7 = getImage ( getDocumentBase (), "cards/s7.gif" );
        s8 = getImage ( getDocumentBase (), "cards/s8.gif" );
        s9 = getImage ( getDocumentBase (), "cards/s9.gif" );
        s10 = getImage ( getDocumentBase (), "cards/s10.gif" );
        sj = getImage ( getDocumentBase (), "cards/sj.gif" );
        sq = getImage ( getDocumentBase (), "cards/sq.gif" );
        sk = getImage ( getDocumentBase (), "cards/sk.gif" );

    }
    public void paint( Graphics screen ){
    int x = 10;
    int y = 10;
    int x2 = 10;
    int y2 = 110;

        Image [] cards = new Image [ 52 ];
        cards [ 0 ] = c1;
        cards [ 1 ] = c2;
        cards [ 2 ] = c3;
        cards [ 3 ] = c4;
        cards [ 4 ] = c5;
        cards [ 5 ] = c6;
        cards [ 6 ] = c7;
        cards [ 7 ] = c8;
        cards [ 8 ] = c9;
        cards [ 9 ] = c10;
        cards [ 10 ] = cj;
        cards [ 11 ] = cq;
        cards [ 12 ] = ck;
        cards [ 13 ] = h1;
        cards [ 14 ] = h2;
        cards [ 15 ] = h3;
        cards [ 16 ] = h4;
        cards [ 17 ] = h5;
        cards [ 18 ] = h6;
        cards [ 19 ] = h7;
        cards [ 20 ] = h8;
        cards [ 21 ] = h9;
        cards [ 22 ] = h10;
        cards [ 23 ] = hj;
        cards [ 24 ] = hq;
        cards [ 25 ] = hk;
        cards [ 26 ] = d1;
        cards [ 27 ] = d2;
        cards [ 28 ] = d3;
        cards [ 29 ] = d4;
        cards [ 30 ] = d5;
        cards [ 31 ] = d6;
        cards [ 32 ] = d7;
        cards [ 33 ] = d8;
        cards [ 34 ] = d9;
        cards [ 35 ] = d10;
        cards [ 36 ] = dj;
        cards [ 37 ] = dq;
        cards [ 38 ] = dk;
        cards [ 39 ] = s1;
        cards [ 40 ] = s2;
        cards [ 41 ] = s3;
        cards [ 42 ] = s4;
        cards [ 43 ] = s5;
        cards [ 44 ] = s6;
        cards [ 45 ] = s7;
        cards [ 46 ] = s8;
        cards [ 47 ] = s9;
        cards [ 48 ] = s10;
        cards [ 49 ] = sj;
        cards [ 50 ] = sq;
        cards [ 51 ] = sk;
    for (int i = 0; i < 4; i++){
        screen.drawImage (cards[( int ) ( float ) Math.random () * 52 - 1], x, y, this);
        x = x + 5;
    }
    for (int i = 0; i < 4; i++){
        screen.drawImage (cards [( int ) ( float ) ( Math.random () ) * 52 - ( int ) ( 1 )], x2, y2, this);
        x = x + 10;
    }
    }





}
4

1 回答 1

2

即使加载了图像,您的小程序也会在第一次绘制事件时终止,因为随机化总是生成 -1 的卡片索引:

( int ) ( float ) Math.random () * 52 - 1

这始终是 -1,因为随机数是在 [0-1) 之间生成的,并且它被转换为整数,这会将它向下舍入为零。在那之后,你减去一个。这是一个无效的索引,你会得到一个异常。

将此随机化代码更改为常数 0 以检查加载是否正确完成。

如果没问题,您应该第一次真正将卡加载到数组中。这样你就可以体面地洗牌。另外,尝试将卡片包装到某个类,因为稍后它会派上用场。

于 2013-01-02T23:27:12.427 回答