Translate

четверг, 24 мая 2012 г.

Android programming for beginners. "Hangman" the word game developing for android. Program drawing.

 It's second part of "handman" game making for android. In this article i will teach you about program drawing. (First part)



 In first, we need to render all letters in table way in alphabet order. Fields in LetterField class:
  
String alphabet="АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";//Russian script
double letter_size=20;
int letter_color=Color.BLACK;
int rows=7;
int columns=6;
double off_const=0.4;//It's offset of letter's table relative top (the all height is 1)

//Special coeficients for aligning of letter for style improving.
double opt_a=0.20;
double opt_b=0.65;

  In onDraw() method we must determine Paint object. It includes different propetries about style of drawing like colors, sizes, fonts:
Paint paint=new Paint();
paint.setTextSize((float) letter_size);
paint.setColor(letter_color);
paint.setStrokeWidth(1);
//We have set letter's style.

double red_height=(1-off_const)*height;//Its variable is part of height for letters


  Methods of Canvas class for program drawing is:

 -drawLine(Xa, Ya, Xb, Yb, paint) - program drawing line from A to B via paint

 -drawText(X, Y, paint) - draw text in (X;Y) via paint

for (int j=0;j<columns;j++){//Loop for every line
          
            float YLine=(float) ((double)(j)/(double)(columns)*red_height+off_const*height);//Find Y of horizontal grid line
            canvas.drawLine((float)(0),  (float)(YLine),  (float)(width),  (float)(YLine),  paint);//Drig line drawing
          
            //We will draw letters
            for (int i=0;i<rows;i++){//Loop for every line
                if(j*rows+i+1<=alphabet.length()){//Determine  number of letter in alphabet and check allowing of this (we don't can to stuff come cells)
                  
                   float X=(float) ((double)(i)/(double)(rows)*width+opt_a/(double)(columns)*height); //Find X of letter (with horizontal shift for style)
                   float Y=(float) ((double)(j)/(double)(columns)*red_height+opt_b/(double)(columns)*red_height+off_const*height);//Analogically like above
                                              
                   canvas.drawText(alphabet.charAt(j*rows+i)+"", X, Y, paint);//Draw letter
                 
                }
            }
}
      
//Verticlal lines drawing like horizaontal
for (int i=0;i<=rows;i++){      
            float XLine=(float) ((double)(i)/(double)(rows)*width);
            canvas.drawLine((float)(XLine),  (float)(height*off_const),  (float)(XLine),  (float)(height),  paint);  
}
Enjoy about drawed letters in launching.

Next part

Download source of the game

Комментариев нет:

Отправить комментарий

Related Posts Plugin for WordPress, Blogger...