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/22

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

/**
 * Write a description of class ScrollBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScrollBar01 extends Actor
{
    /**
     * Act - do whatever the ScrollBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        drawScrollBar01();
    }
    
    public void drawScrollBar01()
    {
        GreenfootImage image = new GreenfootImage(900, 40);
        image.setColor(Color.WHITE);
        image.fillRect(0, 0, 880, 38);
        //image.drawRect(0, 0, 880, 38);
        setImage(image);
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ScrollBar02 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScrollBar02 extends Actor
{
    /**
     * Act - do whatever the ScrollBar02 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        drawScrollBar02();
    }
    
    public void drawScrollBar02()
    {
        GreenfootImage img = new GreenfootImage(200, 30);
        img.setColor(Color.GRAY);
        img.fillRect(0, 0, 180, 28);
        //img.drawRect(0, 0, 180, 28);
        setImage(img);
    }
}
I started drawing a scroll bar now I would like to know how to make the gray bar out of the white bar thank you
danpost danpost

2021/5/22

#
I wrote a scroll bar into my TextFile Viewing World Class. Maybe it can give you some pointers.
ronald ronald

2021/5/22

#
yes I saw, I'm looking for the scrollbar code piece, I think I find what matters me is a vertical scrollbar
ronald ronald

2021/5/22

#
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 MyWorld extends World
{

    int loY,hiY, range;
    int lineHeight, maxScroll;
    
    Actor handle;
    
    public MyWorld()
    {    
        super(900, 600, 1, false); 
        
        GreenfootImage pane = new GreenfootImage(900, lineHeight*lines.size());
        maxScroll = (pane.getHeight() - 600)/lineHeight;
        
        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, 779, 0);
        
        handle = new Actor()
        {
            public void act()
            {
                
            }           
        };        
        handle.setImage(hImg);
        addObject(handle, 890, loY);
    }
}
here is the code I found from your scenario as it's not text I want to scroller but an image with a vertical bar bar For calculations, I think I have an idea for the vertical scroll bar it's not complicated But I block with lines.size (), What do I put in place of lines.size () as it is an image? can be the size of the image by reflecting? Thank you for your help Danpost
danpost danpost

2021/5/22

#
ronald wrote...
I block with lines.size (), What do I put in place of lines.size () as it is an image?
lines.size() is irrelevant to you. I used it to determine the height of the image. You apparently already have an image.
ronald ronald

2021/5/22

#
Ah OK I thought it was different between the text and the image so it still lacks code to see Show the Scroll Bar
danpost danpost

2021/5/22

#
ronald wrote...
so it still lacks code to see Show the Scroll Bar
Not sure what you mean, here. The action code for the handle is what I see missing.
danpost danpost

2021/5/22

#
ronald wrote...
I thought it was different between the text and the image
Let me ask this -- how will you create your image? (the one that will be scrolled)
ronald ronald

2021/5/23

#
I thought I insert a JPG image of 900 * 600
ronald ronald

2021/5/23

#
according to your code, I understand it's text because of <string> lines maybe I'm wrong I have a console error at line 23 with the addition of java.util.list <string> lines = new java.util.arrayList (); At line 21. java.lang.IllegalArgumentException: Width (900) and height (0) cannot be <= 0 at java.desktop/java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016) at java.desktop/java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:186) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:130) at MyWorld.<init>(MyWorld.java:23) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at greenfoot.core.Simulation.newInstance(Simulation.java:580) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$0(WorldHandlerDelegateIDE.java:143) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:470) at greenfoot.core.Simulation.maybePause(Simulation.java:299) at greenfoot.core.Simulation.runContent(Simulation.java:190) at greenfoot.core.Simulation.run(Simulation.java:183)
danpost danpost

2021/5/23

#
ronald wrote...
according to your code, I understand it's text because of <string> lines maybe I'm wrong
Probably. I believe you just want to change line 21 to something like:
GreenfootImage pane = new GreenfootImage("<< whateve r>>.jpg");
int lineHeight can be removed. In your case, assumed to be equal to one (1) everywhere; where multiplying or dividing by it makes no difference. That is, any /lineHeight or *lineHeight in the code can just be removed.
ronald ronald

2021/5/23

#
I see the image but not the scroll bar I will continue to search yes I delete Lineheight
ronald ronald

2021/5/23

#
it is all beast instead of taking a picture of 900 * 600 I insert a larger image of 1024 * 699 And the as magic appears the Scroll Bar
danpost danpost

2021/5/23

#
ronald wrote...
I insert a larger image of 1024 * 699 And the as magic appears the Scroll Bar
That is line 25 at work (good job line 25).
ronald ronald

2021/5/23

#
I noticed, Every hour I had put 500 instead of 600, the scroll bar is posted if I had put 700 instead of 600, I will have nothing
There are more replies on the next page.
1
2