-1

52 枚のカードをロードし、(乱数ジェネレーターを使用して) シャッフルし、デッキの上位 10 枚のカードを 5 枚ずつ 2 行で表示するアプレットを作成しようとしています。これが私のコードですが、画像を印刷していないようです。

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

乱数は [0-1) の間で生成され、0 に切り捨てられる整数にキャストされるため、これは常に -1 です。その後、1を引きます。これは無効なインデックスであり、例外が発生します。

このランダム化コードを定数 0 に変更して、ロードが適切に行われたかどうかを確認します。

それで問題ない場合は、最初にカードをアレイにロードする必要があります。そうすれば、それらを適切にシャッフルできます。また、後で便利になるように、カードをいくつかのクラスにまとめてみてください。

于 2013-01-02T23:27:12.427 に答える