A simple 2D pong game in darkGDK

In case you are wondering, Pong is a video game that simulates ping pong. It is a game where you maneuver a bat or paddle by pressing a control or joystick and the goal of the game is to return the ball to the opposing side just like the real ping pong. If you totally don't have any idea how pong works, I suggest googling it first before moving onto the next section. Being accustomed with the game and how it works greatly aids in trasforming it into codes.

Apart from familiarizing the game, we also need to learn few darkGDK game operations and the underlying commands behind them. These operations and commands are enumerated below:

1. Displaying an Image

Before you can display an image in darkGDK, you'll need to load it first into the computer memory. To be able to do that, use the dbLoadImage command which has the following syntax:
  1. dbLoadImage("Imagefilename",imageloadid);  
Where;
1. Imagefilename is an existent image file saved inside your game project folder. darkGDK supports .bmp, .jpg, and .png image file types.

2. imageloadid is a unique image numeric id that can be used later on to refer to a loaded image.

Example:
  1. dbLoadImage("Galaxian.jpg",1);  
Once an image is loaded in the computer memory, you can then display it by using dbPasteImage or dbSprite. dbSprite has the following syntax:
  1. dbSprite(spriteid, x-coordinate, y-coordinate, imageloadid);  
Where;
1. spriteid is a numerical id that can be use to manipulate the displayed image. spriteid can be identical to the imageloadid.

2. x-coordinate and y-coordinate are the x and y coordinate of the upper-left corner of your sprite or image. If you are unfamiliar of x and y coordinate, think of the screen as the fourth quadrant of the Cartesian coordinate system. The upper left corner is the origin which has the coordinate of x=0,y=0. The value of x coordinate increases as you move right. The value of the y coordinate increases as you move down.

3. imageloadid is the numeric id of a loaded image that you wanted to display.

Example:
  1. dbSprite(1,0,01);  
2. Moving Sprites

Sprites and images can be moved or animated by using dbMoveSprite or by simply incrementing or decrementing the x and y coordinate of an image. If you are familiar with java, c++, or visual basic, you probably know what I am talking about.

3. Detecting Collisions

darkGDK comes with powerful command that can be used to detect sprite or object collision. To detect collision, simply use dbSpriteCollission or dbSpriteHit. dbSpriteHit has the following syntax:
  1. dbSpriteHit(Spriteid1, Spriteid2);  
Where;
1. Spriteid1 and Spriteid2 are the numeric spriteids of the images that you wanted to test for collision.

Now that you have learned few basic commands and operations in darkGDK,I guess we can proceed into making our pong game. The following steps demonstrates how:

1. Click Start>All Programs>Microsoft Visual C++ Express Edition.

2. Click on File>New>Project. In the Project Types tree control, select on Wizards.

3. Select Dark GDK-Game from the available Game project options.

4. Type “Pong” no quotes in the Project Name textbox then Click Ok.

5. Click on View>Solution Explorer. Locate Main.cpp in the Solution Explorer Window then double-click it.

6. The following should then appear:

  1. // Dark GDK - The Game Creators - www.thegamecreators.com     
  2. // the wizard has created a very simple project that uses Dark GDK     
  3. // it contains the basic code for a GDK application       
  4. // whenever using Dark GDK you must ensure you include the header file     
  5. #include "DarkGDK.h"      
  6. // the main entry point for the application is this function     
  7. void DarkGDK ( void )     
  8. {     
  9. // turn on sync rate and set maximum rate to 60 fps     
  10. dbSyncOn   ( );     
  11. dbSyncRate ( 60 );       
  12. // our main loop     
  13. while ( LoopGDK ( ) )     
  14. {     
  15. // update the screen     
  16. dbSync ( );     
  17. }       
  18. // return back to windows     
  19. return;     
  20. }     

7. Locate the line #include "DarkGDK.h" and declare the following variables below it:
  1. //handles the ball velocity, the larger the value   
  2. //the faster the ball will move  
  3. int ballxvelocity=5,ballyvelocity=5;  
  4. //handles the updated x and y coordinate of the ball  
  5. int ballxcoord,ballycoord;  
  6. //handles the updated x and y coordinate   
  7. //of player1's paddle  
  8. int bat1xcoord,bat1ycoord;  
  9. //handles the updated x and y coordinate   
  10. //of player2's paddle  
  11. int bat2xcoord,bat2ycoord;  

