コード例 #1
0
  public Point getEmptyPosition(Dimension spriteSize) {
    Rectangle trialSpaceOccupied = new Rectangle(0, 0, spriteSize.width, spriteSize.height);
    Random rand = new Random(System.currentTimeMillis());
    boolean empty = false;
    int numTries = 0;

    // Search for an empty position
    while (!empty && numTries++ < 100) {
      // Get a trial position
      trialSpaceOccupied.x = Math.abs(rand.nextInt() % backgroundImage.getSize().width);
      trialSpaceOccupied.y = Math.abs(rand.nextInt() % backgroundImage.getSize().height);
      // Iterate through existing
      // sprites, checking if position
      // is empty
      boolean collision = false;
      for (int cnt = 0; cnt < size(); cnt++) {
        Rectangle testSpaceOccupied = ((Sprite) elementAt(cnt)).getSpaceOccupied();
        if (trialSpaceOccupied.intersects(testSpaceOccupied)) {
          collision = true;
        } // end if
      } // end for loop
      empty = !collision;
    } // end while loop
    return new Point(trialSpaceOccupied.x, trialSpaceOccupied.y);
  } // end getEmptyPosition()
コード例 #2
0
 public void drawScene(Graphics g) {
   // Draw the background and erase
   // sprites from graphics area
   // Disable the following statement
   // for an interesting effect.
   backgroundImage.drawBackgroundImage(g);
   // Iterate through sprites, drawing
   // each sprite
   for (int cnt = 0; cnt < size(); cnt++) ((Sprite) elementAt(cnt)).drawSpriteImage(g);
 } // end drawScene()