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

2013/7/11

Changing colour of a fillRect(x1,y1,x2,y2)

8bitorangejuice 8bitorangejuice

2013/7/11

#
Hello, I need help changing colour of a fill. Here's what I got in a World class.
import greenfoot.*;  
import java.awt.Color
public class Menu extends World
{
    public Menu()
    {         
        super(600, 150, 1); 
        getBackground().fillRect(10,10,120,20);       
   
    }}
Thanks!
Gevater_Tod4711 Gevater_Tod4711

2013/7/11

#
To set the color of the background (or of any other GreenfootImage) you need to use getBackground().setColor(Color.black); (of course any other color would work too). And I think you missed a ; in line 2. That probably will cause compiler errors.
8bitorangejuice 8bitorangejuice

2013/7/15

#
Thanks for the help, now I need to know how to apply newColor(0,0,0,0) to the rectangle (I want transparent buttons). Thanks in advance!
Gevater_Tod4711 Gevater_Tod4711

2013/7/15

#
If I understood you right you want a rectagle to be transparent. Thats not so easy because if you overdraw the rectacle using transparent color nothing will heapen. I think the best way would be to use a method like this one:
public void transparentRect(GreenfootImage img, int x1, int y1, int x2, int y2) {
    Color transparency = new Color(0, 0, 0, 0);
    for (int i = x1; i < x2; i++) {
        for (int j = y1; j < y2; j++) {
            img.setColorAt(i, j, transparency);
        }
    }
}
To use this method you have to call it this way:
transparentRect(getBackground(), x1, y1, x2, y2);
instead of:
getBackground().transparentRect(x1, y1, x2, y2);
danpost danpost

2013/7/15

#
I presume the buttons you are using are actors and you want the background of your world to show through them. If this is the case, then filling a rectangle is not necessary as a new GreenfootImage object is transparent (and rectangular) to begin with. The only problem would then be drawing the text on the image with a transparent background; and there are two ways to accomplish that. One is creating a GreenfootImage object with the text (with transparent background) and drawing it on the image of the button. The other is using drawString to draw the string on the image of the button. An example of using this method is in this discussion. The example draws on the world background; but can be adopted to draw on any image.
8bitorangejuice 8bitorangejuice

2013/7/15

#
Hello, and thanks in advice for all the help. I'm afraid all that you have stated is incorrect; I am talking about an actor not a world... I should have said that... Here is the code I have in the actor so far:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;  
public class Start extends Button
{
 private boolean clicked=false;
    String buttonText = "";    
    public Start()     {  
        this("");    }    
    public Start(String text)    {  
        setText(text);      }   
    public void setText(String text)     {  
        buttonText=text;  
        GreenfootImage textImg=new GreenfootImage(" "+text+" ", 22, Color.black, new Color(0, 0, 0, 0));  
        GreenfootImage image=new GreenfootImage(120,20);  
        image.setColor(Color.lightGray);  
        image.fill();  
        image.setColor(Color.lightGray);  
        image.fillRect(0, 0,120,20);  
        image.setColor(Color.darkGray);  
        image.drawImage(textImg, (image.getWidth()-textImg.getWidth())/2, (image.getHeight()-textImg.getHeight())/2);  
        setImage(image);  
    }    
    public void act()  {  
        if(Greenfoot.mouseClicked(this)) {clicked=true;  }
        if(clicked=true){
                    Greenfoot.setWorld(new Test());
     }}  
    public boolean gotClicked()  
    {  
        boolean wasClicked=clicked;  
        clicked=false;  
        return wasClicked;  
    }    
    public String getText()  
    {  
        return buttonText; }  
}  
The whole class in general is in progress... Thanks in advice!
danpost danpost

2013/7/15

#
I stated that the button is probably an actor; and my response was along that line of thought. The only thing I am confused about now is where you want the transparencies to be (or on what). If you wanted the button to be transparent, then just remove lines 15 and 16. And, if you want your button object framed, change line 18 to 'image.drawRect(0, 0, 119, 19);', otherwise remove it and line 17. Line 19 does not have any use as 'drawImage' (on the next, and last, statement modifying the image of the button) does not use the set drawing color; so that line can be removed, also. One more thought: in line 13, you have " "+text+" ", which can be simply ""+text because the background is transparent anyway. Question: is the 'Start' button saved in a field in another class? The reason I ask is you have methods in the start class that makes me believe it is; and yet you are changing worlds immediately (without passing any information). And now, I noticed that line 25 should be either 'if (clicked == true)' or 'if (clicked)', not 'if (clicked=true)' which always will be true (using a single equals sign always sets the field on the left to the value on the right).
8bitorangejuice 8bitorangejuice

2013/7/16

#
Thanks for the help, it inspired me to do something! By the way, I wanted the button to be transparent. By the way, I used this code to achieve what I wanted:
image.setColor(new Color(205,201,201,175));  
image.fillRect(0, 0,120,20);
This is how the detection code turned out:
public void act()  
    {  
        if(Greenfoot.mouseClicked(this)) {
            sequence=1;
        }       
        if(sequence==1)
        {
            setLocation(getX(),getY()-1); //scroll up until all the buttons are uniform
        }
        if(sequence==1&&getY()>20)
        {
            sequence=2;      //stop setLocation(getX(),getY()-2
        }
        if(sequence==2)
        {
            setLocation(getX()-1,getY()); //scroll to the edge
        }
        if(sequence==2&&getX()==0)
        {
            Greenfoot.setWorld(new Test());  
        }
    }   
8bitorangejuice 8bitorangejuice

2013/7/17

#
Oh and as for the question: I am using a static int :)
You need to login to post a reply.