2

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?

gnat
  • 6,213
  • 108
  • 53
  • 73
iLearner
  • 1,670
  • 2
  • 19
  • 45
  • most likely there's a bug in your code. If you show code you use in Canvas#keyPressed and in Canvas#paint, it will be easier to tell. Also, how do you test your app - do you use emulator? do you write log messages from code? – gnat Feb 10 '12 at 07:51

3 Answers3

2

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).

Amir Uval
  • 14,425
  • 4
  • 50
  • 74
1

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.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
iLearner
  • 1,670
  • 2
  • 19
  • 45
0

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.

Lucifer
  • 29,392
  • 25
  • 90
  • 143