RSS Forums RSS

Drawing a spiral

Please support our Java advertiser: Programming Forums
Reply
Posts: 131
Reputation: degamer106 is an unknown quantity at this point 
Solved Threads: 0
degamer106 degamer106 is offline Offline
Junior Poster

Drawing a spiral

  #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:
import javax.swing.JFrame;
/**
   Test driver for Spiral class.
*/
public class SpiralViewer
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      final int FRAME_WIDTH = 400;
      final int FRAME_HEIGHT = 400;

      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
      frame.setTitle("SpiralViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      SpiralComponent component = new SpiralComponent();
      frame.add(component);
      frame.setVisible(true);
   }
}

SpiralComponent:
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;

public class SpiralComponent extends JComponent
{
   public void paintComponent(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;
      final int INITIAL_SIZE = 10;

      int size = Math.min(getWidth(), getHeight());
      SpiralGenerator gen = new SpiralGenerator(
         INITIAL_SIZE,
         new Point2D.Double(size / 2, size / 2));

      while (true)
      {
         Line2D.Double segment = gen.nextSegment();
         if (!segment.intersects(getBounds()))
            return;
         g2.draw(segment);
      }
   }
}

Spiral Generator:
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;

public class SpiralGenerator
{
   /**
      Creates a spiral generator.
      @param initialSize the size of the first (shortest) segment
      of the spiral, in pixels
      @param start the starting point of the spiral
   */
   public SpiralGenerator(double initialSize, Point2D.Double start)
   {
	   init = start;
	   size = initialSize;
   }

   /**
      Returns the next segment of the spiral.
      @return the next segment
   */
   public Line2D.Double nextSegment()
   {
	   Line2D.Double test = new Line2D.Double(0.0,0.0,0.0,0.0);

	   return test;
   }

   // private implementation
   private double size;
   private Point2D.Double init;

}

SpiralTester:
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;

public class SpiralTester
{
   public static void main(String[] args)
   {
      SpiralGenerator gen = new SpiralGenerator(10, new Point2D.Double(100, 100));
      Line2D line = gen.nextSegment();
      System.out.println(line.getX1()); 
      System.out.println("Expected: 100");
      System.out.println(line.getY1()); 
      System.out.println("Expected: 100");
      System.out.println(line.getX2()); 
      System.out.println("Expected: 110");
      System.out.println(line.getY2()); 
      System.out.println("Expected: 100");
      line = gen.nextSegment();
      System.out.println(line.getX1()); 
      System.out.println("Expected: 110");
      System.out.println(line.getY1());
      System.out.println("Expected: 100");
      System.out.println(line.getX2());
      System.out.println("Expected: 110");
      System.out.println(line.getY2());
      System.out.println("Expected: 90");
   }
}
AddThis Social Bookmark Button
Reply With Quote  
Posts: 4,111
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: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Drawing a spiral

  #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  
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

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

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 3683 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:26 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC