วันพุธที่ 7 สิงหาคม พ.ศ. 2556

loops.:hard

Question...  ( Wall )Write a program to draw a wall of bricks, as shown in figure8.10.  Use the libraly method drawRect to draw each brick.This is invoked like this:
                 
                 
                   g. drawRect(x,y,width,height);

                  All the parameters are integers,representing a pixel number.x,y are the coordinates of the top left of the rectangle. Hint: use nested loops.
               

Answer....

               void setup(){}
               void draw() {
                  int flats=5;
                  int floors=4;
                  int yCoord=10;
                  int xCoord;
               for (int floor=0;floor<floors;floor++) {
                  xCoord=0;
                  for (int flat=0;flat<flats;flat++) {
                     rect(xCoord, yCoord, 18, 10);
                     rect(xCoord+9, yCoord+10, 18, 10);
                     xCoord=xCoord+20;
                  }
                  yCoord=yCoord+20;
                }
              }



source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 136
ISBN : 0-13010922-3
การแสดงผล



ไม่มีความคิดเห็น:

แสดงความคิดเห็น