Reputation: 1680
I have created a sample application where I want to do scrolling, but I am not able to achieve the desired output.
I have tried the following code inside paint method >>>>>
g.translate(x,y);
and passing the value for variable 'y' in keydown and keyup event.
Can anyone help me out to do this?
Upvotes: 2
Views: 1340
Reputation: 1680
Have you tried with decrementing the value of Y in keyUP and calling repaint()
method also do same with keyDown but now the value of Y will be decrementing and call repaint()
method.
Use the g.translate(x, changingYvalue);
try this.
Upvotes: 1
Reputation: 29662
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
.
Upvotes: 0
Reputation: 14893
Easiest way (though memory intensive) is to use MutableImage, and draw it with negative y coordinates.
Mutable image is just a new Image(w,h).
Then you get its Graphics context, using myImage.getGraphics()
, and draw with this Graphics object. It serves as an off-screen buffer.
Than in paint(Graphics g), you call:
g.drawImage(0,y,...);
where you loop decreasing y on each system call to paint.
(don't block paint! - each call is one frame, and you're doing frame by frame animation
do the animation loop from another thread, where you decrease y, than call repaint(), and sleep).
Upvotes: 2