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

2021/5/22

HOW TO CREATE A SCROLL BAR ?

1
2
ronald ronald

2021/5/23

#
as your scenario is for the text scroll bar we will have to create a new code for the jpg image I think
danpost danpost

2021/5/23

#
ronald wrote...
as your scenario is for the text scroll bar we will have to create a new code for the jpg image I think
No. I created an image with text prior to building the scroll bar. The image is what scrolls (just like what you want to do). The content of the image is irrelevant (non-text or text). ONLY difference is value of lineHeight (for my text, is was height of a line of text; for you, it is simply one). It is equivalent to the number of pixels to scroll by.
ronald ronald

2021/5/23

#
OK I understand I will try, I can be my little idea for that I'll keep you informed
ronald ronald

2021/5/23

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScrollBarV01 extends World
{
    
    int scroll = 0;
    int loY, hiY, range;
    int pixelHeight, maxScroll;
    Actor page, handle;
    
    public ScrollBarV01()
    {    
        super(900, 600, 1, false);
        
        GreenfootImage image = new GreenfootImage("galaxy-stars02.jpg");
        pixelHeight = image.getHeight();
        
        GreenfootImage pane = new GreenfootImage(900, pixelHeight);
        maxScroll = (pane.getHeight()-600)/pixelHeight;
        if (maxScroll < 0)  maxScroll = 0;
        
        page = new Actor() {};
        page.setImage(pane);
        addObject(page, 450, Math.min(pane.getHeight()/2, 300+page.getImage().getHeight()/2));
        
        int handleHeight = 600*600/pane.getHeight();
        if (handleHeight >= 600) return;

        GreenfootImage hImg = new GreenfootImage(16, handleHeight);
        hImg.setColor(Color.GRAY);
        hImg.fill();
        
        loY = hImg.getHeight()/2;
        hiY = 600-hImg.getHeight()/2;
        range = hiY-loY;
        
        GreenfootImage sbImg = new GreenfootImage(20, 600);
        sbImg.setColor(Color.LIGHT_GRAY);
        sbImg.fill();
        sbImg.setColor(Color.DARK_GRAY);
        sbImg.drawRect(2, 2, 16, 596);
        getBackground().drawImage(sbImg, 879, 0);
        
        handle = new Actor()
        {
            public void act()
            {
                if (Greenfoot.mouseDragged(this))   setLocation(getX(), Greenfoot.getMouseInfo().getY());
                
                if (Greenfoot.isKeyDown("up"))      setLocation(getX(), getY()-1);
                if (Greenfoot.isKeyDown("down"))    setLocation(getX(), getY()+1);
                
                if (getY() < loY)   setLocation(getX(), loY);
                if (getY() > hiY)   setLocation(getX(), hiY);
                
                int ds = maxScroll*(getY()-loY)/range-scroll;
                page.setLocation(page.getX(), page.getY()-ds*pixelHeight);
                scroll += ds;
                
            }           
        };        
        handle.setImage(hImg);
        addObject(handle, 890, loY);
    }
    
}
I ask myself whether I understood correctly You tell me that the size of the image is one and equivalent to the number of pixels So I changed the code by adding PixelHeight by replacing Lineheight, I do not know so well I did well it does not scroll Maybe I still forget the code
danpost danpost

2021/5/24

#
Try this:
public ScrollBarV01()
{    
    super(900, 600, 1, false);
    GreenfootImage pane= new GreenfootImage("galaxy-stars02.jpg");
    maxScroll = (pane.getHeight()-600);
    if (maxScroll < 0)  maxScroll = 0;
    page = new Actor() {};
    page.setImage(pane);
    addObject(page, 450, Math.min(pane.getHeight()/2, 300+page.getImage().getHeight()/2));
    int handleHeight = 600*600/pane.getHeight();
    if (handleHeight >= 600) return;
    GreenfootImage hImg = new GreenfootImage(16, handleHeight);
    hImg.setColor(Color.GRAY);
    hImg.fill();
    loY = hImg.getHeight()/2;
    hiY = 600-hImg.getHeight()/2;
    range = hiY-loY;
    GreenfootImage sbImg = new GreenfootImage(20, 600);
    sbImg.setColor(Color.LIGHT_GRAY);
    sbImg.fill();
    sbImg.setColor(Color.DARK_GRAY);
    sbImg.drawRect(2, 2, 16, 596);
    getBackground().drawImage(sbImg, 879, 0);
    handle = new Actor()
    {
        public void act()
        {
            if (Greenfoot.mouseDragged(this)) setLocation(getX(), Greenfoot.getMouseInfo().getY());
            if (Greenfoot.isKeyDown("up")) setLocation(getX(), getY()-1);
            if (Greenfoot.isKeyDown("down")) setLocation(getX(), getY()+1);
            if (getY() < loY) setLocation(getX(), loY);
            if (getY() > hiY) setLocation(getX(), hiY);
            int ds = maxScroll*(getY()-loY)/range-scroll;
            page.setLocation(page.getX(), page.getY()-ds);
            scroll += ds;
        }           
    };        
    handle.setImage(hImg);
    addObject(handle, 890, loY);
}
ronald ronald

2021/5/24

#
I thank you again for Danpost It seems to me that I tried your code, I do not remember but too bad it works
ronald ronald

2021/5/24

#
I just tested on a vertical scroll bar it also works was enough to reverse, I just realize that I have two same images while I have only one is this normal? Is this because of setimage world and new greenfootimage image (.... jpg)?
You need to login to post a reply.
1
2