Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2008, 10:23 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
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(); } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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 09:43 PM
GUI Animation serfster New To Java 2 06-11-2008 04:37 AM
Text Animation rossomandop@acm.org AWT / Swing 4 05-30-2008 04:34 AM
Double Buffering problem aprenz Java Applets 0 05-28-2008 05:26 AM
Animation with Animated GIF JavaBean Java 2D 1 06-07-2007 06:11 PM


All times are GMT +3. The time now is 04:59 AM.


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