Example #1
0
  /**
   * Set a background image for the world. If the image size is larger than the world in pixels, it
   * is clipped. If it is smaller than the world, it is tiled. A pattern showing the cells can
   * easily be shown by setting a background image with a size equal to the cell size.
   *
   * @see #setBackground(String)
   * @param image The image to be shown
   */
  public final void setBackground(GreenfootImage image) {
    if (image != null) {
      int imgWidth = image.getWidth();
      int imgHeight = image.getHeight();
      int worldWidth = getWidthInPixels();
      int worldHeight = getHeightInPixels();
      boolean tile = imgWidth < worldWidth || imgHeight < worldHeight;

      if (tile) {
        backgroundIsClassImage = false;
        backgroundImage = new GreenfootImage(worldWidth, worldHeight);
        backgroundImage.setColor(DEFAULT_BACKGROUND_COLOR);
        backgroundImage.fill();

        for (int x = 0; x < worldWidth; x += imgWidth) {
          for (int y = 0; y < worldHeight; y += imgHeight) {
            backgroundImage.drawImage(image, x, y);
          }
        }
      } else {
        // To make it behave exactly the same way as when tiling we
        // should make a clone here. But it performs better when not
        // cloning.
        // Performance will be an issue for people changing the
        // background image all the time for animated backgrounds
        backgroundImage = image;
      }
    } else {
      backgroundIsClassImage = false;
      backgroundImage = null;
    }
  }
  /** Update the image on screen to show the current value. */
  private void updateImage() {
    GreenfootImage image = new GreenfootImage(background);
    GreenfootImage text =
        new GreenfootImage(prefix + "Points: " + value, 22, Color.MAGENTA, transparent);

    if (text.getWidth() > image.getWidth() - 20) {
      image.scale(text.getWidth() + 20, image.getHeight());
    }

    image.drawImage(
        text, (image.getWidth() - text.getWidth()) / 2, (image.getHeight() - text.getHeight()) / 2);
    setImage(image);
  }
Example #3
0
  /**
   * Change the background colour of this Button
   *
   * @param background The colour to use
   */
  public void update(Color background) {
    GreenfootImage tempTextImage =
        new GreenfootImage(buttonText, textSize, Color.BLACK, background);

    myImage = new GreenfootImage(tempTextImage.getWidth() + 8, tempTextImage.getHeight() + 8);
    myImage.setColor(Color.BLACK);
    myImage.fill();
    myImage.drawImage(tempTextImage, 4, 4);

    myImage.setColor(Color.BLACK);
    myImage.drawRect(0, 0, tempTextImage.getWidth() + 7, tempTextImage.getHeight() + 7);
    setImage(myImage);
  }
Example #4
0
  /**
   * Change the text displayed on this Button
   *
   * @param text String to display
   */
  public void update(String text) {
    Color buttonFill = new Color(242, 206, 27); // yellow fill colour

    buttonText = text;
    GreenfootImage tempTextImage = new GreenfootImage(text, 20, Color.BLACK, buttonFill);
    myImage = new GreenfootImage(tempTextImage.getWidth() + 8, tempTextImage.getHeight() + 8);
    myImage.setColor(Color.BLACK);
    myImage.fill();
    myImage.drawImage(tempTextImage, 4, 4);

    myImage.setColor(Color.BLACK);
    myImage.drawRect(0, 0, tempTextImage.getWidth() + 7, tempTextImage.getHeight() + 7);
    setImage(myImage);
  }
Example #5
0
 // Method runs to update number of mana potions on screen.
 private void updateImageTwo() {
   GreenfootImage imageTwo =
       new GreenfootImage(fullMana * heightTwo + 64, heightTwo); // create the image's object
   imageTwo.drawRect(
       0, 0, imageTwo.getWidth() - 1, imageTwo.getHeight() - 1); // frame added to the object
   GreenfootImage potions = new GreenfootImage("mana_Ahrenn.png"); // obtains image of potion
   potions.scale(4 * heightTwo / 6, 4 * heightTwo / 6); // resizes image of potion
   for (int i = 0; i < mana; i++) {
     imageTwo.drawImage(
         potions,
         4 * +4 + i * 9 * 4,
         4); // adds a specific number of potions to image based on amount of mana remaining
   }
   setImage(imageTwo); // Actually makes the image look like how the values above desire it to be.
 }
Example #6
0
 // Method runs to update number of hearts on screen.
 private void updateImage() {
   int heightOne = (int) height; // value of height for hearts recasted
   GreenfootImage image =
       new GreenfootImage(fullLives * heightOne + 64, heightOne); // create the image's object
   image.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1); // frame added to the object
   GreenfootImage hearts = new GreenfootImage("hearts_Ahrenn.png"); // obtains image of heart
   hearts.scale(4 * heightOne / 6, 4 * heightOne / 6); // resizes image of heart
   for (int i = 0; i < lives; i++) {
     image.drawImage(
         hearts,
         4 * +4 + i * 9 * 4,
         4); // adds a specific number of hearts to image based on number of lives remaining
   }
   setImage(image); // Actually makes the image look like how the values above desire it to be.
 }
Example #7
0
  /** Draws the count down bar for status duration */
  private void drawBar() {

    myImage.drawImage(bar, 0, 40); // draws the bar image on myImage
  }