Tiny Numbers: You will make an applet containing the number
one through six as shown to the left. Of course, you will have to create
a very small image for each number.
HINT: There are always lots of ways of approaching a problem.
For this applet I predefined arrays to represent the numbers and then I used
these arrays to create the images. I did this in three basic steps:
STEP ONE:
int[]a2={1,1,1,1,1,
0,0,0,0,1,
0,0,1,1,1,
1,1,0,0,0,
1,0,0,0,0,
1,1,1,1,1};
This shows how a two can be defined in a 5x6 grid.
STEP TWO:
public Image makeNumber(int num){
int[]arr=new int[30];
switch(num){
case 1:
arr=a1; break;
case 2:
arr=a2; break;
case 3:
arr=a3; break;
case 4:
arr=a4; break;
case 5:
arr=a5; break;
case 6:
arr=a6; break;
}
for(int i = 0; i
This shows how the predefined arrays are used to create images for each
number. Notice that a generic array called arr is used as a holder for each
of the predefined arrays and then the values contained in each array are
multiplied by the value of yellow.
STEP THREE:
for(int y=20; y<180; y+=8){
for(int x=20; x<180; x+=8){
if( i%6 == 0) g.drawImage(one,x,y,this);
else if( i%6 == 1) g.drawImage(two,x,y,this);
else if( i%6 == 2) g.drawImage(three,x,y,this);
else if( i%6 == 3) g.drawImage(four,x,y,this);
else if( i%6 == 4) g.drawImage(five,x,y,this);
else if( i%6 == 5) g.drawImage(six,x,y,this);
i++;
}
}
Finally, the modulus operator is used to decide which image to draw in the
paint method.