How to add a vertial scrollbar in Canvas?
Question
I am developing a Java ME application using Canvas
. The details displayed on the Canvas
are plentiful, so I need to scroll down the screen to view the further details.
But how can I add a vertical scrollbar on a Canvas
?
Solution
I think that the scrollbar should be drawn inside the paint
method. And you implement the keyReleased
method when clicking the arrow of the scrollbar. To implement the scrolling
I would prefer call repaint();
and I will manage the paint
method with boolean
or some other type of private variable
.
OTHER TIPS
Consider your canvas as the source and the scroll bar as destination use the following formula to find the scroll bar position
Target point X co-ordinate: tX = taX + ((tW * (sX - saX)) / sW)
Target point Y co-ordinate: tY = taY - ((tH * (saY - sY)) / sH)
saX : Source axis start X coordinate
saY : Source axis start Y coordinate
sW : Souce Width
sH : Source Height
taX : Target axis start X coordinate
taY : Target axis start Y coordinate
tW : Target Width
tH : Target Height
The target height would be the height of the canvas. You must draw the scroll bar as the topmost element.