This site requires JavaScript, please enable it in your browser!
Greenfoot back
tudibk
tudibk wrote ...

2020/2/25

Circle image becoming square when scaling

tudibk tudibk

2020/2/25

#
My circle is becoming a square when scaling the image. I dont know why please help me :) Here is the code of my main actor: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.Random; /** * Write a description of class Blob here. * * @author (your name) * @version (a version number or a date) */ public class Blob extends Actor { /** * Act - do whatever the Blob wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ int x; int y; int i; int size = 10; int blobIndex = 0; int maxBlobs = 20; BlobFood blobFood = new BlobFood; Color initial = new Color(Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255)); GreenfootImage img = new GreenfootImage(size, size); public int getBlobIndex(){ return blobIndex; } public int getMaxBlobs(){ return maxBlobs; } public void addBlob(){ if(maxBlobs > blobIndex){ blobFood = new BlobFood(); getWorld().addObject(blobFood,Greenfoot.getRandomNumber(255),Greenfoot.getRandomNumber(255)); blobFood.setImage(blobFood.setShape()); blobIndex += 1; } } public GreenfootImage setShape(){ img.setColor(initial); img.fillOval(0, 0,size,size); return img; } public void eat(){ if(getObjectsInRange(size, BlobFood.class).size() > 0) { getWorld().removeObjects(getObjectsInRange(size, BlobFood.class)); size += 2; getImage().scale(size, size); } } public void movement(){ int x = getX(); int y = getY(); if (Greenfoot.isKeyDown("left")){ x -= 1; setLocation(x,y); } if (Greenfoot.isKeyDown("right")){ x += 1; setLocation(x,y); } if (Greenfoot.isKeyDown("up")){ y -= 1; setLocation(x,y); } if (Greenfoot.isKeyDown("down")){ y += 1; setLocation(x,y); } } public void act() { movement(); eat(); } }
danpost danpost

2020/2/25

#
That is due to re-scaling the image. Create and scale an original image instead. You can keep an original in a field to reduce file directory calls:
private GreenfootImage baseImage;
use a constructor block to assign the image to the field:
public Blob()
{
    baseImage = getImage();
}
and make a copy of the base image to scale when scaling:
GreenfootImage image = new GreenfootImage(baseImage);
image.scale(size, size);
setImage(image);
tudibk tudibk

2020/2/25

#
It worked, thanks a lot! I dont really understand why it worked, but it worked ;)
danpost danpost

2020/2/25

#
tudibk wrote...
I dont really understand why it worked, but it worked ;)
It worked because it does not scale an image that has already been modified by scaling. Scaling slightly distorts an image, usually not noticeably. However, multiple scaling of the same image will compound that distortion and make it quite noticeable.
You need to login to post a reply.