Back to Applets page
Click in the boxes below, select all and copy to grab the applet
HTML listing:
<HTML> <HEAD> <TITLE>Listing 10.4</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <APPLET CODEBASE="classes" CODE="Applet4.class" WIDTH=600 HEIGHT=100> </APPLET> </BODY> </HTML>
Applet listing:
import java.awt.*; import java.applet.*; import java.awt.image.*; public class Applet4 extends Applet { int cardWidth=43,cardHeight=61,imgCt=5; Image thisCard[] = new Image[imgCt]; public void init() { int randomCard,cardPos,i; Image playingCards; ImageFilter cardFilter; ImageProducer cardProducer; boolean usedCards[] = new boolean[52]; setBackground(Color.white); playingCards = getImage(getCodeBase(),"cards.gif"); for (i=0;i<imgCt;i++) { randomCard = (int)(java.lang.Math.random() * 52); if (usedCards[randomCard]) { i--; } else { cardPos = (randomCard*cardWidth)+1; cardFilter = new CropImageFilter(cardPos,1,cardWidth,cardHeight); cardProducer = new FilteredImageSource(playingCards.getSource(),cardFilter); thisCard[i] = createImage(cardProducer); usedCards[randomCard] = true; } } } public void paint(Graphics g) { int i,startPt; for (i=0;i<imgCt;i++) { startPt = (i*(cardWidth+10)); g.drawImage(thisCard[i],startPt+3,3,this); g.drawRoundRect(startPt+1,1,cardWidth+2,cardHeight+2,5,5); } } }