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

2013/3/10

why do i see a gray square?

tylers tylers

2013/3/10

#
i have this code which displays help box. but i want it to grow bigger but every time it turns to a Gray square. this is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
import  java.awt.Color;
/**
 * Write a description of class Help here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Help extends Actor
{
    GreenfootImage img;
    int width;
    int height;
    int cw;
    int ch;
    int doh;
    int dow;
    public Help(String text,int width, int height){
        this.width = width;
        this.height = height;
        img = new GreenfootImage(width,height);
        getImage().scale(width,height);
        img = getImage();        
        Color ts = new Color(239,249,249);
        Font title = new Font("Verdana", Font.BOLD, 12);  
        img.setFont(title);  

        img.drawString(text,45,27); 
        setImage(img);
    }

    /**
     * Act - do whatever the Help wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(cw<width){
            dow++;
        }
        if(ch<height){
            doh++;
        }
        getImage().scale(dow,doh);
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/3/10

#
You should change the color before writing the text. Otherwhile the image will draw the text in the same color and you just can't see it.
tylers tylers

2013/3/10

#
i fixed it now :D i was somthing to do with a bug in my code. this is the FIXED code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
import  java.awt.Color;
/**
 * Write a description of class Help here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Help extends Actor
{
    GreenfootImage img;
    GreenfootImage pImg;

    int width;
    int height;
    int cw;
    int ch;
    int doh;
    int dow;
    String title;
    public Help(String title,int width, int height){
        this.width = width;
        this.height = height;
        this.title = title;
        render(false);
    }

    /**
     * Act - do whatever the Help wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(cw<width){
            dow++;
            cw++;
        }
        if(ch<height){
            doh++;
            ch++;
        }
        render(true);
    }

    public void render(boolean sRender){
        img = new GreenfootImage(width,height);
        pImg = new GreenfootImage("info.png");
        pImg.scale(width,height);
        img.clear();
        img = pImg;
        Color ts = new Color(239,249,249);
        Font titles = new Font("Verdana", Font.BOLD, 12);  
        img.setFont(titles);  

        img.drawString(title,45,27); 
        setImage(img);
        
        if(sRender){            
            img.scale(dow, doh);
        }        
    }
}

Gevater_Tod4711 Gevater_Tod4711

2013/3/10

#
A sugestion for improvement: I think it would be much easyer to use the GreenfootImage(String text, int fontsize, Color foregroundColor, Color backgroundColor) constructor if I understood right what your method should do. Here is a link to the API
danpost danpost

2013/3/10

#
For one thing, lines 22 is useless. You are setting 'img' to a new image; then two lines later setting 'img' to the image of the object that has been scaled, which eliminates the image created. What is causing the gray image is line 45, which scales the same image multiple times, which tends to distort the original image. I have also noticed that you have an excessive number of variables in the class. Compare the following with what you had:
import greenfoot.*; 
import java.awt.Font;
import  java.awt.Color;

public class Help extends Actor
{
    GreenfootImage image;
    int targetWidth;
    int targetHeight;

    public Help(String text, int width, int height)
    {
        targetWidth = width;
        targetHeight = height;
        image = new GreenfootImage(getImage());
        getImage().scale(1, 1);
    }

    public void act()
    {
        int width = getImage().getWidth();
        int height = getImage().getHeight();
        if (width == targetWidth && height == targetHeight) return;
        if (width < targetWidth) width++;
        if (height < targetHeight) height++;
        GreenfootImage img = new GreenfootImage(image);
        img.scale(width, height);
        img.setColor(new Color(239,249,249));
        img.setFont(new Font("Verdana", Font.BOLD, 12));  
        img.drawString(text, 45, 27); 
        setImage(img);
    }
}
Line 26 is the line that creates a copiesof the original image that each will be scaled once.
You need to login to post a reply.