COMP 1600 Project 2: Sines, Sines, Everywhere Sines

30 points
Deadline: March 6*

This is an individual assignment

Your Assignment

This is a GUI-based application to draw random points on a sine wave according to specifications provided in JTextFields. Here is a summary.
  1. I will provide you folder Project2, which contains an application, RandomSine.java. You will find it on the "I: drive". Use Windows Explorer to follow the I: drive, then COMP, then 1600, then 20142-COMP-1600-01 then Common. Copy this folder into your Z: drive workspace.
  2. In jGRASP, open RandomSine.java. Compile and run it to make sure it runs. You will modify this class to build your solution.
  3. What your program will do (short version): Plot a randomly-selected point from a sine wave each time the "Add Point" button is clicked. The total height of the wave, in pixels, is taken from the value in a JTextField. Similarly the total width of the plotting area, in pixels, is taken from a second JTextField and the period (width of one complete sine wave), in pixels, is taken from a third JTextField. This can all be done by modifying the class provided. This is illustrated below.
  4. Submit the resulting RandomSine.java program.

The main purpose of this assignment is to give you experience using the API of classes explained in textbook Chapter 2 and Chapter 3 . You need to use the Java API web site to get information on the classes and methods you will be using. Its URL is: http://docs.oracle.com/javase/6/docs/api/

What Your Program Will Do (Details)

  1. Configure the GUI frame in the RandomSine constructor. The basic code is there; just add the additional components as illustrated in the image above.
  2. Add code to the paintComponent(Graphics g) method to draw axes as illustrated above
  3. Add code to the actionPerformed(ActionEvent evt) method to compute the (x,y) coordinate position for a randomly-selected "point" from a sine wave based on the contents of the three JTextFields. The code to actually draw the point is provided for you in paintComponent().
  4. The code to invoke the paintComponent() method is already programmed into actionPerformed(), so you need not be concerned with that aspect of the program. Each time the button is clicked, paintComponent() will be invoked.
  5. The illustration above shows the graph after the "Add Point" button has been clicked about 40 times. For better visibility, a point is actually drawn as a 5-by-5 pixel filled rectangle.
  6. The word "Height" preceding the first JTextField is a JLabel object. Simply create it and add it in RandomSine constructor, much as the JButton was created and added. Similar for the other two.
  7. When you create each JTextField, give it the initial value as shown in the illustration. Note it must be given as a String.
  8. Your program can obtain the current value entered in a JTextField at any time using its getText() method, which will return a String. Your program may assume it will contain a valid integer. Also assume that the user can change its value at any time! This means that actionPerformed() must get the value and convert it to an integer, then calculate the selected point and where to draw it.
  9. Thus the values of the sine wave height (pixels), plotting area width (pixels) and period length (pixels) can all change at any time. Be sure to always calculate the point correctly on the axis system, so the sine wave is centered around the X-axis. The sine wave will always start at 0 radians (origin).
  10. Your program should use named constants whenever appropriate.

Point Allocations

PointsItem
20 Produces results consistent with those in the above illustration, for the illustrated height, width, and period specifications, using a random number generator.
6 Correctly responds to changes to height, width and period.
4 Use of named constants where appropriate, meaningful variable names throughout, and includes your name at the top as a comment.
4 bonus For bonus points, add a "Clear" button that when clicked will clear the random plotted points, leaving just the axes. You'll have to study the "Leave this code as-is" sections of code to discover how to do this. After clicking the Clear button, the next click of the Add Point button will result in exactly one plotted point on the graph.

To Turn In

When you are finished, place RandomSine.java into your individual DropBox folder. This is on the I: drive. Browse to I:\COMP\1600\20142-COMP-1600-01\DropBox then drop the file into the folder with your name.

* I will be out of town on March 6 and there will be no formal lab period that day. If you can't finish it by the 5th, get help from a friend to finish it on the 6th.


[ COMP 1600 | COMP 1600 Projects | Peter Sanderson | Math Sciences home page | Otterbein ]

Last updated:
Peter Sanderson (PSanderson@otterbein.edu)