主要方法在applet中替换

时间:2012-08-29 22:17:19

标签: java applet main

我是一名新程序员,我刚刚开始使用applet。但是,我仍然对将通常放在应用程序主方法中的代码放在何处感到困惑。目前,我正在编写一个程序,可以从正常套牌中处理10张随机卡片。

import java.awt.Graphics;
import java.awt.Image;
import java.applet.Applet;
import java.lang.Math;

public class unit12Assignment extends Applet
{
Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13;
Image card14, card15, card16, card17, card18, card19, card20, card21, card22, card23, card24, card25, card26;
Image card27, card28, card29, card30, card31, card32, card33, card34, card35, card36, card37, card38, card39;
Image card40, card41, card42, card43, card44, card45, card46, card47, card48, card49, card50, card51, card52;

public void init()
{
    card1 = getImage( getDocumentBase(), "c1.gif" );
    card2 = getImage( getDocumentBase(), "c2.gif" );
    card3 = getImage( getDocumentBase(), "c3.gif" );
    card4 = getImage( getDocumentBase(), "c4.gif" );
    card5 = getImage( getDocumentBase(), "c5.gif" );
    card6 = getImage( getDocumentBase(), "c6.gif" );
    card7 = getImage( getDocumentBase(), "c7.gif" );
    card8 = getImage( getDocumentBase(), "c8.gif" );
    card9 = getImage( getDocumentBase(), "c9.gif" );
    card10 = getImage( getDocumentBase(), "c10.gif" );
    card11 = getImage( getDocumentBase(), "cj.gif" );
    card12 = getImage( getDocumentBase(), "cq.gif" );
    card13 = getImage( getDocumentBase(), "ck.gif" );
    card14 = getImage( getDocumentBase(), "d1.gif" );
    card15 = getImage( getDocumentBase(), "d2.gif" );
    card16 = getImage( getDocumentBase(), "d3.gif" );
    card17 = getImage( getDocumentBase(), "d4.gif" );
    card18 = getImage( getDocumentBase(), "d5.gif" );
    card19 = getImage( getDocumentBase(), "d6.gif" );
    card20 = getImage( getDocumentBase(), "d7.gif" );
    card21 = getImage( getDocumentBase(), "d8.gif" );
    card22 = getImage( getDocumentBase(), "d9.gif" );
    card23 = getImage( getDocumentBase(), "d10.gif" );
    card24 = getImage( getDocumentBase(), "dj.gif" );
    card25 = getImage( getDocumentBase(), "dq.gif" );
    card26 = getImage( getDocumentBase(), "dk.gif" );
    card27 = getImage( getDocumentBase(), "h1.gif" );
    card28 = getImage( getDocumentBase(), "h2.gif" );
    card29 = getImage( getDocumentBase(), "h3.gif" );
    card30 = getImage( getDocumentBase(), "h4.gif" );
    card31 = getImage( getDocumentBase(), "h5.gif" );
    card32 = getImage( getDocumentBase(), "h6.gif" );
    card33 = getImage( getDocumentBase(), "h7.gif" );
    card34 = getImage( getDocumentBase(), "h8.gif" );
    card35 = getImage( getDocumentBase(), "h9.gif" );
    card36 = getImage( getDocumentBase(), "h10.gif" );
    card37 = getImage( getDocumentBase(), "hj.gif" );
    card38 = getImage( getDocumentBase(), "hq.gif" );
    card39 = getImage( getDocumentBase(), "hk.gif" );
    card40 = getImage( getDocumentBase(), "s1.gif" );
    card41 = getImage( getDocumentBase(), "s2.gif" );
    card42 = getImage( getDocumentBase(), "s3.gif" );
    card43 = getImage( getDocumentBase(), "s4.gif" );
    card44 = getImage( getDocumentBase(), "s5.gif" );
    card45 = getImage( getDocumentBase(), "s6.gif" );
    card46 = getImage( getDocumentBase(), "s7.gif" );
    card47 = getImage( getDocumentBase(), "s8.gif" );
    card48 = getImage( getDocumentBase(), "s9.gif" );
    card49 = getImage( getDocumentBase(), "s10.gif" );
    card50 = getImage( getDocumentBase(), "sj.gif" );
    card51 = getImage( getDocumentBase(), "sq.gif" );
    card52 = getImage( getDocumentBase(), "sk.gif" );


}
public void paint(Graphics g)
{
    String card[] = new String[10];

    getCards( card[] );

    g.drawImage( cardSelection[0], 10, 10, this);
    g.drawImage( cardSelection[1], 90, 10, this);
    g.drawImage( cardSelection[2], 170, 10, this);
    g.drawImage( cardSelection[3], 250, 10, this);
            //unfinished
}

}

在那里的某个地方,我想添加将随机卡放入数组并允许paint方法完成其工作的代码。我的代码就是这个。

String cardNumber; 
    double cardRandom;
    int cardRandomNumber;

    for ( int number = 0; number <  10; number++ );
    {
        cardRandom = Math.random() * Math.random() * 10;

        if ( cardRandom <= 52 );
        {
            cardRandomNumber = (int) Math.round( cardRandom );
        }
        cardNumber =  "card" + cardRandomNumber;
        cardSelection[number] = cardNumber;
    }

那么我将把通常放在应用程序主方法中的代码放在哪里?我想我应该把它放在一个方法中,但确保我称之为它。任何见解都表示赞赏。

2 个答案:

答案 0 :(得分:1)

您应该覆盖start()方法并将代码放在那里。

P.S。我强烈建议您了解数组。当您有许多名称仅以数字后缀不同的变量时,您会发现数组很可能更适合解决问题。在这种特殊情况下,您很可能会将init()方法中的52行减少到少于10行。

答案 1 :(得分:1)

您的卡片设置代码属于小程序init()方法,因为您只是初始化一次。您可以将其解压缩为辅助方法。