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-21-2008, 09:42 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
How to create Animation: Paint and thread
This Java tip shows how to create a simple animation using the paint(Graphics g) method and a thread.

Code:
import java.awt.Color; import java.awt.Graphics; import java.awt.Insets; import java.util.Timer; import java.util.TimerTask; import javax.swing.JFrame; public class Animate extends JFrame { private static int DELAY = 100; Insets insets; Color colors[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA }; public void paint(Graphics g) { super.paint(g); if (insets == null) { insets = getInsets(); } // Calculate each time in case of resize int x = insets.left; int y = insets.top; int width = getWidth() - insets.left - insets.right; int height = getHeight() - insets.top - insets.bottom; int start = 0; int steps = colors.length; int stepSize = 360 / steps; synchronized (colors) { for (int i = 0; i < steps; i++) { g.setColor(colors[i]); g.fillArc(x, y, width, height, start, stepSize); start += stepSize; } } } public void go() { TimerTask task = new TimerTask() { public void run() { Color c = colors[0]; synchronized (colors) { System.arraycopy(colors, 1, colors, 0, colors.length - 1); colors[colors.length - 1] = c; } repaint(); } }; Timer timer = new Timer(); timer.schedule(task, 0, DELAY); } public static void main(String args[]) { Animate f = new Animate(); f.setSize(200, 200); f.show(); f.go(); } }
__________________
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
GUI Animation serfster New To Java 2 06-11-2008 04:37 AM
How to make a MouseClick paint an object Devilsfutbol17 New To Java 6 06-05-2008 01:54 PM
radio buttons and paint gtraylo Java Applets 2 05-20-2008 11:45 PM
Animation Delay - Thread problem wererabit Advanced Java 1 04-17-2008 09:41 AM
paint() and paintComponent() goldhouse Java 2D 1 07-17-2007 04:43 AM


All times are GMT +3. The time now is 03:05 AM.


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