Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip Book in Java/Swing

Tags:

java

swing

You gyus have seen those flash based flip books. I want to create the same in Java. I am using JTextArea for leaf of the book. What I want to know is how can I go about implementing the page flip effect, by overriding the componentPaint method combined with Mouse/KeyListener perhaps?

like image 675
SegFault Avatar asked Apr 14 '11 09:04

SegFault


1 Answers

You would need to render the existing page into an off-screen buffer, then transform the buffer to render the page flip.

So,

  • create an off-screen image buffer of the size of the screen.
  • Get the graphics context of the buffer and call super.componentPaint with that context
  • You've now got your page rendered 'flat' into your off screen buffer
  • With your on-screen context,
    • clear the area,
    • copy the offscreen page scaled to show the flip
    • draw an alpha gradient over the top to 'shade' the page so it looks 3D

You'd need to drive this in a thread to repeatedly invoke the re-draw whilst the animation plays out. So Set a 'pageIsTurning' flag, and 'percentageTurned' that will indicate to the paint method that it needs to do the special drawing and where it is in the animation. Update the percentageTurned flag as you repeatedly call the paint method, then once things are complete, reset the flag and allow the componentPaint method to default to super.componentPaint.

like image 193
AndyT Avatar answered Nov 15 '22 01:11

AndyT