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

2021/9/9

Need Help with Changing the Color of an Actor's Picture using RGB Color codes

DatHeroAndy DatHeroAndy

2021/9/9

#
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):
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;
    }
}
(BLACK ACTOR CODE):
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));
    }
}
danpost danpost

2021/9/9

#
DatHeroAndy wrote...
I want to change the one-colored picture of an actor using RGB color codes (ex.: 32, 54, 87)
Line 64 of Black class -- setting the color only prepare that color to be used on future draw commands. For example, you can follow that line with:
this.getImage().fill();
DatHeroAndy DatHeroAndy

2021/9/9

#
Okay, but now I see that the "Integer.parseInt(Greenfoot.ask("Input R/B/G Number"));" lines don't even work. No matter what I type in, it always stays at 0. Is there also a way to fix that? EDIT: Now I see that this line only affects the first black box, but I have multiple of them.
DatHeroAndy DatHeroAndy

2021/9/9

#
Nevermind, fixed that! And also thanks for the "this.getImage().fill();" line.
You need to login to post a reply.