public void updatePosition() {
    Point position = new Point(spaceOccupied.x, spaceOccupied.y);

    // Insert random behavior.  During
    // each update, a sprite has about
    // one chance in 10 of making a
    // random change to its
    // motionVector.  When a change
    // occurs, the motionVector
    // coordinate values are forced to
    // fall between -7 and 7.  This
    // puts a cap on the maximum speed
    // for a sprite.
    if (rand.nextInt() % 10 == 0) {
      Point randomOffset = new Point(rand.nextInt() % 3, rand.nextInt() % 3);
      motionVector.x += randomOffset.x;
      if (motionVector.x >= 7) motionVector.x -= 7;
      if (motionVector.x <= -7) motionVector.x += 7;
      motionVector.y += randomOffset.y;
      if (motionVector.y >= 7) motionVector.y -= 7;
      if (motionVector.y <= -7) motionVector.y += 7;
    } // end if

    // Move the sprite on the screen
    position.translate(motionVector.x, motionVector.y);

    // Bounce off the walls
    boolean bounceRequired = false;
    Point tempMotionVector = new Point(motionVector.x, motionVector.y);

    // Handle walls in x-dimension
    if (position.x < bounds.x) {
      bounceRequired = true;
      position.x = bounds.x;
      // reverse direction in x
      tempMotionVector.x = -tempMotionVector.x;
    } else if ((position.x + spaceOccupied.width) > (bounds.x + bounds.width)) {
      bounceRequired = true;
      position.x = bounds.x + bounds.width - spaceOccupied.width;
      // reverse direction in x
      tempMotionVector.x = -tempMotionVector.x;
    } // end else if

    // Handle walls in y-dimension
    if (position.y < bounds.y) {
      bounceRequired = true;
      position.y = bounds.y;
      tempMotionVector.y = -tempMotionVector.y;
    } else if ((position.y + spaceOccupied.height) > (bounds.y + bounds.height)) {
      bounceRequired = true;
      position.y = bounds.y + bounds.height - spaceOccupied.height;
      tempMotionVector.y = -tempMotionVector.y;
    } // end else if

    if (bounceRequired)
      // save new motionVector
      setMotionVector(tempMotionVector);
    // update spaceOccupied
    setSpaceOccupied(position);
  } // end updatePosition()
Example #2
0
        public void mousePressed(MouseEvent e) {
          requestFocus();
          Point p = e.getPoint();
          int size =
              Math.min(
                  MAX_SIZE,
                  Math.min(
                      getWidth() - imagePadding.left - imagePadding.right,
                      getHeight() - imagePadding.top - imagePadding.bottom));
          p.translate(-(getWidth() / 2 - size / 2), -(getHeight() / 2 - size / 2));
          if (mode == ColorPicker.BRI || mode == ColorPicker.SAT) {
            // the two circular views:
            double radius = ((double) size) / 2.0;
            double x = p.getX() - size / 2.0;
            double y = p.getY() - size / 2.0;
            double r = Math.sqrt(x * x + y * y) / radius;
            double theta = Math.atan2(y, x) / (Math.PI * 2.0);

            if (r > 1) r = 1;

            if (mode == ColorPicker.BRI) {
              setHSB((float) (theta + .25f), (float) (r), bri);
            } else {
              setHSB((float) (theta + .25f), sat, (float) (r));
            }
          } else if (mode == ColorPicker.HUE) {
            float s = ((float) p.x) / ((float) size);
            float b = ((float) p.y) / ((float) size);
            if (s < 0) s = 0;
            if (s > 1) s = 1;
            if (b < 0) b = 0;
            if (b > 1) b = 1;
            setHSB(hue, s, b);
          } else {
            int x2 = p.x * 255 / size;
            int y2 = p.y * 255 / size;
            if (x2 < 0) x2 = 0;
            if (x2 > 255) x2 = 255;
            if (y2 < 0) y2 = 0;
            if (y2 > 255) y2 = 255;

            if (mode == ColorPicker.RED) {
              setRGB(red, x2, y2);
            } else if (mode == ColorPicker.GREEN) {
              setRGB(x2, green, y2);
            } else {
              setRGB(x2, y2, blue);
            }
          }
        }
Example #3
0
 private static JButton getButton(JList<String> list, Point pt, int index) {
   Component c =
       list.getCellRenderer().getListCellRendererComponent(list, "", index, false, false);
   Rectangle r = list.getCellBounds(index, index);
   c.setBounds(r);
   // c.doLayout(); //may be needed for mone LayoutManager
   pt.translate(-r.x, -r.y);
   Component b = SwingUtilities.getDeepestComponentAt(c, pt.x, pt.y);
   if (b instanceof JButton) {
     return (JButton) b;
   } else {
     return null;
   }
 }