8. Locate the line dbSyncOn and dbSynRate(60) then type the following below it:
  1. //sets the window title to pong  
  2. dbSetWindowTitle("Pong");  
  3. //sets the window size to 600x400 that is  
  4. //x coordinates runs from 0-600  
  5. //y coordinates runs from 0-400  
  6. dbSetWindowSize(600,400);  
  7. //loads an image located in your game project folder  
  8. //and gives it an id of 1  
  9. dbLoadImage("ball.png",1);  
  10. //loads an image located in your game project folder  
  11. //and gives it an id of 2  
  12. dbLoadImage("bat1.jpg",2);  
  13. //loads an image located in your game project folder  
  14. //and gives it an id of 3  
  15. dbLoadImage("bat2.jpg",3);  
  16.   
  17. //displays the ball at the center of the screen  
  18. dbSprite(1,300,200,1);  
  19. //player1 paddle at the left side of the screen  
  20. dbSprite(2,50,150,2);   
  21. //player2 paddle at the right side of the screen   
  22. dbSprite(3,500,150,3);  


9. Locate the line while ( LoopGDK ( ) ) and type the following after the open curly bracket:
  1. //move the ball  
  2. //adds the value of ballxvelocity   
  3. //and the current x coordinate of the ball  
  4. //and assign it as a new ball x coord value  
  5. ballxcoord=dbSpriteX(1) + ballxvelocity;  
  6. //do the same with y  
  7. ballycoord=dbSpriteY(1) + ballyvelocity;  
  8. //displays the ball in the updated x and y coord positions  
  9. dbSprite(1,ballxcoord,ballycoord,1);  
  10. //if the ball collides with the   
  11. //top or bottom of the screen then  
  12. if (ballycoord <= 0 || ballycoord >= 400)   
  13. {  
  14. //Multiply ballyvelocity by negative 1  
  15. //The result will  later on be added to the  
  16. //current y coordinate of the ball  
  17. //causing the ball to change position  
  18. //depending on the value of ballyvelocity and  
  19. //the current coord of the ball  
  20. ballyvelocity=ballyvelocity*-1;  
  21. }  
  22. //if the ball collides with the left side   
  23. //or right side of the screen then  
  24. if (ballxcoord <= 0 || ballxcoord >= 600)  
  25. {  
  26. //Multiply ballxvelocity by negative 1  
  27. //The result will later on be added to the  
  28. //current x coordinate of the ball  
  29. //causing the ball to change position  
  30. //depending on the value of ballxvelocity and  
  31. //the current x coord of the ball  
  32. ballxvelocity=ballxvelocity*-1;  
  33. }  
  34.   
  35. //check bat collision  
  36. //if the ball collides with player 1's paddle  
  37. if (dbSpriteHit(1,2)==1)  
  38. {  
  39. //if at this point you still don't   
  40. //understand what this do  
  41. //I suggest recalling the rules in multiplying  
  42. //and adding negative numbers  
  43. ballxvelocity=ballxvelocity*-1;  
  44. }  
  45. //if the ball collides with player 2's paddle  
  46. if (dbSpriteHit(1,3)==1)  
  47. {  
  48. //reverse the direction of the ball  
  49. ballxvelocity=ballxvelocity*-1;  
  50. }  
  51.   
  52. //move the player 1 bat  
  53. //if the enter key is pressed  
  54. if (dbReturnKey()==1)  
  55. {  
  56. //decrement the paddle y coord value  
  57. //causing the paddle to move up  
  58. bat1ycoord=dbSpriteY(2)-5;  
  59. //display the paddle in the  
  60. //updated position  
  61. dbSprite(2,bat1xcoord,bat1ycoord,2);  
  62. }  
  63. //if the down arrow is pressed  
  64. if (dbShiftKey()==1)  
  65. {    
  66. //increment the paddle y coord value  
  67. //causing the paddle to move down  
  68. bat1ycoord=dbSpriteY(2)+5;  
  69. //display the paddle in its  
  70. //updated position  
  71.   
  72.   
  73.   
  74. //move player 2 bat  
  75. //if the up arrow is pressed  
  76. if (dbUpKey()==1)  
  77. {  
  78. //decrement the paddle y coord value  
  79. //causing the paddle to move up  
  80. bat2ycoord=dbSpriteY(3)-5;  
  81. dbSprite(3,bat2xcoord,bat2ycoord,3);  
  82. }  
  83. //if the shift key is pressed  
  84. if (dbDownKey()==1)  
  85. {  
  86. //increment the paddle y coord value  
  87. //causing the paddle to move down  
  88. bat2ycoord=dbSpriteY(3)+5;  
  89. dbSprite(3,bat2xcoord,bat2ycoord,3);  
  90. }  
10. Locate the line dbSync ( ); and type the following after the open curly bracket:
  1. //Deletes the loaded image  
  2. //and sprites when the game is closed  
  3. //this will free up the used memory  
  4. for ( int i = 1; i < 30; i++ )  
  5. dbDeleteSprite ( i );  
  6. dbDeleteImage ( 1 );  
11. Let's have a glance of our codes again and this time comments omitted:
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8. int ballxvelocity=5,ballyvelocity=5;  
  9. int ballxcoord,ballycoord;  
  10. int bat1xcoord,bat1ycoord;  
  11. int bat2xcoord,bat2ycoord;  
  12. // the main entry point for the application is this function  
  13. void DarkGDK ( void )  
  14. {  
  15. // turn on sync rate and set maximum rate to 60 fps  
  16. dbSyncOn   ( );  
  17. dbSyncRate ( 60 );  
  18. dbSetWindowTitle("Pong");  
  19. dbSetWindowSize(600,400);  
  20. dbLoadImage("ball.png",1);  
  21. dbLoadImage("bat1.jpg",2);  
  22. dbLoadImage("bat2.jpg",3);  
  23.   
  24.    
  25. dbSprite(1,300,200,1);     
  26. dbSprite(2,50,150,2);    
  27. dbSprite(3,500,150,3);    
  28.   
  29.   
  30.   
  31. // our main loop  
  32. while ( LoopGDK ( ) )  
  33. {  
  34. //move the ball  
  35. ballxcoord=dbSpriteX(1) + ballxvelocity;  
  36. ballycoord=dbSpriteY(1) + ballyvelocity;  
  37. dbSprite(1,ballxcoord,ballycoord,1);  
  38. if (ballycoord <= 0 || ballycoord >= 400)   
  39. {  
  40. ballyvelocity=ballyvelocity*-1;  
  41. }  
  42. if (ballxcoord <= 0 || ballxcoord >= 600)  
  43. {  
  44. ballxvelocity=ballxvelocity*-1;  
  45. }  
  46.   
  47. //check bat collission  
  48. if (dbSpriteHit(1,2)==1)  
  49. {  
  50.   
  51. ballxvelocity=ballxvelocity*-1;  
  52. }  
  53. if (dbSpriteHit(1,3)==1)  
  54. {  
  55.   
  56. ballxvelocity=ballxvelocity*-1;  
  57. }  
  58.   
  59. //moves player 1 bat  
  60. if (dbReturnKey()==1)  
  61. {  
  62. bat1ycoord=dbSpriteY(2)-5;  
  63. dbSprite(2,bat1xcoord,bat1ycoord,2);  
  64. }  
  65. if (dbShiftKey()==1)  
  66. {  
  67. bat1ycoord=dbSpriteY(2)+5;  
  68. dbSprite(2,bat1xcoord,bat1ycoord,2);  
  69. }  
  70.   
  71.   
  72. //moves player 2 bat  
  73. if (dbUpKey()==1)  
  74. {  
  75. bat2ycoord=dbSpriteY(3)-5;  
  76. dbSprite(3,bat2xcoord,bat2ycoord,3);  
  77. }  
  78. if (dbDownKey()==1)  
  79. {  
  80. bat2ycoord=dbSpriteY(3)+5;  
  81. dbSprite(3,bat2xcoord,bat2ycoord,3);  
  82. }  
  83.   
  84.   
  85. // update the screen  
  86. dbSync ( );  
  87. }  
  88. for ( int i = 1; i < 30; i++ )  
  89. {  
  90. dbDeleteSprite ( i );  
  91. dbDeleteImage ( 1 );  
  92. }  
  93. // return back to windows  
  94. return;  
  95. }  

12. Press F5 to test the game. You can press the Enter key and shift key to move player1's bat and the up and down arrow to move player2's bat.

13. And that's it. A very simple pong game using few lines of codes. At the point of writing I guess this was the very first pong game tutorial written using darkGDK. I will add attract mode animation, menus, scores, and AI enemy to this when I have time but for now, though it's incomplete, you can use it as starting point. Hope you'll learn something from it and enjoy the game.

A simple 2D Color Slot Machine in Dark GDK

I’m sure every one of us is familiar with slot machine or fruit machine game. Today we will be making a slight version of that game, and we are going to name it as “Color Slot Machine”. There are several elements missing in this game such as the game title screen and the game overview screen so I’ll just explain to you the mechanics of the game. Before the game starts you have an initial account of 100000. To play the game, click on the Spin button. After clicking on the Spin button the computer will generate random colors and your score depends upon the generated color. The scoring system is presented below:

ColorWin
Red-Green-Blue+2000
Red-Red-Red+1000
Green-Green-Green+1000
Blue-Blue-Blue+1000
Others-1000
The following steps demonstrate how to create this game:

1.Prepare the images that will be needed in your game. You can use Ms-Paint, Photoshop or GIMP. The following images are required for this game:



2. After preparing the images, Start Visual C++>File>New>Projects>Wizards>Select Dark GDK-Game>Type “Color Machine” in the name textbox (no quotes).

3. Assuming that you have saved the project in the default directory, minimize Microsoft Visual C++ then go to My Documents>Visual Studio 2008>Projects>Color Machine>Color Machine>Then paste the images that you have made in Step 1.



4. Maximize Visual C++>View>Solution Explorer>Double-click Main.cpp. The following should then come into view:

  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8.   
  9. // the main entry point for the application is this function  
  10. void DarkGDK ( void )  
  11. {  
  12.  // turn on sync rate and set maximum rate to 60 fps  
  13.  dbSyncOn   ( );  
  14.  dbSyncRate ( 60 );  
  15.   
  16.  // our main loop  
  17.  while ( LoopGDK ( ) )  
  18.  {  
  19.   // update the screen  
  20.   dbSync ( );  
  21.  }  
  22.   
  23.  // return back to windows  
  24.  return;  
  25. }  

5. Locate the line #include "DarkGDK.h" then declare the following variables below it:
  1. //handles the spriteid of the left box  
  2. int intred;  
  3. //handles the spriteid of the middle box  
  4. int intgreen;  
  5. //handles the spriteid of the right box  
  6. int intblue;  
  7. //handles our loop  
  8. int intcounter;  
  9. //determines if the spin button has been clicked  
  10. int intclicked;  
  11. //displays our status text  
  12. char* chrmessage;  
  13. //handles the balance  
  14. int intbalance=100000;  

6. Locate the line dbSynRate(60); then replace the 60 with 10 so that dark GDK will try to play 10 frames per second and our loop will not play funny , then type the following after it:
  1. //sets the window title  
  2. dbSetWindowTitle("Color Slot Machine");  
  3. //sets the window size  
  4. dbSetWindowSize(400,400);  
  5. //loads our images  
  6. dbLoadImage("red.png",1);  
  7. dbLoadImage("green.png",2);  
  8. dbLoadImage("blue.png",3);  
  9. dbLoadImage("backdrop.png",4);  
  10. dbLoadImage("Spin.png",5);  
  11. dbLoadImage("pointer.png",6);  
  12. //displays our images  
  13. dbSprite(4,0,0,4);  
  14. //sets our backdrop alpha transparency  
  15. //so that our text will be visible  
  16. dbSetSpriteAlpha(4,150);  
  17. dbSprite(5,240,380,5);  
  18. dbSprite(1,130,100,1);  
  19. dbSprite(2,260,100,2);  
  20. dbSprite(3,390,100,3);  

7. Locate the Line while ( LoopGDK ( ) ) then type the following after the open curly brace:
  1. //sets text size to 30 points  
  2. dbSetTextSize(30);  
  3. //displays the balance  
  4. //I still don’t know how to concanate text  
  5. dbText(0,0,"Balance:");  
  6. dbText(150,0,dbStr(intbalance));  
  7.   //displays our status text  
  8.   dbText(250,250,chrmessage);  
  9.   //pessimistic programming  
  10.   //assume that the button has not yet been clicked  
  11.   intclicked=0;  
  12.   //hides our customized mouse pointer  
  13.   //so that only the mouse pointer is visible  
  14.   dbHideSprite(6);  
  15.   //Enables dbRnd to generate an unpredictable  
  16.   //random number generation  
  17.   dbRandomize(dbTimer());  
  18.   dbSetSprite(6,1,100);  
  19.   //enables our sprite to follow the mouse  
  20.   dbSprite(6,dbMouseX(),dbMouseY(),6);  
  21.   //if our hidden sprite collided with the mouse  
  22.   //and the button has been clicked  
  23.   if(dbSpriteHit(6,5)==1 && dbMouseClick()==1)  
  24.   {  
  25.   //assigns 1 to inclicked variable  
  26.   //resets the value of chrmessage  
  27.   intclicked=1;  
  28.   chrmessage="";  
  29.   //generate a random color 10 times  
  30.   for(intcounter=1;intcounter<=10;intcounter++)  
  31.   {  
  32.    intred=1 + dbRnd(2);  
  33.    intblue=1 + dbRnd(2);  
  34.    intgreen=dbRnd(2)+1;  
  35.    dbSprite(1,130,100,intred);  
  36.    dbSprite(2,260,100,intgreen);  
  37.    dbSprite(3,390,100,intblue);  
  38.    dbSync();  
  39.   }  
  40.   }  
  41.     
  42.   //check if the button has been clicked  
  43.   //if it was clicked then  
  44.   
  45.   if (intclicked==1)  
  46.   {  
  47.    //check for color matches  
  48.    if (intred==1 && intblue==1 && intgreen==1)  
  49.    {  
  50.    chrmessage="You win 1000!";  
  51.    intbalance=intbalance+1000;  
  52.     
  53.    }  
  54.    else if (intred==2 && intblue==2 && intgreen==2)  
  55.    {  
  56.    chrmessage="You win 1000!";  
  57.    intbalance=intbalance+1000;  
  58.    }  
  59.    else if (intred==3 && intblue==3 && intgreen==3)  
  60.    {  
  61.    chrmessage="You win 1000!";  
  62.    intbalance=intbalance+1000;  
  63.    }  
  64.    else if (intred==1 && intblue==2 && intgreen==3)  
  65.    {  
  66.    chrmessage="You win 2000!";  
  67.    intbalance=intbalance+1000;  
  68.    }  
  69.    //if no color matches then  
  70.    else  
  71.    {  
  72.    chrmessage="You Lose!";  
  73.    intbalance=intbalance-1000;  
  74.    }  
  75.   }  
  76.   //if the balance is less than or equal to zero  
  77.   if (intbalance<=0)  
  78.   {  
  79.   dbCLS();  
  80.   dbSetTextSize(32);  
  81.   dbText(180,250,"Insufficient balance");  
  82.   dbText(250,300,"GAME OVER");  
  83.   dbSync();  
  84.   dbWait(4000);  
  85.   return;  
  86.  }  
8. Let’s have a look of our code once again and this time, crappy comments ommitted.
  1. // Dark GDK - The Game Creators - www.thegamecreators.com  
  2.   
  3. // the wizard has created a very simple project that uses Dark GDK  
  4. // it contains the basic code for a GDK application  
  5.   
  6. // whenever using Dark GDK you must ensure you include the header file  
  7. #include "DarkGDK.h"  
  8. int intred;  
  9. int intblue;  
  10. int intgreen;  
  11. int intcounter;  
  12. int intclicked;  
  13. char* chrmessage;  
  14. int intbalance=100000;  
  15. // the main entry point for the application is this function  
  16. void DarkGDK ( void )  
  17. {  
  18.   
  19.  // turn on sync rate and set maximum rate to 60 fps  
  20.  dbSyncOn   ( );  
  21.  dbSyncRate ( 20 );  
  22.  dbSetWindowTitle("Color Slot Machine");  
  23.  dbSetWindowSize(400,400);  
  24.  dbSetGamma(500,255,500);  
  25.  dbLoadImage("red.png",1);  
  26.  dbLoadImage("green.png",2);  
  27.  dbLoadImage("blue.png",3);  
  28.  dbLoadImage("backdrop.png",4);  
  29.  dbLoadImage("Spin.png",5);  
  30.  dbLoadImage("pointer.png",6);  
  31.  dbSprite(4,0,0,4);  
  32.  dbSetSpriteAlpha(4,150);  
  33.  dbSprite(5,240,380,5);  
  34.  dbSprite(1,130,100,1);  
  35.  dbSprite(2,260,100,2);  
  36.  dbSprite(3,390,100,3);  
  37.   
  38.  // our main loop  
  39.  while ( LoopGDK ( ) )  
  40.  {  
  41.   
  42.   dbSetTextSize(30);  
  43.   dbText(0,0,"Balance:");  
  44.   dbText(150,0,dbStr(intbalance));  
  45.   dbText(250,250,chrmessage);  
  46.   intclicked=0;  
  47.   dbHideSprite(6);  
  48.   dbRandomize(dbTimer());  
  49.   dbSetSprite(6,1,100);  
  50.   dbSprite(6,dbMouseX(),dbMouseY(),6);  
  51.   if(dbSpriteHit(6,5)==1 && dbMouseClick()==1)  
  52.   {  
  53.   intclicked=1;  
  54.   chrmessage="";  
  55.    for(intcounter=1;intcounter<=10;intcounter++)  
  56.    {  
  57.    intred=1 + dbRnd(2);  
  58.    intblue=1 + dbRnd(2);  
  59.    intgreen=dbRnd(2)+1;  
  60.    dbSprite(1,130,100,intred);  
  61.    dbSprite(2,260,100,intgreen);  
  62.    dbSprite(3,390,100,intblue);  
  63.    dbSync();  
  64.    }  
  65.   }  
  66.   
  67.   if (intclicked==1)  
  68.   {  
  69.    if (intred==1 && intblue==1 && intgreen==1)  
  70.    {  
  71.    chrmessage="You win 1000!";  
  72.    intbalance=intbalance+1000;  
  73.     
  74.    }  
  75.    else if (intred==2 && intblue==2 && intgreen==2)  
  76.    {  
  77.    chrmessage="You win 1000!";  
  78.    intbalance=intbalance+1000;  
  79.    }  
  80.    else if (intred==3 && intblue==3 && intgreen==3)  
  81.    {  
  82.    chrmessage="You win 1000!";  
  83.    intbalance=intbalance+1000;  
  84.    }  
  85.    else if (intred==1 && intblue==2 && intgreen==3)  
  86.    {  
  87.    chrmessage="You win 2000!";  
  88.    intbalance=intbalance+1000;  
  89.    }  
  90.    else  
  91.    {  
  92.    chrmessage="You Lose!";  
  93.    intbalance=intbalance-1000;  
  94.    }  
  95.   }  
  96.     
  97.   if (intbalance<=0)  
  98.   {  
  99.   dbCLS();  
  100.   dbSetTextSize(32);  
  101.   dbText(180,250,"Insufficient balance");  
  102.   dbText(250,300,"GAME OVER");  
  103.   dbSync();  
  104.   dbWait(4000);  
  105.   return;  
  106.   }  
  107.   
  108.   // update the screen  
  109.     
  110.   dbSync ( );  
  111.  }  
  112.   
  113.  // return back to windows  
  114.  return;  
  115. }  
9. Press F5 to test our game. Click the Spin button and see what happens.

Binding a fieldname to a control programmatically in Visual J#

Before the Database Connection and Binding process I want you to make an Ms-Access database file named “dbEmployee” containing a table named “tblEmployee”. Use the following specifications:
Field Name Data Type Description
chrempno text Handles employee id
chrfname text Handles employee’s name
chrlname text Holds employee’s last name

After designing the structure of your table, you can enter appropriate values for each field. For instance:
chrempno chrfname Chrlname
2010A John Doe

Now that we are done creating a table and adding appropriate values to it, we can now link to that table and bind the fields to our windows application control by following these steps:

1. Start>All programs>Visual J# 2005 Express Edition.

2. Click File>New>Project>Select Windows Application from the windows studio installed templates then click Ok.

3. A new form will appear. Before adding appropriate controls to our form let us first establish a connection to our dbEmployee database file. To do this we will use a database access tool called OleDbDataAdapter. OleDbDataAdapter will enable us to set the filename of a database file and the name of the table that we wanted to be made available in our project. It also permits us to set-up how the record values will be displayed on our form by using an appropriate SQL statement.

4. By default, OleDbDataAdapter is not shown on the Visual J# 2005 Express control toolbox, to add it, click Tools>Choose control toolbox>Type “ole” (no quotes ) on the filter textbox then check all the items that starts with “ole” and finally click the Ok button. OleDbDataAdapter should now appear on your toolbox.

5. Expand the All Windows Forms toolbox category then double-click OleDbDataAdapter.

6. A Data Adapter configuration wizard will then appear. Click the new connection button>Select Microsoft Access Database File from the data source list.

7. Click the browse button then locate your dbEmployee.mdb file then click the Ok button.

8. Click Next>A message box containing the following prompt will appear:
“The connection you selected uses a local data file that is not in the current project would you like to your project for the connection? If you copy the data file to your project, it will be copied to the project’s output directory”> just click the Ok button then click next.

9. A Generate SQL statement, type “SELECT * FROM tblEmployee” (no quotes) this will export all the record value of our table to our project then. Click Next after typing the SQL statement and finally click the Finish Button.

10. After specifying the table name, right-click OleDbDataAdapter1 from the bottom portion of Visual J# IDE then click Generate Data Set then click the Ok button. If you are wondering what a dataset is, a Dataset is an imaginary box the holds the field names of your table. It is use a temporary storage box for table data.

11. At this point, you will need to design the interface of your application. Double-click the label tool. In the properties window, change its text to “Employee number:” (no quotes).

12. Double-click the label tool and put it in the right-side of your Employee number label. Change its name to Empnumberlabel.

13. Add another label below the Employee number label and change its text to “Firstname:” (no quotes). Put a label on its right side and name it Fnamelabel.

14. Add another label below the Firstname label and change its text to “Lastname:” (no quotes). Put another label on its right side and name it Lnamelabel.

15. Double-click your form, type the following code:
  1. //fills our dataset with record values  
  2. this.oleDbDataAdapter1.Fill(this.dataSet11);  
  3. //Use the dataset methods collection to retrieve the record values  
  4. //The get_Item() method after the get_Rows() method retrieves the table row value  
  5. //while the last get_Item retrieves the table column value  
  6. Empnumberlabel.set_Text(String.valueOf(dataSet11.get_Tables().get_Item("tblEmployee").get_Rows().get_Item(0).get_Item(0)));  
  7. //The index number of the first table row is 0, the second row is 1 and so on.  
  8. //Column index number starts from 0, the second table column is 1 and so on.  
  9. Fnamelabel.set_Text(String.valueOf(dataSet11.get_Tables().get_Item("tblEmployee").get_Rows().get_Item(0).get_Item(1)));  
  10. //Dataset collection methods returns an object data that is why we use the   
  11. //String.valueOf data conversion function to convert it to string.  
  12. Lnamelabel.set_Text(String.valueOf(dataSet11.get_Tables().get_Item("tblEmployee").get_Rows().get_Item(0).get_Item(2)));  
16. Your code should now look like this:
  1. private void Form1_Load(Object sender, System.EventArgs e)  
  2. {  
  3. this.oleDbDataAdapter1.Fill(this.dataSet11);  
  4. Empnumberlabel.set_Text(String.valueOf(dataSet11.get_Tables().get_Item("tblEmployee").get_Rows().get_Item(0).get_Item(0)));  
  5. Fnamelabel.set_Text(String.valueOf(dataSet11.get_Tables().get_Item("tblEmployee").get_Rows().get_Item(0).get_Item(1)));  
  6. Lnamelabel.set_Text(String.valueOf(dataSet11.get_Tables().get_Item("tblEmployee").get_Rows().get_Item(0).get_Item(2)));  
  7. }  
17. Press F5 to test your application.You should now see the following output: