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

2017/3/8

Write Multiple Things To A File

3
4
5
6
7
danpost danpost

2017/4/2

#
mitrolex wrote...
How do i change the location of those images(strings or whatever they are..) within a loop?
Change the offsets:
image1.drawImage(image2, offfsetX, offsetY);
Within the loop, you could add like 'i*30' to 'offsetY', where 'i' is the for loop counter:
for (int i=0; i<data.length; i++)
mitrolex mitrolex

2017/4/2

#
Could you somehow implement that into this code:
import greenfoot.*; 
import java.io.*;
public class rank extends Actor
{
    boolean p = false;
    rankSlika rs = new rankSlika();
    rankListaIme rli = new rankListaIme();//This is for all the names from the file.
    rankListaPoeni rlp = new rankListaPoeni();//And this is for all the scores from the file.    
    Color c = new Color(0,0,0,0);
    int g = 0;
    public void act() 
    {            
        if(Greenfoot.mouseClicked(this))
        {
            g++;
            bg b = (bg)getWorld();             
            p = true; 
            getWorld().addObject(rs,b.getWidth()/2,b.getHeight()/2);
            getWorld().addObject(rli,b.getWidth()/2-112,b.getHeight()/2-112);//Names
            getWorld().addObject(rlp,b.getWidth()/2+180,b.getHeight()/2-112);//Scores        
            while(p==true)
            {
                b.citanje();
                b.rank();
                p=false;
            } 

            for (int n=0; n<b.data.length; n++)
            {                
                rli.setImage(new GreenfootImage(b.ime, 35, Color.BLACK, c));//Names
                rlp.setImage(new GreenfootImage(""+b.poeni, 35, Color.BLACK, c));//Scores               
            }
        }       

        if (Greenfoot.mouseClicked(this)&&g>=2) 
        {
            getWorld().removeObject(rs);
            getWorld().removeObject(rli);
            getWorld().removeObject(rlp);
            g=0;
        }

    }
}   

I don't really understand what you are talking about..
Super_Hippo Super_Hippo

2017/4/3

#
Line 27, create one image:
GreenfootImage image = new GreenfootImage(200, 30*b.data.length); //30 should be the height of one image (you could check the size of the current image)
Then, in the for loop, draw a new image for each one on the image.
for (int n=0; n<b.data.length; n++)
{
    image.drawImage(new GreenfootImage(b.ime, 35, Color.BLACK, c), 0, 30*n);
    image.drawImage(new GreenfootImage(""+b.poeni, 35, Color.BLACK, c), 50, 30*n);
}
mitrolex mitrolex

2017/4/3

#
Now there are just two small greenfoot logos, one for the name and one for the score. When i remove lines 7, 8, 19, 20, 38 and 39 from my code theres nothing.
danpost danpost

2017/4/3

#
mitrolex wrote...
Now there are just two small greenfoot logos, one for the name and one for the score. When i remove lines 7, 8, 19, 20, 38 and 39 from my code theres nothing.
After the for loop, you still need to 'setImage(image);'.
mitrolex mitrolex

2017/4/8

#
Managed to make something out of it but i have a problem with positions of the strings. They are moving around the screen according to the number of names and scores in the file. When there are 10 or more things in the file everything is where it should be but when there are less then 10 things in the file everything moves a bit down. I hope you understood what i said here. :-D
danpost danpost

2017/4/8

#
mitrolex wrote...
When there are 10 or more things in the file everything is where it should be but when there are less then 10 things in the file everything moves a bit down. I hope you understood what i said here. :-D
Understand? -- not exactly. You need to show the code where these actors are being placed in the world (preferably the class code -- to start with).
mitrolex mitrolex

2017/4/9

