CIS 260 Koch Snowflake
Purpose
The objectives of this assignment are to use a queue and
multithreading, reinforce some basic object-oriented programming
concepts, and get additional experience programming a GUI.
Assignment
This assignment involves the use of fractals, queues, threads,
and a GUI interface.
You should create two classes for this assignment: Line and Snowflake.
You may place them both in the same source file if you
wish, but Snowflake should be the only public class if you do that.
The requirements for both classes are listed below:
Line class
The purpose of this class is to represent a line. A line is
defined in this case by its endpoints. Use the java.awt.Point
class to store your points.
- two private Point variables
- a constructor that accepts a start and end point
- accessor method getP1() to return the first point
- accessor method getP2() to return the second point
- a "void draw(Graphics g)" method that draws the line
(see the Java documentation for the Graphics class
and check out the drawLine method)
- a method named length that returns a double which is the
length of the line; you can calculate the length of a line
as follows:
- lengthX = length of line in x direction
- lengthY = length of line in y direction
- length of line = square root of (lengthX squared
plus lengthY squared)
- Math.pow can do square roots
Snowflake class
This is where all the real work gets done. Here are the requirements:
- The Snowflake class should extend JFrame (so it can be a GUI) and implement
Runnable (so it can be a thread)
- private instance fields
- an ArrayDeque to hold Line objects and be used as a queue
- a boolean to keep track of whether the application is still running
- a JPanel used to display the graphics
- static void main(String[])
- Create a Snowflake object
- default constructor
- Use SwingUtilities to call createAndShowGUI at a proper time
- private void createAndShowGUI
- Set the title of window
- Add the JPanel to draw on
- Create three Point objects: (150,10), (50,183), (250,183)
- Create a new ArrayDeque to be used as a queue
- Create three Line objects using the Points above
- Add the three lines to the queue
- Make sure program stops when window closes
- Set the window to 300 by 300 pixels
- Make the window non-resizable
- Make the window visible
- Make the current object into a thread and start it running
- public void run()
- set the instance variable you have to indicate the thread is running to true
- while the variable just mentioned is true, repeat the rest of these steps
- remove a line from the queue
- send the line to process(Line)
- wait a little while to slow the drawing process down (Thread.sleep(long) could
be helpful here)
- call repaint()
- private void process(Line)
- If the length of the line passed in is less than or equal to 5, then
set the instance variable that indicates the thread is running to false,
add the line that was passed in back into the queue, and exit this method.
You should synchronize the add based on the queue to avoid threading conflicts.
- Now you have to replace the line that was passed in with four new lines. The
replacement will accomplish the following:
|
|
|
old line | new lines |
The line that was passed in was already removed from the queue, but the four
new lines will have to be added to the queue.
- You can get the endpoints for the new lines as follows:
- let startx be the x coordinate of the start point of the original line
- let starty be the y coordinate of the start point of the original line
- let endx be the x coordinate of the end point of the original line
- let endy be the y coordinate of the end point of the original line
- let diffx be (endx-startx) divided by 3
- let diffy be (endy-starty) divided by 3
- p1 is the same as the start point of the original line
- p2's x coordinate is startx + diffx
- p2's y coordinate is starty + diffy
- p3's x coordinate is startx + 1.5 * diffx - diffy
- p3's y coordinate is starty + 1.5 * diffy + diffx
- p4's x coordinate is startx + 2 * diffx
- p4's y coordinate is starty + 2 * diffy
- p5 is the same as the end point of the original line
- Once you have created the four new lines and added them to the
queue, you are done with this method. Adding the lines to the queue
should be done synchronized on the queue to avoid multithreading
conflicts.
- public void paint(Graphics g)
- Get the JPanel's graphics context so you can draw to it.
- Erase the JPanel using the Graphic's class fillRect() method with
white as the color. You can get the height and width from the object
you are since you are a JFrame.
- Get an iterator of all the Lines in the queue and for
each Line in the queue, tell the Line to draw itself. Set the
drawing color before drawing any of the lines. Send the line
objects the graphics context of the JPanel.
You want this done synchronized on the queue.