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

2022/6/4

TEST JAVA

ronald ronald

2022/6/4

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

/**
 * Write a description of class Button here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button extends Actor
{
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    static final Color TRANS = new Color(0,0,0,0);
    
    Actor btn01,btn02;
    Actor mouseOn;
    
    public void act() {
        if (Greenfoot.mouseClicked(btn01)) {
            HelloWorld hello = (HelloWorld) getOneIntersectingObject(HelloWorld.class);
            hello.printHello();
        }
        
        if (Greenfoot.mouseClicked(btn02)) {
            ConstructorNoArg noArg = (ConstructorNoArg) getOneIntersectingObject(ConstructorNoArg.class);
            noArg.printConstructorNoArg();
        }
    }
    
    protected void addedToWorld(World world) {
        world.addObject(btn01 = getNewButton("BUTTON 01"),1050,50);
        world.addObject(btn01 = getNewButton("BUTTON 02"),1050,100);
    }
    
    protected Actor getNewButton(String caption) {
        GreenfootImage img = new GreenfootImage(200,30);
        img.fill();
        img.setColor(Color.BLUE);
        img.fillRect(3,3,194,24);
        
        GreenfootImage text = new GreenfootImage(caption,20,Color.WHITE,TRANS);
        img.drawImage(text,100-text.getWidth()/2,15-text.getHeight()/2);
        img.setTransparency(128);
        
        Actor button = new SimpleActor() {
                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(img);
        return button;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HelloWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HelloWorld extends Actor
{
    /**
     * Act - do whatever the HelloWorld wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        printHello();
    }
    
    public void printHello() {
        GreenfootImage hello = new GreenfootImage(900, 600);
        hello.setColor(Color.BLUE);
        hello.setFont(new Font("calibri", true, true, 50));
        hello.drawString("public class Hello {", 50, 100);
        hello.drawString("    public static void main(String[] args) {", 50, 140);
        hello.drawString("        System.out.println(\"Hello, World!\");", 50, 180);
        hello.drawString("    }", 50, 220);
        hello.drawString("}", 50, 260);
        setImage(hello);
    }
}
hello I am trying to display text with a button I still don't understand this nullPointerException error on line 23 and 28 Thank you for your help
Spock47 Spock47

2022/6/4

#
ronald wrote...
            HelloWorld hello = (HelloWorld) getOneIntersectingObject(HelloWorld.class);
            hello.printHello();
getOneIntersectingObject returns an object of the given type that is in the world at the same position. But if there is no object of that type at the same position, it will return null. In other words: it is to find already existing objects that are colliding with the current object, Here, it looks like you instead actually want to use a new object that needs to be created in this place:
final HelloWorld hello = new HelloWorld();
getWorld().addObject(hello, x, y); // replace x,y with adequate position
hello.printHello();
danpost danpost

2022/6/4

#
ronald wrote...
<< Codes Omitted >> I still don't understand this nullPointerException error on line 23 and 28
As Spock47 already pointed out, if the button is not already touching, intersecting, the object, whether of type HelloWorld or a ContructorNoArg, then hello or noArg will be set to null. Lines 23 and 28 try to access methods in their classes. But if there is no object (variable is null), then the method cannot be called. It is true that you can create a new object of the particular type and call the method on it, but there is a more immediate problem with the Button class. First off, you have given it a multiple personality complex by having a button try to decide what it is supposed to do. Secondly, you have given it an ego by giving it fields so that one can hold itself. Also, you have to create a button for other buttons, the real ones, to be created. The thing is, you do not need a Button class at all -- you have a SimpleActor class which can create the actors that will be the buttons. Also, you have a getNewButton(String) method, which not only creates those SimpleActor objects, but has an anonymous class encoded in it for the buttons, giving them the ability to detect the mouse hovering over it. That IS the class for your buttons (lines 47 thru 59 of your Button class above). The buttons should be added into the world from your world constructor and the fields (lines 17 and 18) and act method codes should also be in the class of your world. The getNewButton(String) method should also be moved to the class of your world. In essence, everything in your Button class above should be in the class of your world and the Button class should be removed (deleted entirely).
ronald ronald

2022/6/4

#
public void act() {
        if (Greenfoot.mouseClicked(btn01) || getObjects(HelloWorld.class).isEmpty()) {
            ?????????            
        }
    }
Hello again so I deleted the button class in actor and everything moved to the world class I don't know if it's the same thing, I created private actor button as a field instead of putting everything actor button from the getnewbutton method in the constructor now i am trying to figure out how to click button to print helloworld class with getObjects I don't know if it's possible, it's true that it complicates a bit with several buttons for the methods or constructors of the actor classes thank you
danpost danpost

2022/6/4

#
ronald wrote...
now i am trying to figure out how to click button to print helloworld class with getObjects
You do not need to use the getObjects method. All you need are the following parts:
// fields for actors
private Actor btn01, btn02, mouseOn;
private Actor hello, noArg;

// constructor code to add create and add actors into world
btn01 = getNewButton("BUTTON 01");
btn02 = getNewButton("BUTTON 02");
hello = new HelloWorld();
noArg = new ConstructorNoArg();
addObject(btn01, 1050, 50);
addObject(btn02, 1050, 100);
addObject(hello, ???, ??); // wherever
addObject(noArg, ???, ??); // wherever

// act code to detect clicks on buttons
if (Greenfoot.mouseClicked(btn01)) hello.printHello();
if (Greenfoot.mouseClicked(btn02)) noArg.printConstructorNoArg();
and the getNewButton(String) method. Remove the act methods from both the HelloWorld and ConstructorNoArg classes. Add constructors to both those classes containing the following code:
public /** class name here */()
{
    setImage((GreenfootImage) null);
}
This will get rid of the little greenfoot icon image that is initially given the actors.
ronald ronald

