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

2023/8/21

length - width height

1
2
ronald ronald

2023/8/21

#
Good morning I would like to have is it possible to adjust the width according to the length of the graphical interface? for example for a length of 100, the width 300 and 200 for the height and for a length of 300, it goes to 900 for the width and 600 for the height let's say width and height change with length thank you for your understanding
danpost danpost

2023/8/22

#
ronald wrote...
is it possible to adjust the width according to the length of the graphical interface? for example for a length of 100, the width 300 and 200 for the height and for a length of 300, it goes to 900 for the width and 600 for the height let's say width and height change with length
So, you want a world whose variable dimensions (width/height) are in a 3:2 ratio in units of 100. Each World object (instance of the World class) is permanently fixed in its dimensions. Therefore, there is a restriction on where it is feasible to perform this. For instantiating World objects of some size that is not changed during gameplay, it is not hard to do. Ask about "instantiating various sized worlds" for on how. Ask about "changing world size" (during gameplay) for what is involved there.
ronald ronald

2023/8/22

#
OK we have to change the world I tried with the getter and setter method in actor I did not succeed it didn't fit with pendulum length as constructor argument in actor I tell myself that it is not likely to open if I put 100 as the length in the constructor with argument when I click and not 100 but length in the creation of the object with new
danpost danpost

2023/8/22

#
ronald wrote...
we have to change the world I tried with the getter and setter method in actor I did not succeed it didn't fit with pendulum length as constructor argument in actor I tell myself that it is not likely to open if I put 100 as the length in the constructor with argument when I click and not 100 but length in the creation of the object with new
I think in your case that each pendulum is a single gameplay. So, unless a specific pendulum can change its length, what you really want is instantiating various sized worlds. Start with something like the following:
import greenfoot.*;

public class MyWorld extends World
{
    // initial constructor; giving default size
    public MyWorld() {
        this(100);
    }
    
    // secondary constructor; allowing various sizes
    public MyWorld(int halfHeight) {
        super(halfHeight*3, halfHeight*2, 1);
        // etc. (prepare world)
    }
    
    public void act() {
        String key = Greenfoot.getKey();
        if (key != null) {
            // actions to change world size
            int change = 0;
            if ("up".equals(key)) change = 1;
            if ("down".equals(key)) change = -1;
            if (change != 0) {
                int halfHeight = getHeight()/2+change;
                if (halfHeight>= 100 && halfHeight<= 300) {
                    Greenfoot.setWorld(new MyWorld(halfHeight));
                }
            }
            // other key related actions (preferably here)
        }
    }
    // other actions
}
There are other ways to go about changing the world size. For example, you could use Greenfoot.ask() to get a half height (or actual dimension, horizontal or vertical), then check the input for (1) is it not null; (2) is it a valid integer input; (3) is it a valid dimension or half height; before setting a new sized world. You would still need a key input or mouse action to trigger the ask.
ronald ronald

2023/8/22

#
thanks again
ronald ronald

2023/8/25

#
hello danpost I just tried your code, it's super funny. I realize that I expressed myself badly or maybe not. what i meant is that the pendulum adapts to the height and width of the frame if i understand this pendulum code correctly in rosettacode normally the balls also enlarge depending on the length so I redid a getter and setter, but it's not what I expected the balls do not expand with length and neither does the frame. or is it me who doesn't understand i hope i am not mistaken
danpost danpost

2023/8/25

#
ronald wrote...
what i meant is that the pendulum adapts to the height and width of the frame if i understand this pendulum code correctly in rosettacode normally the balls also enlarge depending on the length so I redid a getter and setter, but it's not what I expected the balls do not expand with length and neither does the frame. or is it me who doesn't understand i hope i am not mistaken
I think all you need to do is use some protected void addedToWorld(World) methods, so that you can make use of the dimensions of the world, to size the actors.
danpost danpost

2023/8/25

#
ronald wrote...
hello danpost I just tried your code, it's super funny.
How (or why) was it funny ???
ronald ronald

2023/8/25

