Graphical Application that Draws a Spiral
Not what you're looking for?
Write a graphical application that draws a spiral, such as the following:
Your main class should be called SpiralViewer.
Complete the following class in your solution:
public class SpiralGenerator
{
// private implementation
. . .
/**
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) { . . . }
/**
Returns the next segment of the spiral.
@return the next segment
*/
public Line2D.Double nextSegment() { . . . }
}
Use the following class in your solution:
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
{
private int segments;
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);
}
}
}
Use the following class as your tester class:
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");
}
}
Purchase this Solution
Solution Summary
The expert draws a graphical application that draws a spiral.
Purchase this Solution
Free BrainMass Quizzes
Word 2010: Tables
Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.
C++ Operators
This quiz tests a student's knowledge about C++ operators.
C# variables and classes
This quiz contains questions about C# classes and variables.
Basic Computer Terms
We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.
Excel Introductory Quiz
This quiz tests your knowledge of basics of MS-Excel.