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:46 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 a Hypnosis Spiral in Java
This Java tip creates a hypnosis spiral animation in Java.

Code:
import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.Timer; /** * A Swing component that smoothly animates a spiral in a hypnotic way. */ public class Hypnosis extends JComponent implements ActionListener { double x, y; // The center of the spiral double r1, r2; // The inner and outer radii of the spiral double a1, a2; // The start and end angles of the spiral double deltaA; // How much the angle changes each frame double deltaX, deltaY; // The trajectory of the center float linewidth; // How wide the lines are Timer timer; // The object that triggers the animation BufferedImage buffer; // The image we use for double-buffering Graphics2D osg; // Graphics2D object for drawing into the buffer public Hypnosis(double x, double y, double r1, double r2, double a1, double a2, float linewidth, int delay, double deltaA, double deltaX, double deltaY) { this.x = x; this.y = y; this.r1 = r1; this.r2 = r2; this.a1 = a1; this.a2 = a2; this.linewidth = linewidth; this.deltaA = deltaA; this.deltaX = deltaX; this.deltaY = deltaY; // Set up a timer to call actionPerformed() every delay milliseconds timer = new Timer(delay, this); // Create a buffer for double-buffering buffer = new BufferedImage((int) (2 * r2 + linewidth), (int) (2 * r2 + linewidth), BufferedImage.TYPE_INT_RGB); // Create a Graphics object for the buffer, and set the linewidth // and request antialiasing when drawing with it osg = buffer.createGraphics(); osg.setStroke(new BasicStroke(linewidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); osg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } // Start and stop the animation by starting and stopping the timer public void start() { timer.start(); } public void stop() { timer.stop(); } /** * Swing calls this method to ask the component to redraw itself. This * method uses double-buffering to make the animation smoother. Swing does * double-buffering automatically, so this may not actually make much * difference, but it is important to understand the technique. */ public void paintComponent(Graphics g) { // Clear the background of the off-screen image osg.setColor(getBackground()); osg.fillRect(0, 0, buffer.getWidth(), buffer.getHeight()); // Now draw a black spiral into the off-screen image osg.setColor(Color.black); osg.draw(new Spiral(r2 + linewidth / 2, r2 + linewidth / 2, r1, a1, r2, a2)); // Now copy that off-screen image onto the screen g.drawImage(buffer, (int) (x - r2), (int) (y - r2), this); } /** * This method implements the ActionListener interface. Our Timer object * calls this method periodically. It updates the position and angles of the * spiral and requests a redraw. Instead of redrawing the entire component, * however, this method requests a redraw only for the area that has * changed. */ public void actionPerformed(ActionEvent e) { // Ask to have the old bounding box of the spiral redrawn. // Nothing else has anything drawn in it, so it doesn't need a redraw repaint((int) (x - r2 - linewidth), (int) (y - r2 - linewidth), (int) (2 * (r2 + linewidth)), (int) (2 * (r2 + linewidth))); // Now animate: update the position and angles of the spiral // Bounce if we've hit an edge Rectangle bounds = getBounds(); if ((x - r2 + deltaX < 0) || (x + r2 + deltaX > bounds.width)) deltaX = -deltaX; if ((y - r2 + deltaY < 0) || (y + r2 + deltaY > bounds.height)) deltaY = -deltaY; // Move the center of the spiral x += deltaX; y += deltaY; // Increment the start and end angles; a1 += deltaA; a2 += deltaA; if (a1 > 2 * Math.PI) { // Don't let them get too big a1 -= 2 * Math.PI; a2 -= 2 * Math.PI; } // Now ask to have the new bounding box of the spiral redrawn. This // rectangle will be intersected with the redraw rectangle requested // above, and only the combined region will be redrawn repaint((int) (x - r2 - linewidth), (int) (y - r2 - linewidth), (int) (2 * (r2 + linewidth)), (int) (2 * (r2 + linewidth))); } /** Tell Swing not to double-buffer for us, since we do our own */ public boolean isDoubleBuffered() { return false; } /** This is a main() method for testing the component */ public static void main(String[] args) { JFrame f = new JFrame("Hypnosis"); Hypnosis h = new Hypnosis(200, 200, 10, 100, 0, 11 * Math.PI, 7, 100, 2 * Math.PI / 30, 3, 5); f.getContentPane().add(h, BorderLayout.CENTER); f.setSize(400, 400); f.show(); h.start(); } }
__________________
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
I must create a robotic hand with java .... any one can help ?? yashar New To Java 9 03-25-2008 04:46 PM
How to create ToolTip in Java 3d roshithmca AWT / Swing 0 02-04-2008 07:57 AM
how to create a menu bar in java tommy New To Java 1 08-05-2007 08:43 AM
Create a Calculator in Java Albert New To Java 2 07-04-2007 09:01 AM
how to create pdf document from java sreedharvlsi New To Java 1 07-02-2007 12:57 PM


All times are GMT +3. The time now is 02:47 PM.


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