Results 1 to 11 of 11
Thread: Draw a RECTANGULAR spiral
- 01-19-2012, 08:35 PM #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! :)
- 01-19-2012, 08:35 PM #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);
}
}
}
- 01-19-2012, 08:36 PM #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;
}
- 01-19-2012, 08:37 PM #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.add(component);
frame.setVisible(true);
}
}
- 01-20-2012, 01:36 PM #5
Re: Draw a RECTANGULAR spiral
Please explain what the problem is.I'm having the most trouble with returning the next segment using the Line2D.double nextSegment method
- 01-20-2012, 07:25 PM #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.
- 01-20-2012, 07:27 PM #7
Re: Draw a RECTANGULAR spiral
What is supposed to be shown in the Frame?it's just returning an empty JFrame.
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.
- 01-20-2012, 08:10 PM #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?
- 01-20-2012, 10:40 PM #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?
- 01-20-2012, 11:43 PM #10
Re: Draw a RECTANGULAR spiral
It's being called, never mind.
- 01-20-2012, 11:57 PM #11
Similar Threads
-
spiral recursion
By fatmig in forum New To JavaReplies: 9Last Post: 04-06-2011, 06:45 AM -
rotating a rectangular image
By ighor10 in forum Java 2DReplies: 1Last Post: 10-24-2010, 07:22 PM -
Spiral Array Order Number
By rendra in forum Advanced JavaReplies: 14Last Post: 11-20-2009, 03:20 AM -
Non Rectangular Window in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:28 PM -
How to create a Hypnosis Spiral in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks