Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default 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. #2
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default 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. #3
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default 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. #4
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default 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.add(component);
    frame.setVisible(true);
    }
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default 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. #6
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default 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. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default 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. #8
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default 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. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default 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. #10
    Join Date
    Jan 2012
    Posts
    19
    Rep Power
    0

    Default Re: Draw a RECTANGULAR spiral

    It's being called, never mind.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,897
    Rep Power
    25

    Default 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.

Similar Threads

  1. spiral recursion
    By fatmig in forum New To Java
    Replies: 9
    Last Post: 04-06-2011, 07:45 AM
  2. rotating a rectangular image
    By ighor10 in forum Java 2D
    Replies: 1
    Last Post: 10-24-2010, 08:22 PM
  3. Spiral Array Order Number
    By rendra in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 04:20 AM
  4. Non Rectangular Window in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:28 PM
  5. How to create a Hypnosis Spiral in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:46 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •