I have a simple JavaFX application with a Canvas and a ScrollBar. The Canvas acts as a virtualized canvas whose contents can be scrolled with the ScrollBar.
The width of the Canvas is 500 pixels but the logical width of the Canvas is 1000 pixels. I've set the ScrollBar's max to 1000 and the ScrollBar's visible amount to 500. The problem is that when the ScrollBar is scrolled all the way to the right the value of the ScrollBar is 1000, not 500. Logically when a ScrollBar is scrolled all the way to the right there should be some way to determine the actual scrolled offset and that does not seem possible. Please suggest how the amount of scroll required can be obtained. Thank you
Here is the example scrolled all the way to the left. The scrollbar looks good. Its visible width is 50% of the window size.
Here is the example scrolled halfway to the right. Here the problem is clear. The Canvas has been scrolled 500 pixels to the right, but if the Canvas was correctly scrolled halfway it would be scrolled only 250 pixels.
Here is the example scrolled all the way to the right.
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.stage.Stage;
public class Sc extends Application {
public ScrollBar scrollBar;
double scrolled;
Canvas canvas;
@Override
public void start(Stage stage) {
VBox vbox = new VBox();
Scene scene = new Scene(vbox);
stage.setScene(scene);
canvas = new Canvas(500, 100);
scrollBar = new ScrollBar();
scrollBar.setMin(0);
scrollBar.setMax(1000);
scrollBar.setVisibleAmount(500);
vbox.getChildren().addAll(canvas, scrollBar);
draw();
scrollBar.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov,
Number old_val, Number new_val) {
scrolled = new_val.doubleValue();
draw();
}
});
stage.show();
}
private void draw()
{
GraphicsContext graphics = canvas.getGraphicsContext2D();
graphics.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(1, Color.RED)};
LinearGradient lg = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
graphics.setFill(lg);
graphics.fillRect(0-scrolled, 30, 1000, 40);
}
public static void main(String[] args) {
launch(args);
}
}