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

2020/10/26

hello world

ronald ronald

2020/10/26

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

/**
 * Write a description of class Test here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Test extends Actor
{
    /**
     * Act - do whatever the Test wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        test();
        // Add your action code here.
    }
    
    public void test()
    {
        GreenfootImage gfim = new GreenfootImage(900 ,600);
        gfim.setColor(Color.BLUE);
        gfim.setFont(new Font("OptimusPrinceps", true, true, 25));
        
        gfim.drawString("ENGLISH",200, 50);
        gfim.fillOval(400,25,25,25);
        gfim.drawOval(400,25,25,25);
            if (Greenfoot.mouseClicked(this))
            {               
                gfim.drawString("Hello, World", 450,50);
            }
        
        gfim.drawString("GERMAN",200, 100);
        gfim.fillOval(400,75,25,25);
        gfim.drawOval(400,75,25,25);
            if (Greenfoot.mouseClicked(this))
            {
                gfim.drawString("Hi, Welt", 450,100);
            }
        
        gfim.drawString("JAPANESE",200, 150);
        gfim.fillOval(400,125,25,25);
        gfim.drawOval(400,125,25,25);       
            if (Greenfoot.mouseClicked(this))
            {
                gfim.drawString("こんにちは、世界よ", 450,150);
            }
            
        setImage(gfim);
    }
    
}
I'm back all i try to do i choose the language by clicking on it on the oval and hello world is displayed but does not freeze Thank you for your help
danpost danpost

2020/10/26

#
If you want your ovals to respond separately, then either they should not be on the same actor; or you must determine where the click was on the Test actor; Main issue is your understanding on the keyword "this". It refers to a particular instance of the class. In this case, it is the Test actor that greenfoot has called act to run for. Any click on this actor will be a click on "this" regardless on its image particulars (other than size, which, in part, determines the hit area where a click is considered to be on this actor).
ronald ronald

2020/10/26

#
I have to create other actors without inheritance if I understand correctly for example if I create actor1, there is no API like setImage I admit sometimes I have trouble understanding
danpost danpost

2020/10/26

#
ronald wrote...
I have to create other actors without inheritance if I understand correctly for example if I create actor1, there is no API like setImage I admit sometimes I have trouble understanding
If you are to use actors, then they can be 3 different instances of the same class the extends directly from Actor. They can be created by the Text actor and the Test actor can check for clicks on them. A SimpleActor class can be used for this:
public class SimpleActor extends greenfoot.Actor {}
If you are wondering -- YES, that is the entire class code. Your Test class might then be as follows:
import greenfoot.*

public class Test extends Actor
{
    private Actor btnEnglish, btnGerman, btnJapanese;
    
    public Test()
    {
        GreenfootImage img = new GreenfootImage(25, 25);
        img.setColor(Color.BLUE);
        img.fillOval(0, 0, 25, 25);

        btnEnglish = new SimpleActor();
        btnEnglish.setImage(img);

        btnGerman = new SimpleActor();
        btnGerman.setImage(img);

        btnJapanese = new SimpleActor();
        btnJapanese.setImage(img);

        GreenfootImage gfim = new GreenfootImage(900 ,600);
        gfim.setColor(Color.BLUE);
        gfim.setFont(new Font("OptimusPrinceps", true, true, 25));
        gfim.drawString("ENGLISH",200, 50);
        gfim.drawString("GERMAN",200, 100);;
        gfim.drawString("JAPANESE",200, 150);
        setImage(gfim);
    }
    
    protected void addedToWorld(World world)
    {
        world.addObject(btnEnglish, 412, 37);
        world.addObject(btnGerman, 412, 87);
        world.addObject(btnJapanese, 412, 137);
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(btnEnglish)) getImage().drawString("Hello, World", 450, 50);
        if (Greenfoot.mouseClicked(btnGerman)) getImage().drawString("Hi, Welt", 450, 100);
        if (Greenfoot.mouseClicked(btnJapanese)) getImage().drawString("こんにちは、世界よ", 450, 150);
    }
}
ronald ronald

2020/10/27

#
thanks for the code I understand better by using a private access for the buttons which allows to differentiate them, I did not think about it thank you
You need to login to post a reply.