Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2008, 09:23 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,681
Rep Power: 4
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default Demonstration of animation using double buffering
Code:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;/**
 * This class demonstrates animation. It uses double buffering
 */
public class AnimatorDoubleBuffer {
  // The width (and height) of the image
  private static final int IMAGE_WIDTH = 100;

  // The timer interval in milliseconds
  private static final int TIMER_INTERVAL = 10;

  // The location of the "ball"
  private int x = 0;
  private int y = 0;

  // The direction the "ball" is moving
  private int directionX = 1;
  private int directionY = 1;

  // We draw everything on this canvas
  private Canvas canvas;

  /**
   * Runs the application
   */
  public void run() {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Animator Double Buffer");
    createContents(shell);
    shell.open();

    // Set up the timer for the animation
    Runnable runnable = new Runnable() {
      public void run() {
        animate();
        display.timerExec(TIMER_INTERVAL, this);
      }
    };

    // Launch the timer
    display.timerExec(TIMER_INTERVAL, runnable);

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }

    // Kill the timer
    display.timerExec(-1, runnable);
    display.dispose();
  }

  /**
   * Creates the main window's contents
   * 
   * @param shell the main window
   */
  private void createContents(final Shell shell) {
    shell.setLayout(new FillLayout());

    // Create the canvas for drawing
    canvas = new Canvas(shell, SWT.NO_BACKGROUND);
    canvas.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent event) {
        // Create the image to fill the canvas
        Image image = new Image(shell.getDisplay(), canvas.getBounds());

        // Set up the offscreen gc
        GC gcImage = new GC(image);

        // Draw the background
        gcImage.setBackground(event.gc.getBackground());
        gcImage.fillRectangle(image.getBounds());

        // Set the color of the ball
        gcImage.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));

        // Draw the ball
        gcImage.fillOval(x, y, IMAGE_WIDTH, IMAGE_WIDTH);

        // Draw the offscreen buffer to the screen
        event.gc.drawImage(image, 0, 0);

        // Clean up
        image.dispose();
        gcImage.dispose();
      }
    });
  }

  /**
   * Animates the next frame
   */
  public void animate() {
    // Determine the ball's location
    x += directionX;
    y += directionY;

    // Determine out of bounds
    Rectangle rect = canvas.getClientArea();
    if (x < 0) {
      x = 0;
      directionX = 1;
    } else if (x > rect.width - IMAGE_WIDTH) {
      x = rect.width - IMAGE_WIDTH;
      directionX = -1;
    }
    if (y < 0) {
      y = 0;
      directionY = 1;
    } else if (y > rect.height - IMAGE_WIDTH) {
      y = rect.height - IMAGE_WIDTH;
      directionY = -1;
    }

    // Force a redraw
    canvas.redraw();
  }

  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new AnimatorDoubleBuffer().run();
  }
}
__________________
"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create animation Java Tip java.awt 0 06-21-2008 08:43 PM
GUI Animation serfster New To Java 2 06-11-2008 03:37 AM
Text Animation rossomandop@acm.org AWT / Swing 4 05-30-2008 03:34 AM
Double Buffering problem aprenz Java Applets 0 05-28-2008 04:26 AM
Animation with Animated GIF JavaBean Java 2D 1 06-07-2007 05:11 PM


All times are GMT +2. The time now is 04:16 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org