ronald wrote...
Why do not numbers display?/** 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);
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)); } } }
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; } }