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

2019/5/28

Transparency on Click

1
2
3
Celesterra Celesterra

2019/5/28

#
I am trying to make the number class become transparent when I click it and if it is clicked again, it returns to a solid color. However, the image of the number is variable (1 to 9) and the image was made within the program. If anyone could help, I thank you in advance. Number Class:
public class Number extends Actor
{
    //Variables used
    private GreenfootImage img = getImage();
    private boolean clicked = false;
    private boolean match = false;
    public int number;
    
    One one = new One();
    Two two = new Two();
    Three three = new Three();
    Four four = new Four();
    Five five = new Five();
    Six six = new Six();
    Seven seven = new Seven();
    Eight eight = new Eight();
    Nine nine = new Nine();
    
    public Number(int n)
    {
        number = n;
        image();
    }
    
    public void act() 
    {
        //If the one was clicked, clear the image and change the number to gray
        if(Greenfoot.mouseClicked(this) && !clicked)
        {
            updateImage();
            clicked = true;
        }
        
        //If the one was clicked, keep it clicked
        if(!Greenfoot.mouseClicked(this) && clicked)
        {
            clicked = true;
        }
        
        //If the clicked one is clicked again, unclick it
        if(Greenfoot.mouseClicked(this) && clicked)
        {
            resetImage();
            clicked = false;
        }
    }
    
    public void image()
    {
        if(number == 1) setImage(one.getImage());
        
        if(number == 2) setImage(two.getImage());
        
        if(number == 3) setImage(three.getImage());
        
        if(number == 4) setImage(four.getImage());
        
        if(number == 5) setImage(five.getImage());
        
        if(number == 6) setImage(six.getImage());
        
        if(number == 7) setImage(seven.getImage());
        
        if(number == 8) setImage(eight.getImage());
        
        if(number == 9) setImage(nine.getImage());
    }
    
    public void updateImage()
    {
        img.setTransparency(100);
    }
    
    public void resetImage()
    {
        img.setTransparency(255);
    }
    
    public int getActualHeight()
    {
        //Gets the height of a number's image
        return getImage().getHeight();
    }
    
    public int getActualWidth()
    {
        //Gets the width of a number's image
        return getImage().getWidth();
    }
}
One Class: - 1 to 9 class are similar, just have different image colors
public class One extends Actor
{
    public One()
    {
        //Creates a 45x45 empty image
        GreenfootImage tile = new GreenfootImage(45,45);
        
        //Sets the font and its size
        Font font = new Font("Courier", true, false, 45);
        tile.setFont(font);
        
        //Frames the tile with a square
        tile.setColor(Color.GRAY);
        tile.drawRect(0, 0, 45, 45);
        
        //Displays the number one
        tile.setColor(Color.BLACK);
        tile.drawString("1",11,38);
        
        //Sets the image of the tile
        setImage(tile);
    }
}
danpost danpost

2019/5/28

#
I think to reset the image, you just set the image to img. -- (maybe not; just a guess)
Super_Hippo Super_Hippo

2019/5/28

#
If the number variable never changes, why does your Number object need to create nine objects of different classes? Try this class:
import greenfoot.*;
public class Number extends Actor
{
    private static GreenfootImage[] images = new GreenfootImage[10]; //creating the 0 as well so the index equals the displayed number
    
    static
    {
        GreenfootImage baseImage = new GreenfootImage(45, 45);
        baseImage.setFont(new Font(true, false, baseImage.getHeight()));
        baseImage.setColor(Color.GRAY);
        baseImage.drawRect(0, 0, baseImage.getWidth()-1, baseImage.getHeight()-1);
        baseImage.setColor(Color.BLACK);
        for (int i=0; i<images.length; i++)
        {
            images[i] = new GreenfootImage(baseImage);
            images[i].drawString(""+i, 11, 38);
        }
    }
    
    public Number(int i)
    {
        setImage(new GreenfootImage(images[i]));
    }
    
    public void act()
    {
        if (Greenfoot.mousePressed(this))
        {
            getImage().setTransparency(getImage().getTransparency()==255 ? 100 : 255);
        }
    }
}
Celesterra Celesterra

2019/5/29

#
@Super_Hippo I have the different classes because the numbers are all different colors. If you have any way to add different colors for each number, let me know. Here are the colors for each number (which are custom colors, except black): One and Nine: Black Two and Eight: Orange Three and Seven: Red Four and Six: Green Five: Blue
Celesterra Celesterra

2019/5/29

#
Now that I think about it, could I make a color into a variable? Ex: color = new Color(0,0,0);
danpost danpost

2019/5/29

