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

2021/4/27

Clock

1
2
ronald ronald

2021/4/27

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

/**
 * Write a description of class Clock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Clock extends Actor
{
    final float degrees06 = (float) (Math.PI / 30);
    final float degrees30 = degrees06 * 5;
    final float degrees90 = degrees30 * 3;
 
    final int size = 590;
    final int spacing = 100;
    final int diameter = size - 2 * spacing;
    final int cx = diameter / 2 + spacing;
    final int cy = diameter / 2 + spacing;
    
    public Clock() 
    {
        
    }
    
    /**
     * Act - do whatever the Clock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        GreenfootImage gfim = new GreenfootImage(900,600);
        paintComponent(gfim);
        setImage(gfim);
    } 
    
    public void paintComponent(GreenfootImage gfim) {
       
        drawFace(gfim);
 
        final LocalTime time  = LocalTime.now();
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
 
        float angle = degrees90 - (degrees06 * second);
        drawHand(gfim, angle, diameter / 2 - 30, Color.RED);
 
        float minsecs = (minute + second / 60.0F);
        angle = degrees90 - (degrees06 * minsecs);
        drawHand(gfim, angle, diameter / 3 + 10, Color.BLACK);
 
        float hourmins = (hour + minsecs / 60.0F);
        angle = degrees90 - (degrees30 * hourmins);
        drawHand(gfim, angle, diameter / 4 + 10, Color.BLACK);
        
    }
 
    private void drawFace(GreenfootImage gfim) {
        //gfim.setStroke(new BasicStroke(2));
        gfim.setColor(Color.WHITE);
        gfim.fillOval(spacing, spacing, diameter, diameter);
        gfim.setColor(Color.BLACK);
        gfim.drawOval(spacing, spacing, diameter, diameter);
        //setLocation(600,300);
    }
 
    private void drawHand(GreenfootImage gfim, float angle, int radius, Color color) {
        int x = cx + (int) (radius * Math.cos(angle));
        int y = cy - (int) (radius * Math.sin(angle));
        gfim.setColor(color);
        gfim.drawLine(cx, cy, x, y);
        
    }
}
Hello I found a clock code I would like to increase the thickness of the oval and needles How to do with this code? Thank you for your help
RcCookie RcCookie

2021/4/28

#
For the oval:
// globally
static final int BORDER = 2; // set border here
// Replace drawFace content
gfim.setColor(Color.BLACK);
gfim.fillOval(spacing, spacing, diameter, diameter);
gfim.setColor(Color.WHITE);
gfim.fillOval(spacing + BORDER, spacing + BORDER, diameter - 2 * BORDER, diameter - 2 * BORDER);
For the needles:
// Add „int width“ parameter to drawHand method
// Replace drawHand content
int x = cx + (int) (radius * Math.cos(angle));
int y = cy - (int) (radius * Math.sin(angle));
int dx = (int)(0.5 * width * Math.sin(angle));
int dy = (int)(0.5 * width * Math.cos(angle));
gfim.setColor(color);
gfim.fillPolygon(new int[] {cx - dx, cx + dx, x + dx, x - dx}, new int[] {cy - dy, cy + dy, y + dy, y - dy}, 4);
Now add the desired width as parameter wherever drawHand is called.
ronald ronald

2021/4/28

#
thank you
ronald ronald

2021/4/29

#
 public void addedToWorld(World world)
    {
        Locator loc = new Locator();
        getWorld().addObject(loc, getX(), getY());
        
        for(int i=1; i<=12; i++)
        {
            loc.setLocation(getX(), getY());
            loc.setRotation(i*30-90);
            loc.move(150);
            GreenfootImage num = new GreenfootImage(""+i, 36, Color.GRAY, new Color(0, 0, 0, 0));
            loc.setLocation(loc.getX()-num.getWidth()/2, loc.getY()-num.getHeight()/2);
            int xOff = getX()-getImage().getWidth()/2;
            int yOff = getY()-getImage().getHeight()/2;
            getImage().drawImage(num, loc.getX()-xOff, loc.getY()-yOff);
        }
        getWorld().removeObject(loc);
    }
    
    public class Locator extends Actor
    {
        public Locator()
        {
            setImage(new GreenfootImage(1, 1));
        }
    }
I try to insert numbers into the clock for that I base myself on the scenario of Danpost I do not know if it's good code?
danpost danpost

2021/4/29

#
ronald wrote...
I try to insert numbers into the clock I do not know if it's good code?
Seems to work okay (provided clock image is large enough).
ronald ronald

2021/4/29

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

/**
 * Write a description of class Clock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Clock01 extends Actor
{
    final float degrees06 = (float) (Math.PI / 30);
    final float degrees30 = degrees06 * 5;
    final float degrees90 = degrees30 * 3;
 
    final int size = 590;
    final int spacing = 100;
    final int diameter = size - 2 * spacing;
    final int cx = diameter / 2 + spacing;
    final int cy = diameter / 2 + spacing;
    
    static final int BORDER = 5;
    int width = 5;
    
    public Clock01() 
    {
        
    }
    
    public void addedToWorld(World world)
    {
        Locator loc = new Locator();
        getWorld().addObject(loc, getX(), getY());
        
        for(int i=1; i<=12; i++)
        {
            loc.setLocation(getX(), getY());
            loc.setRotation(i*30-90);
            loc.move(150);
            GreenfootImage num = new GreenfootImage(""+i, 36, Color.GRAY, new Color(0, 0, 0, 0));
            loc.setLocation(loc.getX()-num.getWidth()/2, loc.getY()-num.getHeight()/2);
            int xOff = getX()-getImage().getWidth()/2;
            int yOff = getY()-getImage().getHeight()/2;
            getImage().drawImage(num, loc.getX()-xOff, loc.getY()-yOff);
        }
        getWorld().removeObject(loc);
    }
    
    /**
     * Act - do whatever the Clock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        GreenfootImage gfim = new GreenfootImage(900, 600);
        paintComponent(gfim);
        setImage(gfim);
    } 
    
    public void paintComponent(GreenfootImage gfim) 
    {   
        drawFace(gfim);
 
        final LocalTime time  = LocalTime.now();
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
 
        float angle = degrees90 - (degrees06 * second);
        drawHand(gfim, width, angle, diameter / 2 - 30, Color.RED);
 
        float minsecs = (minute + second / 60.0F);
        angle = degrees90 - (degrees06 * minsecs);
        drawHand(gfim, width, angle, diameter / 3 + 10, Color.BLACK);
 
        float hourmins = (hour + minsecs / 60.0F);
        angle = degrees90 - (degrees30 * hourmins);
        drawHand(gfim, width, angle, diameter / 4 + 10, Color.BLACK);
        
    }
 
    private void drawFace(GreenfootImage gfim) 
    {
        gfim.setColor(Color.BLACK);
        gfim.fillOval(spacing, spacing, diameter, diameter);
        gfim.setColor(Color.WHITE);
        gfim.fillOval(spacing + BORDER, spacing + BORDER, diameter - 2 * BORDER, diameter - 2 * BORDER);
        //gfim.setColor(Color.BLACK);
        //gfim.drawOval(spacing, spacing, diameter, diameter);
    }
 
    private void drawHand(GreenfootImage gfim, int width, float angle, int radius, Color color) 
    {
        int x = cx + (int) (radius * Math.cos(angle));
        int y = cy - (int) (radius * Math.sin(angle));
        int dx = (int) (0.5 * width * Math.sin(angle));
        int dy = (int) (0.5 * width * Math.cos(angle));
        gfim.setColor(color);
        //gfim.drawLine(cx, cy, x, y);
        gfim.fillPolygon(new int[] {cx - dx, cx + dx, x + dx, x - dx},
                         new int[] {cy - dy, cy + dy, y + dy, y - dy}, 4);
    }
    
    public class Locator extends Actor
    {
        public Locator()
        {
            setImage(new GreenfootImage(1, 1));
        }
    }
}
It does not seem to work What is the problem ?
danpost danpost

2021/4/30

#
Try this:
public void act()
{
    GreenfootImage gfim = new GreenfootImage(600, 600);
    paintComponent(gfim);
    setImage(gfim);
    addedToWorld(getWorld());
}
danpost danpost

2021/4/30

#
Actually, a better place for line 6 would be immediately after drawFace(gfim); in the paintComponent(GreenfootImage) method. Still, line 3 needs change as given.
ronald ronald

2021/4/30

#
If I understand correctly, you want me to place line 6 addedtoworld (getworld ()); In the Paincomponent Methode
ronald ronald

2021/4/30

#
OK I understood I put the line 6 addedtoworld (getworld ()); In the PaintComponent method just after DrawFace (GFIM); It does not give anything
danpost danpost

2021/4/30

#
ronald wrote...
OK I understood I put the line 6 addedtoworld (getworld ()); In the PaintComponent method just after DrawFace (GFIM); It does not give anything
Show your updated code.
ronald ronald

2021/4/30

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

/**
 * Write a description of class Clock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Clock01 extends Actor
{
    final float degrees06 = (float) (Math.PI / 30);
    final float degrees30 = degrees06 * 5;
    final float degrees90 = degrees30 * 3;
 
    final int size = 590;
    final int spacing = 100;
    final int diameter = size - 2 * spacing;
    final int cx = diameter / 2 + spacing;
    final int cy = diameter / 2 + spacing;
    
    static final int BORDER = 5;
    int width = 5;
    
    public Clock01() 
    {
        
    }
    
    /**
     * Act - do whatever the Clock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        GreenfootImage gfim = new GreenfootImage(600, 600);
        paintComponent(gfim);
        setImage(gfim);
        addedToWorld(getWorld());
    } 
    
    public void paintComponent(GreenfootImage gfim) 
    {   
        drawFace(gfim);
       
        final LocalTime time  = LocalTime.now();
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
 
        float angle = degrees90 - (degrees06 * second);
        drawHand(gfim, width, angle, diameter / 2 - 30, Color.RED);
 
        float minsecs = (minute + second / 60.0F);
        angle = degrees90 - (degrees06 * minsecs);
        drawHand(gfim, width, angle, diameter / 3 + 10, Color.BLACK);
 
        float hourmins = (hour + minsecs / 60.0F);
        angle = degrees90 - (degrees30 * hourmins);
        drawHand(gfim, width, angle, diameter / 4 + 10, Color.BLACK);
        
    }
 
    private void drawFace(GreenfootImage gfim) 
    {
        gfim.setColor(Color.BLACK);
        gfim.fillOval(spacing, spacing, diameter, diameter);
        gfim.setColor(Color.WHITE);
        gfim.fillOval(spacing + BORDER, spacing + BORDER, diameter - 2 * BORDER, diameter - 2 * BORDER);
        //gfim.setColor(Color.BLACK);
        //gfim.drawOval(spacing, spacing, diameter, diameter);
    }
 
    private void drawHand(GreenfootImage gfim, int width, float angle, int radius, Color color) 
    {
        int x = cx + (int) (radius * Math.cos(angle));
        int y = cy - (int) (radius * Math.sin(angle));
        int dx = (int) (0.5 * width * Math.sin(angle));
        int dy = (int) (0.5 * width * Math.cos(angle));
        gfim.setColor(color);
        //gfim.drawLine(cx, cy, x, y);
        gfim.fillPolygon(new int[] {cx - dx, cx + dx, x + dx, x - dx},
                         new int[] {cy - dy, cy + dy, y + dy, y - dy}, 4);
    }
    
    public void addedToWorld(World world)
    {
        Locator loc = new Locator();
        getWorld().addObject(loc, getX(), getY());
        
        for(int i=1; i<=12; i++)
        {
            loc.setLocation(getX(), getY());
            loc.setRotation(i*30-90);
            loc.move(150);
            GreenfootImage num = new GreenfootImage(""+i, 36, Color.GRAY, new Color(0, 0, 0, 0));
            loc.setLocation(loc.getX()-num.getWidth()/2, loc.getY()-num.getHeight()/2);
            int xOff = getX()-getImage().getWidth()/2;
            int yOff = getY()-getImage().getHeight()/2;
            getImage().drawImage(num, loc.getX()-xOff, loc.getY()-yOff);
        }
        getWorld().removeObject(loc);
    }
    
    public class Locator extends Actor
    {
        public Locator()
        {
            setImage(new GreenfootImage(1, 1));
        }
    }
}
Otherwise it works
danpost danpost

2021/4/30

#
Moving line 40 to line 46 will paint hands over the numbers.
ronald ronald

2021/4/30

#
ok I understand the needles pass on the numbers and not behind the numbers yes, I do not see the numbers anymore
ronald ronald

2021/5/2

#
Why do not numbers display?
There are more replies on the next page.
1
2