arrays - Adding multiple images to GPanel with Java -


im building blackjack game in java school , can't seem figure out how add first 4 cards gpanel. array of cards shuffled , strings in array match file name of images. can first card in array load not other three. awesome.

public void paintcomponent(graphics g) {         //system.out.println(gameinprogress);          if(gameinprogress == true) {             super.paintcomponent(g);              graphics2d g2 = (graphics2d) g;             image image = null;             int i;             currentposition = 0;             (i = 0; < 4; i++) {                 image = toolkit.getdefaulttoolkit().getimage("images/"+deck[currentposition - i]+".png");                 currentposition++;             }             g2.drawimage(image, 115, 5, 80, (int) 106.67, this);             g2.finalize();         }     } 

you have 3 main issues in code.

the first issue caused [currentposition - i] because result equal 0, image/card @ array index 0 1 ever drawn.

the second issue draw 1 image because g2.drawimage called after loop, instead should inside loop 4 images/cards drawn.

the third issue draw images in same place, if drawing 4 images see last 1 because covers previous ones.

try this, should print 4 images (assuming have 435x112 panel):

public void paintcomponent(graphics g) {     //system.out.println(gameinprogress);      if(gameinprogress == true) {         super.paintcomponent(g);          //add couple new variables         int x = 115;          graphics2d g2 = (graphics2d) g;         image image = null;         int i;         (i = 0; < 4; i++) {             image = toolkit.getdefaulttoolkit().getimage("images/"+deck[i]+".png");             //new location of g2.drawimage             //the x positions should change each time             g2.drawimage(image, x, 5, 80, 107, this);             //change x location of next image             x = x + 80;         }         //moved g2.drawimage here above bracket         g2.finalize();     } }