Animate a bouncing ball with Double Buffering : Animation « SWT 2D Graphics « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » SWT 2D Graphics » Animation 
18.16.2.Animate a bouncing ball with Double BufferingPrevious/Next
Animate a bouncing ball with Double Buffering
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.*;

public class AnimationDoubleBuffering {
  private static final int IMAGE_WIDTH = 100;
  private static final int TIMER_INTERVAL = 10;

  private static int x = 0;
  private static int y = 0;

  private static int directionX = 1;
  private static int directionY = 1;

  private static Canvas canvas;

  public static void animate() {
    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();
  }

  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Animator");

    shell.setLayout(new FillLayout());
    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);

        gcImage.setBackground(event.gc.getBackground());
        gcImage.fillRectangle(image.getBounds());
        gcImage.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
        gcImage.fillOval(x, y, IMAGE_WIDTH, IMAGE_WIDTH);

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

        image.dispose();
        gcImage.dispose();
      }
    });

    
    shell.open();
    Runnable runnable = new Runnable() {
      public void run() {
        animate();
        display.timerExec(TIMER_INTERVAL, this);
      }
    };
    display.timerExec(TIMER_INTERVAL, runnable);

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    // Kill the timer
    display.timerExec(-1, runnable);
    display.dispose();

  }
}
18.16.Animation
18.16.1.Animation a bounce ball with flickerAnimation a bounce ball with flicker
18.16.2.Animate a bouncing ball with Double BufferingAnimate a bouncing ball with Double Buffering
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.