Hypnosis Spiral : Shape « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » ShapeScreenshots 
Hypnosis Spiral
Hypnosis Spiral

/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

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) (* r2 + linewidth),
        (int) (* 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(00, 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) ((r2 + linewidth))(int) ((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 > * Math.PI) { // Don't let them get too big
      a1 -= * Math.PI;
      a2 -= * 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) ((r2 + linewidth))(int) ((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(20020010100011 * Math.PI, 7100,
        * Math.PI / 3035);
    f.getContentPane().add(h, BorderLayout.CENTER);
    f.setSize(400400);
    f.show();
    h.start();
  }
}


           
       
Related examples in the same category
1. fillRect (int, int, int, int) method draws a solid rectangle
2. Draw lineDraw line
3. Draw a PolygonDraw a Polygon
4. Draw an oval outline
5. Draw a (Round)rectangleDraw a (Round)rectangle
6. Fill a polygonFill a polygon
7. Fill a solid oval
8. Fill a (Round)rectangleFill a (Round)rectangle
9. Change fontChange font
10. Draw rectangle 2Draw rectangle 2
11. Draw ArcDraw Arc
12. Draw EllipseDraw Ellipse
13. Fill a Rectangle 2Fill a Rectangle 2
14. Fill Arc 2Fill Arc 2
15. Draw textDraw text
16. Draw unicode string Draw unicode string
17. Shape combineShape combine
18. EffectsEffects
19. Mouse drag and drop to drawMouse drag and drop to draw
20. Arc demonstration: scale, move, rotate, sheerArc demonstration: scale, move, rotate, sheer
21. Shape Test Shape Test
ww___w___._ja___v___a__2_s__.__c___om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.