#
Celesterra wrote...
If you have any way to add different colors for each number, let me know.
Remove the image method and change the constructor to the following:
public Number(int n)
{
    number = n;
    Color[] colors = new Color[]
    {
        Color.BLACK, Color.ORANGE, Color.RED,
        Color.GREEN, Color.BLUE, Color.GREEN,
        Color.RED, Color.ORANGE, Color.BLACK
    };
    GreenfootImage tile = new GreenfootImage(45, 45);
    Font font = new Font("Courier", true, false, 45);
    tile.setFont(font);
    tile.setColor(colors[n-1]);
    tile.fill();
    tile.setColor(Color.BLACK);
    tile.drawString(""+n,11,38);
    setImage(tile);
}
Then you can delete all 9 number classes (One, Two, etc.).
danpost danpost

2019/5/29

#
Celesterra wrote...
Now that I think about it, could I make a color into a variable? Ex: color = new Color(0,0,0);
You could do that and pass it along with the number to the constructor (instead of what I did above).
danpost danpost

2019/5/29

#
Oops, I changed the wrong color in my code above. Line 13 should be:
tile.setColor(Color.GRAY);
and line 15 should be:
tile.setColor(colors[n-1]);
Celesterra Celesterra

2019/5/30

#
Alright, I made the number class the way danpost said and it works (much better than what I had). However, I still can't get the transparency to work. Could it be that I need to set the image to the transparent one after, or am I missing something completely obvious? I did try Super_Hippo's code, but it didn't seem to work. New Number Code:
public class Number extends Actor
{
    //Variables and Arrays used
    private GreenfootImage img = getImage();
    private boolean clicked = false;
    private boolean match = false;
    public int number;
    
    Color orange = new Color(255,161,38);
    Color red = new Color(163,8,3);
    Color green = new Color(8,204,11);
    Color blue = new Color(37,113,234);
    Color[] colors = new Color[] {Color.BLACK, orange, red, green, blue, green, red, orange, Color.BLACK};
    
    public Number(int n)
    {
        number = n;
        
        //Creates an image that is 45x45
        GreenfootImage tile = new GreenfootImage(45, 45);
        
        //Sets the font of the image
        Font font = new Font("Courier", true, false, 45);
        tile.setFont(font);
        
        //Frames the tile with a gray square
        tile.setColor(Color.GRAY);
        tile.drawRect(0,0,45,45);
        
        //Displays the number and colors the number (depending on the number)
        tile.setColor(colors[n-1]);
        tile.drawString(""+ n ,11,38);
        
        //Sets the image of the tile
        setImage(tile);
    }
    
    public void act() 
    {
        //If a number was clicked, clear the image and change the image's transparency
        if(Greenfoot.mouseClicked(this) && !clicked)
        {
            img.setTransparency(100);
            clicked = true;
        }
        
        //If a number was clicked, keep it clicked
        if(!Greenfoot.mouseClicked(this) && clicked)
        {
            clicked = true;
        }
        
        //If the clicked number is clicked again, unclick it
        if(Greenfoot.mouseClicked(this) && clicked)
        {
            img.setTransparency(255);
            clicked = false;
        }
    }
}
danpost danpost

2019/5/30

#
Celesterra wrote...
I still can't get the transparency to work. Could it be that I need to set the image to the transparent one after, or am I missing something completely obvious? << Code Omitted >>
The obvious thing is that you change the image. The one referenced by img is NOT the image that you create and set to the actor in the constructor. Drop line 4 and replace img with getImage() in lines 43 and 56.
Celesterra Celesterra

2019/5/30

#
It still doesn't work... What am I missing?
danpost danpost

2019/5/30

#
Celesterra wrote...
It still doesn't work... What am I missing?
Please explain exactly what you want and how the behavior is different from that in details.
danpost danpost

2019/5/30

#
There is one thing I noticed. On line 48, you are checking for not clicking on the actor. This is true when no clicks occur as well. Change line 48 to the following:
if (Greenfoot.mouseClicked(null) && !Greenfoot.mouseClicked(this) && clicked)
Celesterra Celesterra

2019/5/30

#
I changed it, but it still isn't working. What I am trying to accomplish is if a number is clicked once, the image changes and it stays 'clicked' until it is clicked again.
danpost danpost

2019/5/30

#
Celesterra wrote...
I changed it, but it still isn't working. What I am trying to accomplish is if a number is clicked once, the image changes and it stays 'clicked' until it is clicked again.
Try simplifying your act method to this:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        setTransparency(355-getTransparency());
    }
}
There are more replies on the next page.
1
2
3