2022/6/5

#
thank you danpsot a small problem with the hello.printHello() method is undeclared and the same for noarg.constructornoarg();
ronald ronald

2022/6/5

#
how to declare the prinHello method I put the object of the class in the act method that is line 8 and 10 of your code in act world It works well
danpost danpost

2022/6/5

#
ronald wrote...
how to declare the prinHello method
I do not understand why you get an undeclared error as you have shown the printHello the method is in your HelloWorld class and I presume you have a printConstructorNoArg method in your ConstructorNoArg class.
I put the object of the class in the act method that is line 8 and 10 of your code in act world It works well
Lines 8 and 10 ??? My code ??? Those two lines don't even do the same thing. I am not seeing what you are trying to say here. Show lines of interest.
ronald ronald

2022/6/5

#
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 MyWorld extends World
{
    static final Color TRANS = new Color(0,0,0,0);
    
    private Actor button;
    
    private Actor btn01,btn02, btn03, btn04;
    private Actor mouseOn;
       
    private Actor helloWorld, constructorNoArg, constructorArg, constructorArgThis;
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld() {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 600, 1);
        
        GreenfootImage bg = getBackground();
        bg.setColor(Color.LIGHT_GRAY);
        bg.fillRect(0, 0, getWidth(), getHeight());
        bg.setColor(Color.CYAN);
        bg.fillRect(0, 0, getWidth()-300, getHeight());
        
        button = new SimpleActor();
        
        btn01 = getNewButton("LESSON 01");
        btn02 = getNewButton("LESSON 02");
        btn03 = getNewButton("LESSON 03");
        btn04 = getNewButton("LESSON 04");
        
        addObject(btn01, 1050, 50);
        addObject(btn02, 1050, 100);
        addObject(btn03, 1050, 150);
        addObject(btn04, 1050, 200);
        
        prepare();
    }
    
    public void act() {
        HelloWorld helloWorld = new HelloWorld();
        addObject(helloWorld, 425, 500);
        if (Greenfoot.mouseClicked(btn01))  helloWorld.printHello();
        
        ConstructorNoArg constructorNoArg = new ConstructorNoArg();
        addObject(constructorNoArg, 425, 500);
        if (Greenfoot.mouseClicked(btn02)) constructorNoArg.printConstructorNoArg();
        
        ConstructorArg constructorArg = new ConstructorArg("ronald", 50);
        addObject(constructorArg, 425, 500);
        if (Greenfoot.mouseClicked(btn03)) constructorArg.printConstructorArg();
        
        ConstructorArgThis constructorArgThis = new ConstructorArgThis("donald", 25);
        addObject(constructorArgThis, 425, 500);
        if (Greenfoot.mouseClicked(btn04)) constructorArgThis.printConstructorArgThis();
    }
    
    public void prepare() {
        /*System.out.println("Lesson01 : " + "Hello, World!");
        
        ConstructorNoArg constructorNoArg = new ConstructorNoArg();
        constructorNoArg.printConstructorNoArg();
        System.out.println("Lesson02 : " + constructorNoArg.name);
        
        ConstructorArg constructorArg = new ConstructorArg("ronald", 50);
        constructorArg.printConstructorArg();
        System.out.println("Lesson03 : " + constructorArg.nameOne + " " + constructorArg.ageOne);
        
        ConstructorArgThis constructorArgThis = new ConstructorArgThis("donald", 25);
        constructorArgThis.printConstructorArgThis();
        System.out.println("Lesson04 : " + constructorArgThis.name + " " + constructorArgThis.age);
        */
        
        Board board = new Board();
        addObject(board, 1050, 500);
    }
    
    protected Actor getNewButton(String caption) {
        GreenfootImage img = new GreenfootImage(200,30);
        img.fill();
        img.setColor(Color.BLUE);
        img.fillRect(3,3,194,24);
        
        GreenfootImage text = new GreenfootImage(caption,20,Color.WHITE,TRANS);
        img.drawImage(text,100-text.getWidth()/2,15-text.getHeight()/2);
        img.setTransparency(128);
        
        Actor button = new SimpleActor() {
                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(img);
        return button;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HelloWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HelloWorld extends Actor
{
    /**
     * Act - do whatever the HelloWorld wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public HelloWorld() {
        setImage((GreenfootImage) null);
    }
    
    public void printHello() {
        GreenfootImage hello = new GreenfootImage(900, 600);
        hello.setColor(Color.BLUE);
        hello.setFont(new Font("calibri", true, true, 50));
        hello.drawString("public class Hello {", 50, 100);
        hello.drawString("    public static void main(String[] args) {", 50, 140);
        hello.drawString("        System.out.println(\"Hello, World!\");", 50, 180);
        hello.drawString("    }", 50, 220);
        hello.drawString("}", 50, 260);
        setImage(hello);
    }
}
well i don't understand either I show you the class world and helloworld I did that and it works
danpost danpost

2022/6/5

#
ronald wrote...
<< Codes Omitted >> well i don't understand either I show you the class world and helloworld I did that and it works
Remove lines 13 and 34 from your MyWorld class and move lines 50 and 51 into the constructor or into the prepare method (remove the first word from line 50).
ronald ronald

2022/6/5

#
well i don't know how to tell you I fired or commented to test I have the same problem undeclared method print at line 52 in act mouseclickcked btn01
Spock47 Spock47

2022/6/5

#
The attribute helloWorld is of type Actor, but Actor does not have a printHello method. So the attribute has to be defined as type HelloWorld:
private Actor helloWorld, constructorNoArg, constructorArg, constructorArgThis;
->
private HelloWorld helloWorld;
private ConstructorNoArg constructorNoArg;
...
Note: If you move line 50 into the constructor, make sure to use the attribute, not an additional local variable:
helloWorld = new HelloWorld(); // CORRECT: This uses the attribute.
HelloWorld helloWorld = new HelloWorld(); // INCORRECT: This creates a new local variable and ignores the attribute.
ronald ronald

2022/6/5

#
thanks to you two
You need to login to post a reply.