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

2020/4/23

Polyspiral

ronald ronald

2020/4/23

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

/**
 * Write a description of class PolySpiral here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PolySpiral extends Actor
{
    double inc = 0;
    
    public PolySpiral()
    {
        GreenfootImage gfim = new GreenfootImage(900,600);
        inc = (inc + 0.05) % 360;
        drawSpiral(gfim, 5, Math.toRadians(inc));
        setImage(gfim);
    }
    
    public void drawSpiral(GreenfootImage gfim, int length, double angleIncrement) 
    {
 
        double x1 = gfim.getWidth() / 2;
        double y1 = gfim.getHeight() / 2;
        double angle = angleIncrement;
 
        for (int i = 0; i < 150; i++) {
 
            gfim.setColor(Color.BLUE);//(Color.getHSBColor(i / 150f, 1.0f, 1.0f));
 
            double x2 = x1 + Math.cos(angle) * length;
            double y2 = y1 - Math.sin(angle) * length;
            gfim.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
            x1 = x2;
            y1 = y2;
 
            length += 3;
 
            angle = (angle + angleIncrement) % (Math.PI * 2);
        }
        
    }
}
what are my errors in this code? Thank you for your help
Super_Hippo Super_Hippo

2020/4/23

#
The method “PolySpiral” is the constructor of the class. It is only executed once when an object of the class is created. If you want that it happens once per act, you can use the act method.
ronald ronald

2020/4/23

#
superb thank you
You need to login to post a reply.