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

2021/5/18

Crop Circle

ronald ronald

2021/5/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Crop_Circle extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Crop_Circle()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1);
        getBackground().drawImage(getCrop_Circle(400), 500, 100);
        prepare();
    }

    private GreenfootImage getCrop_Circle(int size)
    {
        GreenfootImage image = new GreenfootImage(size, size);
        image.setColor(Color.BLACK);
        image.fillOval(0, 0, size, size);
        image.setColor(Color.WHITE);
        image.fillOval(0, size/4, size/2, size/2);
        image.setColor(Color.BLACK);
        image.fillOval(0, 20+size/3, size/4, size/4);        
        return image;
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Crop_Circle01 crop_Circle01 = new Crop_Circle01();
        addObject(crop_Circle01, 280, 300);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Crop_Circle01 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Crop_Circle01 extends Actor
{
    /**
     * Act - do whatever the Crop_Circle01 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        drawOval();
    }
    
    private void drawOval()
    {
        GreenfootImage image = new GreenfootImage(400, 400);
        image.setColor(Color.BLACK);
        image.fillOval(0, 0, 400, 400);
        image.setColor(Color.WHITE);
        image.fillOval(25, 25, 350, 350);
        image.setColor(Color.BLACK);
        image.fillOval(150, 150, 100, 100); 
        setImage(image);        
    }
}
Is there the same way to do as a world that is to say using Width and Height as a parameter instead of New Greenfootimage (400, 400); Do you need local variables?
danpost danpost

2021/5/18

#
I think you mean by using:
drawOval(400);
and
public void drawOval(int size)
and replacing values in the method with expressions using size. I would suggest that it not be done repeatedly, as drawing it once is sufficient. Remove the act method entirely and add a constructor to call the method:
public Crop_Circle01()
{
    drawOval(400);
}
ronald ronald

2021/5/18

#
thank you
ronald ronald

2021/5/18

#
 private GreenfootImage getCrop_Circle(int size)
    {
        GreenfootImage image = new GreenfootImage(size, size);
        image.setColor(Color.BLACK);
        image.fillOval(0, 0, size, size);
        image.setColor(Color.WHITE);
        image.fillOval(0, size/4, size/2, size/2);
        image.setColor(Color.BLACK);
        image.fillOval(0, 20+size/3, size/4, size/4);        
        return image;
    }
I try to create a new image by reducing it from half in World without repeating this code how to do ? thank you
ronald ronald

2021/5/18

#
its good I found
ronald ronald

2021/5/19

#
I have a question Can I use Move, Turn ... in World? If yes, how ?
danpost danpost

2021/5/19

#
ronald wrote...
I have a question Can I use Move, Turn ... in World? If yes, how ?
Actor actor = << some actor >>;
actor.move(10);
actor.turn(90);
ronald ronald

2021/5/19

#
danpost wrote...
Actor actor = << some actor >>;
????
 public void act()
    {        
        if(Greenfoot.mouseClicked(btn01))
        {
            Actor actor = new Actor();
            actor.move(10);
            actor.turn(90);
        }
        if(Greenfoot.mouseClicked(btn02)) ;
    }
ronald ronald

2021/5/19

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class YinYang02 extends World
{
    static final Color TRANS = new Color(0, 0, 0, 0);
    Actor btn01, btn02;
    Actor mouseOn;

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public YinYang02()
    {    
        super(900, 900, 1);
        getBackground().drawImage(getYinYang02(300), 300, 300);
        getBackground().drawImage(getYinYang02(150), 375, 100);
        getBackground().drawImage(getYinYang02(150), 575, 175);
        getBackground().drawImage(getYinYang02(150), 650, 375);
        getBackground().drawImage(getYinYang02(150), 575, 575);
        getBackground().drawImage(getYinYang02(150), 375, 650);
        getBackground().drawImage(getYinYang02(150), 175, 575);
        getBackground().drawImage(getYinYang02(150), 175, 175);
        getBackground().drawImage(getYinYang02(150), 100, 375);

        addObject(btn01 = getNewButton("LEFT"), 200, 800);
        addObject(btn02 = getNewButton("RIGHT"), 700, 800);
    }

    public void act()
    {        
        if(Greenfoot.mouseClicked(btn01))
        {
            Actor actor = new Actor();
            actor.move(10);
            actor.turn(90);
        }
        if(Greenfoot.mouseClicked(btn02)) ;
    }

    private GreenfootImage getYinYang02(int size)
    {
        GreenfootImage image = new GreenfootImage(size, size);
        image.setColor(Color.BLACK);
        image.fillOval(0, 0, size, size);
        image.setColor(Color.WHITE);
        image.fillRect(size/2, 0, size, size);
        image.setColor(Color.BLACK);
        image.fillOval(size/4, 0, size/2, size/2);
        image.setColor(Color.WHITE);
        image.fillOval(size/4, size/2, size/2, size/2);
        image.fillOval(7*size/16, 3*size/16, size/8, size/8);
        image.setColor(Color.BLACK);
        image.fillOval(7*size/16, 11*size/16, size/8, size/8);
        image.drawOval(0, 0, size-1, size-1);
        return image;       
    }

    private Actor getNewButton(String caption)
    {
        GreenfootImage base = new GreenfootImage(200, 30);
        base.fill();
        base.setColor(Color.BLACK);
        base.fillRect(3, 3, 194, 24);

        GreenfootImage text = new GreenfootImage(caption, 24, Color.WHITE, TRANS);
        base.drawImage(text, 100-text.getWidth()/2, 15-text.getHeight()/2);
        base.setTransparency(128);

        Actor button = new Actor()
            {
                public void act()
                {
                    if(mouseOn == null && Greenfoot.mouseMoved(this))
                    {
                        mouseOn = this;
                        getImage().setTransparency(255);
                    }

                    if(mouseOn == this && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
                    {
                        mouseOn = null;
                        getImage().setTransparency(128);
                    }
                }
            };
        button.setImage(base);
        return button;
    }
    
}
I give you another code Maybe you would understand better I have no actor
danpost danpost

2021/5/19

#
ronald wrote...
????
Actor actor = new Actor();
No. It should be an actor that is already in your world.
ronald ronald

2021/5/19

#
Actor button = new Actor()
            {
                public void act()
                {
                    if(mouseOn == null && Greenfoot.mouseMoved(this))
                    {
                        mouseOn = this;
                        getImage().setTransparency(255);
                    }

                    if(mouseOn == this && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
                    {
                        mouseOn = null;
                        getImage().setTransparency(128);
                    }
                    
                    if(Greenfoot.mouseClicked(btn01))
                    {
                        
                        move(10);
                        turn(90);
                    }
                    
                    if(Greenfoot.mouseClicked(btn02)) ;
                    
                }
            };
I do not know what I did if I understand The actor is already in GetNewbutton in the world I try to run the center of Yinyang and all the Yinyangs around the center by clicking
danpost danpost

2021/5/19

#
Lines 17 thru 24 should not be in that act method. You are showing code to an anonymous Actor class that should be in another class. It is the act method of the outer class that those lines should be in.
You need to login to post a reply.