Reputation: 99
I'm a new programmer and I have just started recently working with applets. However, I am still confused on where to put the code I would normally put in the main method of the application. Currently, I am writing a program that would deal out 10 random cards from the normal deck.
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
}
}
Somewhere in there, I want to add the code that will place random cards into an array and allow the paint method to do its job. The code I have for that is this.
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;
}
So where do I put the code I would normally put in the main method of the application? I suppose I should put it in a method but insure what I would call it. Any insight is appreciated.
Upvotes: 3
Views: 305
Reputation: 159874
Your card setup code belongs in the applet init()
method as you are only initializing this once. You can extract it out into a helper method.
Upvotes: 1
Reputation: 83587
You should override the start()
method and place your code there.
p.s. I strongly suggest that you learn about arrays. When you have many variables whose name only differs by a number suffix, you will find that arrays are most likely more suited to the solve problem. In this particular case, you can very likely reduce the 52 lines in your init()
method to less than 10.
Upvotes: 1