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

2014/5/6

I dont understand the image. code!

1
2
Thrillofit Thrillofit

2014/5/6

#
Hey all! Im new at programmering and needed some help with it! I have made/got help about this code and wonder what image. code means here http://pastebin.com/CxJa99Ph
AIMrOrange AIMrOrange

2014/5/6

#
Check out this link
Thrillofit Thrillofit

2014/5/6

#
Yeah i have seen that but i still cant understand. The problem i meant is that i dont really know what Image comes from. What it does actually. Like, what image?
AIMrOrange AIMrOrange

2014/5/6

#
image is the object that contains an image. For examle:
1
GreenfootImage image = new GreenfootImage("picture.png");
You make a new Image called "image" which contains the image "picture.png". This image can now be manipulated with methods like "setRotation()", "scale()" and other methods. They all do something with the picture of your object.
Thrillofit Thrillofit

2014/5/6

#
I really trying to understand but i do understand better now:) But in my code it says Greenfootimage image = getImage() But what kind of image does it do then? because the code does a loading counter from 0% to 100%.
danpost danpost

2014/5/6

#
If you use 'getImage', which is an Actor class method, it will return the GreenfootImage object that holds the currently set image for that actor. For more information, please supply some code around the one line you gave (giving some context); or, maybe ask in a different way, so that we may more understand what information you are looking for.
Thrillofit Thrillofit

2014/5/6

#
Okey! I will post the code tommorow morning since its 12 AM here now and i need to get some sleep right now! :))))
danpost danpost

2014/5/7

#
I have looked at the pastebin code and came to the conclusion that the code shown is probably not what you want to use. There are a couple of reasons I say that which I will not get into at this time. Let us just say that from a programming perspective it is far from optimal and uses some non-standard techniques to accomplish various tasks. From where did you acquire that ShieldPercent class? I have a Bar class that might suit your needs. It is called Progress bar/health bar class. It is a fully functional (maybe overly friendly with all the methods available) bar class. I could easily break it down to the minimal code that you would need if you like.
Thrillofit Thrillofit

2014/5/7

#
Here is the code for ShieldPercent:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
  
public class ShieldPercent extends Actor
{
    GreenfootImage image = getImage();
    private String procent = (int) Rocket.shieldAmmount + "%";
    private int length = procent.length();
    public ShieldPercent()
    {
        image.clear();
        image.scale(40, image.getHeight());
        image.setColor(Color.GREEN);
        image.drawString(procent, (image.getWidth() / 2) - (procent.length() * 3), image.getHeight());
    }
  
    /**
     * Act - do whatever the FuelIndicator wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        updateImage();
        procent = (int) Rocket.shieldAmmount + " %";
    }   
  
    private void updateImage()
    {
        if (Rocket.shieldAmmount <= 101)
        {
            image.clear();
            image.setColor(Color.GREEN);
            image.drawString(procent, (image.getWidth() / 2) - (procent.length() * 3), image.getHeight());
        
    }
}
}
Since i dont know how to upload my full game so i will share it by Zippyshare.com. Here is my full version game: http://www55.zippyshare.com/v/32173281/file.html
Thrillofit Thrillofit

2014/5/7

#
I would appreciate it danpost! All i need is a kind a bar that counts from 0% to 100%. And by using my shield, it should drop down to 0% and load again up to 100%. Oh yeah, i hope you do understand where i want to come:)
danpost danpost

2014/5/7

#
Ok, I came up with this more simplistic bar compared to my Bar class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import greenfoot.*;
 
public class Bar extends Actor
{
    private int value;
    private int maxValue;
    private String uom;
    private GreenfootImage titleImage;
 
    public Bar(String title, String suffix, int val, int max)
    {
        uom = suffix;
        maxValue = max;
        value = val;
        titleImage = new GreenfootImage(" "+title+" ", 20, java.awt.Color.black, null);
        updateImage();
    }
 
    public void setValue(int val)
    {
        if (val < 0 || val > maxValue || value == val) return;
        value = val;
        updateImage();
    }
 
    private void updateImage()
    {
        GreenfootImage frame = new GreenfootImage(104, 14);
        frame.drawRect(0, 0, 103, 13);
        GreenfootImage bar = new GreenfootImage(100, 10);
        bar.setColor(java.awt.Color.green);
        bar.fillRect(0, 0, 100*value/maxValue, 10);
        frame.drawImage(bar, 2, 2);
        GreenfootImage valueImage = new GreenfootImage(" "+value+" "+uom+" ", 20, java.awt.Color.black, null);
        int greaterWidth = titleImage.getWidth();
        if (valueImage.getWidth() > greaterWidth) greaterWidth = valueImage.getWidth();
        GreenfootImage main = new GreenfootImage(2*greaterWidth+104, 16);
        main.drawImage(frame, main.getWidth()/2-52, 2);
        main.drawImage(titleImage, main.getWidth()/2-52-titleImage.getWidth(), 8-titleImage.getHeight()/2);
        main.drawImage(valueImage, main.getWidth()/2+52, 8-valueImage.getHeight()/2);
        setImage(main);
    }
}
I used this for testing, which is an example of how to implement it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;
 
public class TestWorld extends World
{
    int maxShieldStrength = 100;
    int shieldStrength = maxShieldStrength;
    Bar shieldBar;
     
    public TestWorld()
    {
        super(300, 100, 1);
        addObject(shieldBar = new Bar("Shield", "%", 100, 100), 150, 50);
    }
     
    public void act()
    { // use the space bar to trigger hit and lose of shield strength for testing
        if (shieldStrength > 0 && Greenfoot.isKeyDown("space"))
        {
            shieldStrength--;
            shieldBar.setValue(100*shieldStrength/maxShieldStrength);
        }
    }
}
Thrillofit Thrillofit

2014/5/7

#
Ok and by that, should i remove my Shieldpercent Class and add a new one with the code u gave me? Or should i I put it on Space class?
danpost danpost

2014/5/7

#
You can remove the Shieldpercent class and add my Bar class, then implement it similar to how my TestWorld class implements it. You may wish to pass the Bar object to the ship (or whatever will be using the shield) to have it in a field in that class instead of in the world class for easier access when the shield strength for that object changes. It would be a bar specifically for the shield of that specific object, so that is where the field holding the reference to the Bar object should be.
Thrillofit Thrillofit

2014/5/7

#
Okey just did it and got the problem with ur code nr7. " Bar shieldBar;", I should prob. add something in Space, But i dont really know what?
danpost danpost

2014/5/7

#
What is the message you are getting about that line? Did you not add the Bar class to your project?
There are more replies on the next page.
1
2