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

2021/4/27

Clock

1
2
danpost danpost

2021/5/2

#
ronald wrote...
Why do not numbers display?
Okay ... I have an idea. How about creating a base image for the clock where then all you would need to do is copy the image and draw the hands each act to update the actor's image.
ronald ronald

2021/5/2

#
I will try your idea and returns to you
ronald ronald

2021/5/2

#
I guess you're talking about constructor clock and I have a little trouble with this sentence
danpost wrote...
draw the hands each act to update the actor's image.
danpost danpost

2021/5/3

#
ronald wrote...
I guess you're talking about constructor clock and I have a little trouble with this sentence
danpost wrote...
draw the hands each act to update the actor's image.
/**  with field  */
private GreenfootImage baseImage;

/**  in addedToWorld,, build clock image w/o hands and set to "baseImage" */

/**  in act (or paintComponent)  */
GreenfootImage gfim = new GreenfootImage(baseImage);
// draw hands on "gfim" (basic paintComponent codes)
setImage(gfim);
ronald ronald

2021/5/3

#
I'm having trouble with Drawhand's parameters I do not know where to put them by following your idea
danpost danpost

2021/5/3

#
ronald wrote...
I'm having trouble with Drawhand's parameters I do not know where to put them by following your idea
Here:
import greenfoot.*;
import java.time.LocalTime;

public class Clock extends Actor
{
    static final int BORDER = 5;
    
    final float degrees06 = (float)Math.PI/30f;
    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;
    
    int width = 5;
    GreenfootImage baseImage;
    
    public void addedToWorld(World world)
    {
        GreenfootImage gfim = new GreenfootImage(600, 600);
        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);
        setImage(gfim);
        
        Locator loc = new Locator();
        world.addObject(loc, 0, 0);
        for(int i=1; i<=12; i++)
        {
            loc.setLocation(getX(), getY());
            loc.setRotation(30*i-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);
        baseImage = getImage();
    }
    
    public void act()
    {
       GreenfootImage gfim = new GreenfootImage(baseImage);
        
        LocalTime time  = LocalTime.now();
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
        float minsecs = minute+second/60.0f;
        float hourmins = hour+minsecs/60f;
        
        float angle = degrees90-degrees30*hourmins;
        drawHand(gfim, width+2, angle, diameter/4+10, Color.BLACK);
        
        angle = degrees90-degrees06*minsecs;
        drawHand(gfim, width, angle, diameter/3+10, Color.BLACK);
        
        angle = degrees90-degrees06*second;
        drawHand(gfim, width-1, angle, diameter/2-30, Color.RED);
        
        setImage(gfim);
    }
    
    private void drawHand(GreenfootImage gfim, int width, float angle, int radius, Color color)
    {
        int x = cx+ (int)(Math.cos(angle)*radius);
        int y = cy - (int)(Math.sin(angle)*radius);
        int dx = (int)(Math.sin(angle)*width/2);
        int dy = (int)(Math.cos(angle)*width/2);
        gfim.setColor(color);
        int[] xs = { cx-dx, cx+dx, x+dx, x-dx };
        int[] ys = { cy-dy, cy+dy, y+dy, y-dy };
        gfim.fillPolygon(xs, ys, 4);
    }
    
    public class Locator extends Actor
    {
        public Locator()
        {
            setImage(new GreenfootImage(1, 1));
        }
    }
}
ronald ronald

2021/5/4

#
thank you
ronald ronald

2021/5/9

#
I want to create a circular bar around the clock I already have the code of the circular bar of the last time How to adapt the needles or clock with the circular bar?
danpost danpost

2021/5/10

#
ronald wrote...
How to adapt the needles or clock with the circular bar?
What do you mean by "adapt"? Details please.
ronald ronald

2021/5/10

#
I have just had an idea at the moment I mean the circular bar that follows the seconds of the clock If for example it is 13h30m29sec the circular bar should be 29 seconds knowing that the circular bar lasts 60 seconds when I press Run
danpost danpost

2021/5/10

#
Set rotation of circular bar actor at 180 and set its range from 0 to 60. Then adjust its value with the seconds.
ronald ronald

3 days ago

#
I have no idea of how to do Can you help me please ? thank you
danpost danpost

2 days ago

#
ronald wrote...
I have no idea of how to do Can you help me please ?
The circular bar will need to be painted first (to hide the textual value and to not have bar image interfere with the hands of clock) and be larger than the clock (to view the bar) The bar will need to be sized at about 150 percent using the setPercentSized method. The method's code will need adjusted to accommodate that size (allow to up to 200 percent). Keep a reference to bar in world. Add a method in world to receive a value to set bar to and adjust bar value. Have clock call this method when seconds is available. OR: Make hour, minute and second as int fields in clock, keep reference to both clock and bar in world and have world act method pass second value from clock to bar.
You need to login to post a reply.
1
2