943,708 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8829
  • Java RSS
Oct 24th, 2007
0

Drawing a spiral

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006
Oct 24th, 2007
0

Re: Drawing a spiral

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Oct 24th, 2007
0

Re: Drawing a spiral

Heya, thanks for the reply but I already finished it =P.
Reputation Points: 10
Solved Threads: 0
Junior Poster
degamer106 is offline Offline
131 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Please help me to explain these codes
Next Thread in Java Forum Timeline: how to upload an audio file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC