View Single Post
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