Random positioning at random times help

Hi, I have a small game in which the player has to shoot targets which pop up in the windows of a house. So far, I have all the shooting etc working, but I have written the code myself for when and where the targets are being placed. I only have 4 targets which pop up at the moment, so obviously its not a very long game and I know there must be a much easier and faster way than typing out loads of code for everytime a target comes up.

So what I’m asking is if anyone could help me by showing me how I could randomly select a position from 2 arrays(one for both x and y positions) which I can then use to draw Images from.
This may seem obvious, but I’m still new to java and I’m not very good with arrays and such.
Thanks in advance to anyone who can help me, also if you need any more information I can tell you.

Source:
http://pastebin.java-gaming.org/2d782976c23

You should read about arrays and for loops. You shouldn’t be doing this:


     if(target1){
         if(time/200>=2){
            targ1x=125;
            targ1y=150;
            target50.draw(targ1x, targ1y);
         }
      }
      if(target2){
         if(time/20>=35){
            targ2x = 275;
            targ2y = 500;
            target50.draw(targ2x, targ2y);
         }
      }
      if(target3){
         if(time/20>=43){
            btargx=310;
            btargy=125;
            target.draw(btargx,btargy);
         }
      }
      if(target4){
         
         if(time/20>=60){
            targ1x = 65;
            targ1y = 450;
            target50.draw(targ1x, targ1y);
         }

You should be doing something like :


         if(time/20>=60){

         for (int i=0;i<NUM ;i++) /* this will repeat NUM times for all your targets */
              {    
               targx[i] = 65;
               targy[i] = 450;
               targy[i].draw(  targx[i], targy[i]);
               }
         }

This is still not the best way, since X and Y should be members of class, but let’s leave as it is for now.
Read about what I told you and you’ll dramatically reduce your code.

I’ll read about them now, I’ve taught myself from the internet so I don’t know loads about the most efficient ways of doing things.

Also, for the code you posted, would that work for different times too?