#
import greenfoot.*; 
import java.io.*;
public class rank extends Actor
{
    boolean p = false;
    rankSlika rs = new rankSlika();    
    rankListaPoeni rlp = new rankListaPoeni();
    Color c = new Color(0,0,0,0);    
    public void act() 
    {        
        int g=0;          
        if(Greenfoot.mouseClicked(this))
        {    
            g++;
            bg b = (bg)getWorld();             
            p = true; 
            getWorld().addObject(rs,b.getWidth()/2,b.getHeight()/2);            
            getWorld().addObject(rlp,b.getWidth()/2+180,b.getHeight()/2-112);       
            while(p==true)
            {
                b.citanje();
                b.rank();
                p=false;
            }
            int nMax=b.data.length;
            if(b.data.length>=10)
            {
                nMax=10;
            }          
            int h = nMax*24;
            GreenfootImage image = new GreenfootImage(900, h+28*nMax);
            for (int n=0; n<nMax; n++)
            {                       
                image.drawImage(new GreenfootImage(n+1+"."+(String)b.data[n][0]+":"+(Integer)b.data[n][1], 35, Color.BLACK, c), 105, h+28*n);                                                              
                rlp.setImage(image);
            }            
        }               
        if(Greenfoot.mouseClicked(rlp)) 
        {            
            getWorld().removeObjects(getWorld().getObjects(rankSlika.class)); 
            getWorld().removeObjects(getWorld().getObjects(rankListaPoeni.class));
        }
    }
} 
Here's the class where the actors are being placed into the world.
mitrolex mitrolex

2017/4/13

#
Anything?
danpost danpost

2017/4/13

#
The only things I see are: (1) a 'g' variable that does absolutely nothing; (2) a 'p' boolean that might as well not exist; and (3) a 'setImage' line that would be better placed after the loop; As far as the placement of the lines, maybe you should set 'h' to a constant of '240' at line 30, instead of varying the "header" size depending on the length of the array.
mitrolex mitrolex

2017/4/14

#
http://imgur.com/iqs2ne7 It looks like this when there are 2 players in the file. http://imgur.com/a8K29y2 And like this when there are 10 of them.
import greenfoot.*; 
import java.io.*;
public class rank extends Actor
{
    rankSlika rs = new rankSlika();    
    rankListaPoeni rlp = new rankListaPoeni();
    Color c = new Color(0,0,0,0);    
    public void act() 
    {                       
        if(Greenfoot.mouseClicked(this))
        {
            bg b = (bg)getWorld();
            getWorld().addObject(rs,b.getWidth()/2,b.getHeight()/2);            
            getWorld().addObject(rlp,b.getWidth()/2+180,b.getHeight()/2-112);            
                b.citanje();
                b.rank();                           
            int nMax=b.data.length;
            if(b.data.length>=10)
            {
                nMax=10;
            }          
            int h = 250;
            GreenfootImage image = new GreenfootImage(900, h+28*nMax);
            for (int n=0; n<nMax; n++)
            {                       
                image.drawImage(new GreenfootImage(n+1+"."+(String)b.data[n][0]+":"+(Integer)b.data[n][1], 35, Color.BLACK, c), 105, h+28*n);                                                              
                
            }
            rlp.setImage(image);
        }               
        if(Greenfoot.mouseClicked(rlp)) 
        {            
            getWorld().removeObjects(getWorld().getObjects(rankSlika.class)); 
            getWorld().removeObjects(getWorld().getObjects(rankListaPoeni.class));
        }
    }
}  
Here's the code with the changes you mentioned.
Super_Hippo Super_Hippo

2017/4/14

#
Try to add this line after line 29:
setLocation(getX()-getWorld().getHeight()/2+50+image.getWidth()/2, getY());
mitrolex mitrolex

2017/4/14

#
Nope, everything stays the same.
Super_Hippo Super_Hippo

2017/4/14

#
Lost internet connection, so couldn't edit earlier...
setLocation(getX(), getY()-getWorld().getHeight()/2+50+image.getHeight()/2);
mitrolex mitrolex

2017/4/14

#
It's moving the actor that i click on and not the strings that get written on the screen.
There are more replies on the next page.
3
4
5
6
7