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

2021/5/31

VARIOUS

1
2
3
ronald ronald

2021/5/31

#
I'm looking to multiply an image when it touches another image thank you
Gbasire Gbasire

2021/5/31

#
you mean an actor ?
ronald ronald

2021/5/31

#
I am thinking I'm wondering It is the actor that it is necessary to multiply and not the image of the actor?
Gbasire Gbasire

2021/5/31

#
I think so
danpost danpost

2021/5/31

#
That is something you must be careful with. How long will the actor be touching the "image" (of the other actor? is what Gbasire was getting at -- I think) ?
ronald ronald

2021/6/1

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

/**
 * Write a description of class Ball05 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball05 extends Actor
{
    
    private int SIZE = 400;
    private int a = SIZE/2;
    private int b = a;
    private int diameter = 4 * SIZE / 5;
    private int n = 12;
    
    public void act() 
    {
        addBall05();
    }
    
    public void addBall05()
    {
        GreenfootImage img = new GreenfootImage(SIZE, SIZE);
        img.setColor(Color.BLACK);
       
        int m = Math.min(a, b);
        diameter = 4 * m / 5;
        int r2 = Math.abs(m - diameter) / 2;
        img.drawOval(a - diameter, b - diameter, 2 * diameter, 2 * diameter);
        
        img.setColor(Color.BLUE);
        for (int i = 0; i < n; i++)
        {
            double angle = 2 * Math.PI * i / n;
            int x = (int) Math.round(a + diameter * Math.cos(angle));
            int y = (int) Math.round(b + diameter * Math.sin(angle));
            
            img.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);
            
        }
        setImage(img);
    }
}
I found the math formula to place the rounds on the circle but not the blue balloon image I also enjoy to review maths thank you for correcting me so int m = 200; int diamed = 160; int r2 = 20; img.drawal (40,40,320,320); for n = 12; Double angle = 6.28 int x = 359; int y = 199; IMG FILLOVAL (339,179,80.80);
ronald ronald

2021/6/1

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

/**
 * Write a description of class Ball04 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball04 extends Actor
{
    private int size = 400;
    private int a = size/2;
    private int b = a;
    private int r = 4 * size / 5;
    private int n = 12;
    
    public void act() 
    {
        drawImg();   
    }
    
    public void drawImg()
    {
        GreenfootImage img = new GreenfootImage(size, size);
        img.setColor(Color.BLACK);
       
        int m = Math.min(a, b);
        r   = 4 * m / 5;
        int r2 = Math.abs(m - r) / 2;
        img.drawOval(a - r, b - r, 2 * r, 2 * r);
        
        //img.setColor(Color.BLUE);
        for (int i = 0; i < n; i++)
        {
            double t = 2 * Math.PI * i / n;
            int x = (int) Math.round(a + r * Math.cos(t));
            int y = (int) Math.round(b + r * Math.sin(t));
            getWorld().addObject(new Ball04(), x+100, y+100);
            //img.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);           
        }
        setImage(img);
    }
}
java.lang.OutOfMemoryError: Java heap space at java.desktop/java.awt.image.DataBufferInt.<init>(DataBufferInt.java:76) at java.desktop/java.awt.image.Raster.createPackedRaster(Raster.java:467) at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at java.desktop/java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:186) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:130) at Ball04.drawImg(Ball04.java:24) at Ball04.act(Ball04.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space at greenfoot.vmcomm.VMCommsSimulation.doInterVMComms(VMCommsSimulation.java:279) at greenfoot.vmcomm.VMCommsSimulation$1.run(VMCommsSimulation.java:178) with part of the code I put the image with Getworld () I have a message of memory error be said You told me to pay attention, is this error you wanted to talk to me ??? my image is not big but has several images l error comes from the method and addition to the actor How can I correct this error? thank you
danpost danpost

2021/6/2

#
The problem stems from line 38. Almost never should you add a new instance of a class into the world from the class of that instance.
ronald ronald

2021/6/2

#
If I understood correctly I do not add a class instance in the world If I have already added an object with Getworld in the actor and even No Getworld in the actor if I have already added the object in the world I can not have both
danpost danpost

2021/6/2

#
ronald wrote...
If I understood correctly I do not add a class instance in the world If I have already added an object with Getworld in the actor and even No Getworld in the actor if I have already added the object in the world I can not have both
I do not think you understood. I was just saying that "new Ball04()" should never appear in the Ball04 class.
ronald ronald

2021/6/2

#
sorry I understand as I can After the Greenfoot API, add the actor object, int x, int y I do not understand anything anymore
danpost danpost

2021/6/2

#
The API gives you the method formats and descriptions. The non-static methods given can only be executed on an instance of that class's type (an instance specifically of that class or of any of its subclasses. The World instance method shown as: addObject(Actor object, int x, int y) gives a usage of someWorldInstance.addObject(someActorInstance, someIntValue, anotherIntValue); In a World subclass named MyWorld, possible usages are:
// in constructor
public MyWorld()
{
    super(600, 400, 1);
    addObject(new Ball(), 50, 50); // "this." is implicitly understood
}

/* ********************** */
// in a method called by constructor
public MyWorld()
{
    super(600, 400, 1);
    prepare(); // "this." is implicitly understood
}

private void prepare()
{
    Ball ball = new Ball();
    addObject(ball, 0, 0); // "this." is implicitly understood
    ball.setLocation(50, 50);
}

/* *********************** */
// in act method
if (this.getObjects(Ball.class).isEmpty()) this.addObject(new Ball(), 50, 50);
The world should have full control of what happens in general. That is, initial set up and major transition points are to be coded in your World subclass. Code in Actor subclasses are for initializing actors and giving them their specific states (fields) and behaviors (methods).
ronald ronald

2021/6/3

#
okay I add getObject in the ball01 actor and creates a new class ball03 it works but shows that a ball instead of 12 balls on the circle In my opinion, I have to review the calculation formula
danpost danpost

2021/6/3

#
ronald wrote...
okay I add getObject in the ball01 actor and creates a new class ball03 it works but shows that a ball instead of 12 balls on the circle In my opinion, I have to review the calculation formula
Show codes.
ronald ronald

2021/6/3

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

/**
 * Write a description of class Ball04 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball04 extends Actor
{
    private static final int SIZE = 400;
    private int a = SIZE/2;
    private int b = a;
    private int r = 4 * SIZE / 5;
    private int n = 12;
    
    public void act() 
    {
        paintComponent();       
    }
    
    public void paintComponent()
    {
        GreenfootImage img = new GreenfootImage(SIZE, SIZE);
        img.setColor(Color.BLACK);
       
        int m = Math.min(a, b);
        r   = 4 * m / 5;
        int r2 = Math.abs(m - r) / 2;
        img.drawOval(a - r, b - r, 2 * r, 2 * r);
        
        //img.setColor(Color.BLUE);
        for (int i = 0; i < n; i++)
        {
            double t = 2 * Math.PI * i / n;
            int x = (int) Math.round(a + r * Math.cos(t));
            int y = (int) Math.round(b + r * Math.sin(t));
            if(getWorld().getObjects(Ball06.class).isEmpty())
            {
                getWorld().addObject(new Ball06(), x+100, y+100);                
            }
            //img.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);           
        }        
        setImage(img);
    }    
}
here is the code
There are more replies on the next page.
1
2
3