import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Button here. * * @author (your name) * @version (a version number or a date) */ public class Button extends Actor { /** * Act - do whatever the Button wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ static final Color TRANS = new Color(0,0,0,0); Actor btn01,btn02; Actor mouseOn; public void act() { if (Greenfoot.mouseClicked(btn01)) { HelloWorld hello = (HelloWorld) getOneIntersectingObject(HelloWorld.class); hello.printHello(); } if (Greenfoot.mouseClicked(btn02)) { ConstructorNoArg noArg = (ConstructorNoArg) getOneIntersectingObject(ConstructorNoArg.class); noArg.printConstructorNoArg(); } } protected void addedToWorld(World world) { world.addObject(btn01 = getNewButton("BUTTON 01"),1050,50); world.addObject(btn01 = getNewButton("BUTTON 02"),1050,100); } protected Actor getNewButton(String caption) { GreenfootImage img = new GreenfootImage(200,30); img.fill(); img.setColor(Color.BLUE); img.fillRect(3,3,194,24); GreenfootImage text = new GreenfootImage(caption,20,Color.WHITE,TRANS); img.drawImage(text,100-text.getWidth()/2,15-text.getHeight()/2); img.setTransparency(128); Actor button = new SimpleActor() { public void act() { if (mouseOn == null && Greenfoot.mouseMoved(this)) { mouseOn = this; getImage().setTransparency(255); } if (mouseOn == this && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this)) { mouseOn = null; getImage().setTransparency(128); } } }; button.setImage(img); return button; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HelloWorld here. * * @author (your name) * @version (a version number or a date) */ public class HelloWorld extends Actor { /** * Act - do whatever the HelloWorld wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { printHello(); } public void printHello() { GreenfootImage hello = new GreenfootImage(900, 600); hello.setColor(Color.BLUE); hello.setFont(new Font("calibri", true, true, 50)); hello.drawString("public class Hello {", 50, 100); hello.drawString(" public static void main(String[] args) {", 50, 140); hello.drawString(" System.out.println(\"Hello, World!\");", 50, 180); hello.drawString(" }", 50, 220); hello.drawString("}", 50, 260); setImage(hello); } }