#
I find that funny because when I click on up or down the frame inside the large window changes shape but the pendulum does not change its place
ronald ronald

2023/8/25

#
I misunderstood the code the pendulum does not change size according to the length but I will be able to make the pendulum change shape depending on the length otherwise thank you again
ronald ronald

2023/8/25

#
sorry it's me again that's it, I adjusted the width, I did as your code and the pendulum doesn't move, it stays in place
ronald ronald

2023/8/25

#
I just came across one of your scenarios world scaling demo Obviously, now I ask myself questions do you have to modify the length or the background according to the change
int change = 0;
            if ("up".equals(key))   change = 1;
            if ("down".equals(key)) change = -1;
            if (change != 0) {
                int halfWidth = getWidth() / 2 + change;
                int halfHeight = getHeight() / 2 + change;
                if (halfWidth >= 100 && halfWidth <= 300) {
                    if (halfHeight >= 100 && halfHeight <= 300) {
                        Greenfoot.setWorld(new PendulumWorld4(halfWidth, halfHeight));
                    }
                }
                // other key related actions (preferably here)
                GreenfootImage img = new GreenfootImage(pendulum_4.getImage());
                img.scale(length + change, length + change);
                setBackground(img);
            }
danpost danpost

2023/8/27

#
ronald wrote...
do you have to modify the length or the background according to the change
That depends on your background image -- how it is being set, what it is being set to, etc.
ronald wrote...
the pendulum doesn't move, it stays in place
That is most probably a problem in the class of the pendulum object.
ronald ronald

2023/8/27

#
what i mean when i clicked up or down, the pendulum was moving left or right, I don't remember but now it doesn't move anymore, it stays centered otherwise for the background I only have your code in world and a pendulum actor class that I found in rosetta code a question ? do you think of expanding or shrinking the moving pendulum when I click on up or down?
ronald ronald

2023/8/27

#
import greenfoot.*;

/**
 * Pendulum
 * 
 * @author (Ronald) 
 * @version (Pendulum)
 */

public class Pendulum_4 extends Actor {
    
    private double angle = Math.PI / 2;
    private int length;
    double angleAccel = 0;
    double angleVelocity = 0;
    double dt = 0.2;
    
    public Pendulum_4(int length) {
        
        this.length = length;
    }
    
    public void act() {
        
        paint();
        run();
    }
    
    public void paint() {
        
        GreenfootImage gfim = new GreenfootImage(2 * length + 50, 2 * length + 50);
        gfim.setColor(Color.WHITE);
        gfim.fillRect(0, 0, gfim.getWidth(), gfim.getHeight());
        gfim.setColor(Color.BLUE);
        gfim.fillRect((int) (2 * length + 50) / 4, (int) ((2 * length + 50) / 4) / 2, gfim.getWidth() / 2, gfim.getHeight() / 10);
        gfim.setColor(Color.GREEN);
        gfim.fillRect((int) (2 * length + 50) / 4, 2 * length - (2 * length) / 8, gfim.getWidth() / 2, gfim.getHeight() / 10);
        gfim.setColor(Color.BLACK);
        int anchorX = gfim.getWidth() / 2;
        int anchorY = gfim.getHeight() / 4;
        int ballX = anchorX + (int) (Math.sin(angle) * length);
        int ballY = anchorY + (int) (Math.cos(angle) * length); 
        gfim.drawLine(anchorX, anchorY, ballX, ballY);
        gfim.fillOval(anchorX - (((length / 5) - 5) / 2) - 1, anchorY - (((length / 5) - 5) / 2) - 1, (length / 5) - 5, (length / 5) - 5);
        gfim.fillOval(ballX - ((length / 5) - 5), ballY - ((length / 5) - 5), 2 * ((length / 5) - 5), 2 * ((length / 5) - 5));
        this.setImage(gfim);
    }
        
    public void run() {    
        
       angleAccel = -9.81 / length * Math.sin(angle);
       angleVelocity += angleAccel * dt;
       angle += angleVelocity * dt;
    }
}
There are more replies on the next page.
1
2