I want to change the one-colored picture of an actor using RGB color codes (ex.: 32, 54, 87)
First, when the key "0" is pressed down, you get asked which color codes you want to use. Then if you click on the actor the color should change, but it doesn't work.
Here are the codes for reference:
(MYWORLD CODE):
(BLACK ACTOR CODE):
import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class MyWorld extends World { private int ok; private int timer = 26; private int epsilon; private double delay = 0; public String color; /** * Constructor for objects of class MyWorld. */ public MyWorld() { super(810, 600, 1); epsilon = 15; ok = -15; addObject( new Black(), 15, 15); Greenfoot.start(); } /** * */ public void act() { rowOne(); button(); delay = delay - 1; } /** * */ public void rowOne() { Black black = (Black)getObjects(Black.class).get(0); if (timer >= 0) { ok = ok + 30; addObject( new Black(), ok, epsilon); timer = timer - 1; } if (ok == 795 && epsilon != 585) { this.epsilon = this.epsilon + 30; timer = 26; ok = -15; } } /** * */ public void button() { if (Greenfoot.isKeyDown("1") && delay <= -1) { color = "BL"; Greenfoot.playSound("BLACK.mp3"); this.delay = 50; } else if (Greenfoot.isKeyDown("2") && delay <= -1) { color = "W"; Greenfoot.playSound("WHITE.mp3"); this.delay = 50; } else if (Greenfoot.isKeyDown("3") && delay <= -1) { color = "RED"; Greenfoot.playSound("RED.mp3"); delay = 50; } else if (Greenfoot.isKeyDown("4") && delay <= -1) { color = "YEL"; Greenfoot.playSound("YELLOW.mp3"); delay = 50; } else if (Greenfoot.isKeyDown("5") && delay <= -1) { color = "BLU"; Greenfoot.playSound("BLUE.mp3"); delay = 50; } else if (Greenfoot.isKeyDown("6") && delay <= -1) { color = "ORA"; Greenfoot.playSound("ORANGE.mp3"); delay = 50; } else if (Greenfoot.isKeyDown("7") && delay <= -1) { color = "GRE"; Greenfoot.playSound("GREEN.mp3"); delay = 50; } else if (Greenfoot.isKeyDown("8") && delay <= -1) { color = "VIO"; Greenfoot.playSound("VIOLET.mp3"); delay = 50; } else if (Greenfoot.isKeyDown("9") && delay <= -1) { color = "PINK"; Greenfoot.playSound("PINK.mp3"); delay = 50; } else if (Greenfoot.isKeyDown("0") && delay <= -1) { colorPicker(); } } public void colorPicker() { color = "PICKER"; delay = 50; } }
import lang.stride.*; import java.util.*; import greenfoot.*; import java.awt.Color; /** * */ public class Black extends Actor { private int InputValueR; private int InputValueG; private int InputValueB; /** * Act - do whatever the Black wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { String worldColor = ((MyWorld)getWorld()).color; if (Greenfoot.mouseClicked(this) && worldColor == "BL") { this.setImage("black_px.png"); } if (Greenfoot.mouseClicked(this) && worldColor == "W") { this.setImage("white_px.png"); } if (Greenfoot.mouseClicked(this) && worldColor == "RED") { this.setImage("red_px.png"); } if (Greenfoot.mouseClicked(this) && worldColor == "YEL") { this.setImage("yellow_px.png"); } if (Greenfoot.mouseClicked(this) && worldColor == "BLU") { this.setImage("blue_px.png"); } if (Greenfoot.mouseClicked(this) && worldColor == "ORA") { this.setImage("orange_px.png"); } if (Greenfoot.mouseClicked(this) && worldColor == "GRE") { this.setImage("green_px.png"); } if (Greenfoot.mouseClicked(this) && worldColor == "VIO") { this.setImage("violet_px.png"); } if (Greenfoot.mouseClicked(this) && worldColor == "PINK") { this.setImage("pink_px.png"); } if (worldColor == "PICKER") { InputValueR = Integer.parseInt(Greenfoot.ask("Input R Number")); InputValueG = Integer.parseInt(Greenfoot.ask("Input G Number")); InputValueB = Integer.parseInt(Greenfoot.ask("Input B Number")); ((MyWorld)getWorld()).color = "PICKERP2"; } if (Greenfoot.mouseClicked(this) && ((MyWorld)getWorld()).color == "PICKERP2") { this.picker(); } } /** * */ public void picker() { this.getImage().setColor( new greenfoot.Color(InputValueR, InputValueG, InputValueB)); } }