Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Wiki
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Building Green Software: A Sustainable Approach to Software Development and Operations
this week in the
Agile/Processes
forum!
Bookmark Topic
Watch Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Devaka Cooray
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Wiki
Code Barn Hello Anim Thread C
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
[b]CODE[/b] [code=java] // Simple Animation --last in a series of four --- series started with HelloAnimThreadFirst // A rectangle bounces off the edges of the applet, forever. // Uses double-buffering to provide smooth, flicker-free animation import java.awt.* ; import java.applet.Applet ; public class HelloAnimThreadC extends Applet implements Runnable { private int maxWidth , maxHeight ; // animation boundary private int currX , currY ; // left,top (x,y) of the animating rectangle private int currXVelocity , currYVelocity ; // pixels per move private int imageHeight , imageWidth ; // size of animating rectangle // new stuff for dbl-buffering Graphics offscreen ; Image imageForOffscreen ; public void init() { setBackground( Color.yellow ) ; maxWidth = 200 ; maxHeight = 200 ; imageHeight = 25 ; imageWidth = 40 ; currXVelocity = 3 ; currYVelocity = 3 ; // set up random positions to start the images currX = (int)( ( Math.random() * 80 ) + 1 ); currY = (int)( ( Math.random() * 80 ) + 10 ); // new stuff for dbl-buffering imageForOffscreen = createImage( maxWidth , maxHeight ); offscreen = imageForOffscreen.getGraphics(); Thread animator = new Thread( this ); animator.start(); } // close init public void paint( Graphics g ) //<-- dbl-buffered now { // first do what the update would have done -- //erase the background of the offscreen Graphics surface offscreen.setColor( getBackground() ); offscreen.fillRect( 0 ,0 ,maxWidth , maxHeight ); // paint the rectangle onto the offscreen surface // If there were more images to be painted, it would // all happen here. Paint ALL images at this step offscreen.setColor( Color.black ); offscreen.drawRect( currX , currY , imageWidth , imageHeight ); // then blast that final image to the screen g.drawImage( imageForOffscreen , 0 , 0 , this ); } // close paint // override update to prevent it from erasing the background public void update( Graphics g ) { paint( g ); } public void run() { // update location/velocity of the rectangle while ( true ) { try { Thread.sleep( 50 ); } catch ( InterruptedException e ) { } currX = currX + currXVelocity ; currY = currY + currYVelocity ; if ( ( currX + imageWidth ) >= maxWidth ) { currXVelocity = -3 ; } else if ( currX <= 0 ) { currXVelocity = 3 ; } if ( ( currY + imageHeight ) >= maxHeight ) { currYVelocity = -3 ; } else if ( currY <= 0 ) { currYVelocity = 3 ; } repaint(); } // close while loop } // close run } // close class
CodeBarnApplets
Consider Paul's
rocket mass heater
.
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Animation
Why does animation need threads????
Code Barn Hello Anim Thread First
Weird displaying issues between computers?
Animation
More...