# Thread: Draw a RECTANGULAR spiral

1. ## Draw a RECTANGULAR spiral

The three classes are: SpiralComponent, SpiralGenerator, and SpiralViewer.
The rectangular segment is drawn segment by segment. I'm having the most trouble with returning the next segment using the Line2D.double nextSegment method. I have posted the three classes below. Please help, thanks! :)

2. ## Re: Draw a RECTANGULAR spiral

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
{
double x1, x2, y1, y2;

public SpiralComponent(double x1, double x2, double y1, double y2){
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}

public void paintComponent(Graphics g){

Graphics2D g2 = (Graphics2D) g;
final int INITIAL_SIZE = 10;
int size = Math.min(getWidth(), getHeight());
double startX = size/2;
double startY = size/2;
SpiralGenerator gen = new SpiralGenerator(INITIAL_SIZE, new Point2D.Double(startX, startY));

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

}
}
}

3. ## Re: Draw a RECTANGULAR spiral

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

public class SpiralGenerator
{

public double startX;
public double startY;
/**
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
*/

//while(
//start.setLocation(xCoord, yCoord);

public Line2D.Double nextSegment()
{
Point2D.Double starter = new Point2D.Double(startX, startY);

Line2D.Double test = new Line2D.Double(starter.getX(),starter.getY(),starte r.getX()+10,starter.getY()+10);

return test;

}

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

}

4. ## Re: Draw a RECTANGULAR spiral

import java.awt.geom.Point2D;

import javax.swing.JFrame;

public class SpiralViewer
{

public static double startX, startY;

public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setTitle("SpiralViewer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
Point2D.Double starter = new Point2D.Double(startX, startY);
SpiralComponent component = new SpiralComponent(starter.getX(),starter.getY(),star ter.getX()+10,starter.getY()+10);
frame.setVisible(true);
}
}

5. ## Re: Draw a RECTANGULAR spiral

I'm having the most trouble with returning the next segment using the Line2D.double nextSegment method
Please explain what the problem is.

6. ## Re: Draw a RECTANGULAR spiral

Right now it's just returning an empty JFrame.

Also, do I actually need a segment, generator, and viewer class? Or can I combine the segment and generator classes? Thanks.

7. ## Re: Draw a RECTANGULAR spiral

it's just returning an empty JFrame.
What is supposed to be shown in the Frame?
What does the SpiralComponent object do that is supposed to generate something to be viewed?
Is its paintComponent method being called?
Is the draw() method being called?

Add some printlns to the code to verify what it is doing.

8. ## Re: Draw a RECTANGULAR spiral

Ok, tested using printlns. paintComponent isn't being called, nor is the draw method.
Originally, SpiralGenerator was supposed to return the next segment, or next component of the spiral, to be viewed.

The JFrame (no JApplet, it's not animated) should return an unmoving rectangular/square spiral starting from the center of the frame, with linear segments.

Would that just be complicating things? Is it possible to nix the SpiralGenerator class, and just use a for loop inside paintComponent (in the SpiralComponent class) to keep adding components of increasing size until the spiral hits the bounds of the JFrame?

9. ## Re: Draw a RECTANGULAR spiral

You should get a working program before trying to change it.

You need to see why the paintComponent method is not being called. It's being called in the code I copied and executed.
Why is your version not being called?

10. ## Re: Draw a RECTANGULAR spiral

It's being called, never mind.

11. ## Re: Draw a RECTANGULAR spiral

If it is being called, then why isn't the draw() method being called?
When is the condition in the while statement true?
Java Code:
`while(!segment.intersects(getBounds())){`
If it is never true, the draw method is never called and nothing is drawn.