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

2021/5/13

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

2021/5/13

#
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.
ronald ronald

2021/5/17

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

/**
 * Write a description of class CircularBar03 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CircularBar03 extends Actor
{
    static final Color TRANS = new Color(0, 0, 0, 0);
    int seconds = 0;
    
    private static GreenfootImage barImage;
    static
    {
        barImage = new GreenfootImage(150, 300);
        barImage.setColor(Color.GREEN);
        barImage.fillOval(0, 0, 300, 300);
        barImage.setColor(Color.WHITE);
        barImage.fillOval(20, 20, 260, 260);
        
        GreenfootImage image = new GreenfootImage(300, 300);
        image.drawImage(barImage, 0, 0);
         
        int x = 20;
        Color trans = TRANS;
        for (int y=149; y>=20; y--)
        {
            while (!Color.WHITE.equals(image.getColorAt(x++, y)) && x<150);
            for (int n=(--x); n<=300-x; n++) image.setColorAt(n, y, trans);
        }
         
        barImage.clear();
        barImage.drawImage(image, 0, 0);
    }
     
    private int percentSized = 100;
    //private String caption;
    private int value;
    private int maximumValue;
    
    public CircularBar03(int initValue, int maxValue)
    {
        //caption = text;
        value = initValue;
        maximumValue = maxValue;
        updateImage();
    }
    
    public void act()
    {
        setValue(seconds);
    }
    
    public void add(int amount)
    {
        setValue(value+amount);
    }
    
    public int getValue()
    {
        return value;
    }
    
    public void setValue(int val)
    {
        if (val > maximumValue) val = maximumValue;
        if (val < 0) val = 0;
        value = val;
        updateImage();
    }
    
    public int getMaximumValue()
    {
        return maximumValue;
    }
    
    public void setPercentSized(int pct)
    {
        if (pct < 20) pct = 20;
        if (pct > 100) pct = 100;
        percentSized = pct;
        updateImage();
    }
    
    private void updateImage()
    {
        GreenfootImage bar = new GreenfootImage(300, 300);
        GreenfootImage half = new GreenfootImage(150, 300);
        GreenfootImage rotater = new GreenfootImage(300, 300);
        rotater.drawImage(barImage, 0, 0);
        int pct = (value*100)/maximumValue;
        rotater.rotate(180+360*pct/100);
        
        if (pct > 50)
        {
            bar.drawImage(barImage, 0, 0);
            half.drawImage(rotater, -150, 0);
            bar.drawImage(half, 150, 0);
        }
        else
        {
            half.drawImage(rotater, 0, 0);
            bar.drawImage(half, 0, 0);
        }
        
        //GreenfootImage text = new GreenfootImage(""+value+caption, 96, Color.BLACK, TRANS);
        //bar.drawImage(text, 150-text.getWidth()/2, 150-text.getHeight()/2);
        bar.scale(300*percentSized/100, 300*percentSized/100);
        
        setImage(bar);
    }
}  
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Calendar;

/**
 * Write a description of class DigitalClock03 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DigitalClock03 extends Actor
{
    /**
     * Act - do whatever the DigitalClock03 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private String hours;
    private String minutes;
    private String seconds;
    
    public void addedToWorld(World world)
    {
        act();
    }
    
    public void act() 
    {
        updateTime();
        draw();
    }   
    
    private void updateTime()
    {
        Calendar calendar = Calendar.getInstance();
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        this.hours = getStr(hours);
        int minutes = calendar.get(Calendar.MINUTE);
        this.minutes = getStr(minutes);
        int seconds = calendar.get(Calendar.SECOND);
        this.seconds = getStr(seconds);
    }
    
    private String getStr(int val)
    {
        String str = null;
        if(val<10)
        {
            str = "0"+val;
        }
        else
        {
            str = ""+val;
        }
        return str;
    }
    
    private void draw()
    {
        GreenfootImage image = new GreenfootImage(400, 300);
        Font font = image.getFont();
        font = font.deriveFont(new Float(60));
        image.setFont(font);
        image.setColor(Color.BLACK);
        image.drawString(hours + " : " + minutes + " : " + seconds, 25 ,100);
        setImage(image);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Seconds here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Seconds extends Actor
{
    /**
     * Act - do whatever the Seconds wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private static int seconds = 0;
    
    public void act() 
    {
        // Add your action code here.
    }
    
    public static void setSeconds(int sec)
    {
        if (sec>=0 && sec<=60)
        {
            seconds = sec;
        }
    }
    
    public static void adjustSeconds(int adjustment)
    {
        if (seconds+adjustment >= 0 && seconds+adjustment <= 60)
        {
            seconds += adjustment;
        }
    }
    
    public static int getSeconds()
    {
        return seconds;
    }
}
I changed code Can you tell me what's wrong? thank you
ronald ronald

2021/5/17

#
public void act()
    {
        Calendar calendar = Calendar.getInstance();
        setValue(calendar.get(Calendar.SECOND));
    }
I just insert this code into the circular bar I'm remaining there is no way to do
You need to login to post a reply.
1
2