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

2018/2/8

Align Left

Maus2525 Maus2525

2018/2/8

#
Hey! I just tried to set a new text on an image each time I press a certain key, and my program isn't working. Also, I need my text to be aligned to the top-left corner!!! Can you help me find my coding mistakes? I'd love to see whether the solution can be written in a repetitive "for" or "while" loop. Thanks!
import greenfoot.*;
import java.awt.Color;

public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage back = getImage();
    
    public String text_Mercur = "Mercury";
    public String text_Venus = "Venus";
    public String text_Pamant = "Earth";
    public String text_Marte = "Mars";
    public String text_Ceres = "Ceres";
    public String text_Jupiter = "Jupiter";
    public String text_Saturn = "Saturn";
    public String text_Uranus = "Uranus";
    public String text_Neptun = "Neptune";
    public String text_Pluto = "Pluto";
    
    public Counter()
    {
        GreenfootImage table = getImage();
        table.rotate(90);
        table.scale(600, 400);
    }

    public void act()
    {
        GreenfootImage background = new GreenfootImage(back);
        if (Greenfoot.isKeyDown("1") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Mercur);
        }
        else if (Greenfoot.isKeyDown("2") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Venus);
        }
        else if (Greenfoot.isKeyDown("3") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Pamant);
        }
        else if (Greenfoot.isKeyDown("4") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Marte);
        }
        else if (Greenfoot.isKeyDown("5") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Ceres);
        }
        else if (Greenfoot.isKeyDown("6") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Jupiter);
        }
        else if (Greenfoot.isKeyDown("7") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Saturn);
        }
        else if (Greenfoot.isKeyDown("8") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Uranus);
        }
        else if (Greenfoot.isKeyDown("9") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Neptun);
        }
        else if (Greenfoot.isKeyDown("0") == true)
        {
            GreenfootImage text = new GreenfootImage(text_Pluto);
        }
        GreenfootImage text = new GreenfootImage(text, 24, Color.BLACK, null);
        background.drawImage(text, 0, 0);
        setImage(background);
        text.clear();
    }
}
Super_Hippo Super_Hippo

2018/2/8

#
Line 32 (and the similar ones) are creating an image and they aren't used. Line 70 shouldn't compile. Maybe this is what you are trying to do?
    public void act()
    {
        GreenfootImage background = new GreenfootImage(back);
        String text = "";
        if (Greenfoot.isKeyDown("1")) text = "Mercury";
        else if (Greenfoot.isKeyDown("2")) text = "Venus";
        else if (Greenfoot.isKeyDown("3")) text = "Earth";
        else if (Greenfoot.isKeyDown("4")) text = "Mars";
        else if (Greenfoot.isKeyDown("5")) text = "Ceres";
        else if (Greenfoot.isKeyDown("6")) text = "Jupiter";
        else if (Greenfoot.isKeyDown("7")) text = "Saturn";
        else if (Greenfoot.isKeyDown("8")) text = "Uranus";
        else if (Greenfoot.isKeyDown("9")) text = "Neptune";
        else if (Greenfoot.isKeyDown("9")) text = "Pluto";
        
        GreenfootImage txt = new GreenfootImage(text, 24, Color.BLACK, transparent);
        background.drawImage(txt, 0, 0);
        setImage(background);
    }
Maus2525 Maus2525

2018/2/9

#
Hey! Thanks, but my code is still not working. I want to write on an imported image on the top left corner some information when I consequently press the number keys on my keyboard, but the canvas is still blank.
danpost danpost

2018/2/9

#
Try WHITE instead of BLACK on line 16.
Maus2525 Maus2525

2018/2/9

#
Thanks! Now it displays the text, but I'd like to know how to align it left. It's strange that when I use the endline symbol '\n', it moves all text to the center of the image.
danpost danpost

2018/2/9

#
I do not see where you are using the "new line" escape sequence -- '\n'. At any rate, you will have to create a separate GreenfootImage for each line and then draw them individually on the background image.
Maus2525 Maus2525

2018/2/9

#
Okay, thanks!
You need to login to post a reply.