Drawing a spiral

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Drawing a spiral

 
0
  #1
Oct 24th, 2007
Ok, so my goal is to draw a rectangular spiral in java. So far, I've created the Spiral Viewer class and the SpiralComponent but I'm not quite sure how I would implement the SpiralGenerator class. Here's what I have:

SpiralViewer:
  1. import javax.swing.JFrame;
  2. /**
  3.   Test driver for Spiral class.
  4. */
  5. public class SpiralViewer
  6. {
  7. public static void main(String[] args)
  8. {
  9. JFrame frame = new JFrame();
  10. final int FRAME_WIDTH = 400;
  11. final int FRAME_HEIGHT = 400;
  12.  
  13. frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
  14. frame.setTitle("SpiralViewer");
  15. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. SpiralComponent component = new SpiralComponent();
  17. frame.add(component);
  18. frame.setVisible(true);
  19. }
  20. }

SpiralComponent:
  1. import javax.swing.JComponent;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.Point2D;
  5. import java.awt.geom.Line2D;
  6.  
  7. public class SpiralComponent extends JComponent
  8. {
  9. public void paintComponent(Graphics g)
  10. {
  11. Graphics2D g2 = (Graphics2D) g;
  12. final int INITIAL_SIZE = 10;
  13.  
  14. int size = Math.min(getWidth(), getHeight());
  15. SpiralGenerator gen = new SpiralGenerator(
  16. INITIAL_SIZE,
  17. new Point2D.Double(size / 2, size / 2));
  18.  
  19. while (true)
  20. {
  21. Line2D.Double segment = gen.nextSegment();
  22. if (!segment.intersects(getBounds()))
  23. return;
  24. g2.draw(segment);
  25. }
  26. }
  27. }

Spiral Generator:
  1. import java.awt.geom.Point2D;
  2. import java.awt.geom.Line2D;
  3.  
  4. public class SpiralGenerator
  5. {
  6. /**
  7.   Creates a spiral generator.
  8.   @param initialSize the size of the first (shortest) segment
  9.   of the spiral, in pixels
  10.   @param start the starting point of the spiral
  11.   */
  12. public SpiralGenerator(double initialSize, Point2D.Double start)
  13. {
  14. init = start;
  15. size = initialSize;
  16. }
  17.  
  18. /**
  19.   Returns the next segment of the spiral.
  20.   @return the next segment
  21.   */
  22. public Line2D.Double nextSegment()
  23. {
  24. Line2D.Double test = new Line2D.Double(0.0,0.0,0.0,0.0);
  25.  
  26. return test;
  27. }
  28.  
  29. // private implementation
  30. private double size;
  31. private Point2D.Double init;
  32.  
  33. }

SpiralTester:
  1. import java.awt.geom.Point2D;
  2. import java.awt.geom.Line2D;
  3.  
  4. public class SpiralTester
  5. {
  6. public static void main(String[] args)
  7. {
  8. SpiralGenerator gen = new SpiralGenerator(10, new Point2D.Double(100, 100));
  9. Line2D line = gen.nextSegment();
  10. System.out.println(line.getX1());
  11. System.out.println("Expected: 100");
  12. System.out.println(line.getY1());
  13. System.out.println("Expected: 100");
  14. System.out.println(line.getX2());
  15. System.out.println("Expected: 110");
  16. System.out.println(line.getY2());
  17. System.out.println("Expected: 100");
  18. line = gen.nextSegment();
  19. System.out.println(line.getX1());
  20. System.out.println("Expected: 110");
  21. System.out.println(line.getY1());
  22. System.out.println("Expected: 100");
  23. System.out.println(line.getX2());
  24. System.out.println("Expected: 110");
  25. System.out.println(line.getY2());
  26. System.out.println("Expected: 90");
  27. }
  28. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,477
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Drawing a spiral

 
0
  #2
Oct 24th, 2007
There are two different ways you can approach drawing the spiral.
1) Using GeneralPath to define the path from a fixed coordinate system in which you define the coordinates of the segments to be drawn on a fixed x-y plane.
2) Using coordinate transformations (translate, rotate) to alter the coodinate system as you draw each segment. With translation and rotation, drawing the path becomes trivial. You simply draw a line in a fixed direction (which ever way you want to go from your origin, like 0,0 to 0,100, translate to the end of that line, rotate 90 deg left or right, draw a slightly shorter line in the same direction as the first (perhaps 0,0 to 0,90), and continue until you reach the endpoint you want. By moving and rotating your origin as you draw, you only have to draw increasingly shorter line segments in the direction of path traversal.

There are some examples of using coordinate transforms here:
http://www.java2s.com/Code/Java/2D-G...ndRotation.htm

Hope that helps get you started.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Re: Drawing a spiral

 
0
  #3
Oct 24th, 2007
Heya, thanks for the reply but I already finished it =P.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC