/** * 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); }
/** 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); }
/** * 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); }
public SpriteAnimation(String imageFile, int numFrames, int ticksPerFrame) { try { // Load the raw frames into a frame buffer GreenfootImage baseImg = new GreenfootImage(imageFile); GreenfootImage[] baseFrames = new GreenfootImage[numFrames]; int frameWidth = baseImg.getWidth() / numFrames; int frameHeight = baseImg.getHeight(); BufferedImage rawImg = baseImg.getAwtImage(); for (int x = 0; x < numFrames; x++) { baseFrames[x] = new GreenfootImage(frameWidth, frameHeight); baseFrames[x] .getAwtImage() .setData(rawImg.getSubimage(x * frameWidth, 0, frameWidth, frameHeight).getData()); } // Re-use object references to save from copying the images multiple times frames = new GreenfootImage[numFrames * ticksPerFrame]; flipped = new GreenfootImage[numFrames * ticksPerFrame]; for (int f = 0; f < numFrames; f++) { for (int t = 0; t < ticksPerFrame; t++) { frames[f * ticksPerFrame + t] = baseFrames[f]; flipped[f * ticksPerFrame + t] = new GreenfootImage(baseFrames[f]); flipped[f * ticksPerFrame + t].mirrorHorizontally(); } } } catch (IllegalArgumentException e) { frames = flipped = new GreenfootImage[1]; frames[0] = ERROR_IMAGE; } }
/** * 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; } }
private void printText() { laranjaEscuro = new Color(214, 95, 0); imgHelp.setColor(laranjaEscuro); Font play = new Font("sanserif", Font.BOLD, 30); imgHelp.setFont(play); int x = imgHelp.getWidth() - 130; int y = imgHelp.getHeight() - 16; imgHelp.drawString("Help", x, y); }
private void drawBox() { amarelo = new Color(255, 188, 0); laranja = new Color(255, 133, 0); imgHelp = getImage(); imgHelp.setColor(laranja); imgHelp.fill(); imgHelp.scale(200, 50); imgHelp.setColor(amarelo); int margem = 5; int largura = imgHelp.getWidth() - 2 * margem; int altura = imgHelp.getHeight() - 2 * margem; imgHelp.fillRect(margem, margem, largura, altura); }
/** * Return the color at the center of the cell. To paint a color, you need to get the background * image for the world and paint on that. * * @see #getBackground() * @throws IndexOutOfBoundsException If the location is not within the world bounds. If there is * no background image at the location it will return Color.WHITE. */ public Color getColorAt(int x, int y) { ensureWithinXBounds(x); ensureWithinYBounds(y); int xPixel = (int) Math.floor(getCellCenter(x)); int yPixel = (int) Math.floor(getCellCenter(y)); // Take tiling into account if (isTiled()) { xPixel = xPixel % backgroundImage.getWidth(); yPixel = yPixel % backgroundImage.getHeight(); } // TODO if it is not tiled, and outside, what should be returned? BGcolor? Null? if (xPixel >= backgroundImage.getWidth()) { return Color.WHITE; } if (yPixel >= backgroundImage.getHeight()) { return Color.WHITE; } return backgroundImage.getColorAt(xPixel, yPixel); }
// 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. }
// 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. }
/** * Return the color at the centre of the cell. To paint a color, you need to get the background * image for the world and paint on that. * * @param x The x coordinate of the cell. * @param y The y coordinate of the cell. * @see #getBackground() * @throws IndexOutOfBoundsException If the location is not within the world bounds. If there is * no background image at the location it will return Color.WHITE. */ public Color getColorAt(int x, int y) { ensureWithinXBounds(x); ensureWithinYBounds(y); int xPixel = (int) Math.floor(getCellCenter(x)); int yPixel = (int) Math.floor(getCellCenter(y)); if (xPixel >= backgroundImage.getWidth()) { return DEFAULT_BACKGROUND_COLOR; } if (yPixel >= backgroundImage.getHeight()) { return DEFAULT_BACKGROUND_COLOR; } return backgroundImage.getColorAt(xPixel, yPixel); }