|
| |
Plotting a Graph of Points with
Automatic Scaling
Write a Java program that accepts an array of Points and
graphs them as a set of dots in the application's window. The
graph should be automatically scaled to the size of the window.
For example, the set of points:
(1, 11) (2, 13) (3, 16) (4, 20) (5, 25) (6, 31) (7, 33)
(8, 32) (9, 29) (10, 23) (11, 16) (12, 12)
should produce output that looks like:

When you resize the window, the program should automatically
rescale the graph, as you see below:

Notes:
You will need to do the following:
- Scan through the set of Points to determine the ranges
(min and max values) of x and y values that it contains.
- Determine the Dimensions of the client area (remember its
Insets).
- Use the x and y ranges and client area Dimensions to
determine the scaling factors so that a given (x,y) point
will be plotted at the proper place in the window. (Note
that the conventional y scale increases as you
move down in the window, whereas you will be
plotting the graph in the conventional way, where y scale
values increase as you go up in the
window, so you will have to compensate for this.)
- Plot each point at the proper position in the window,
using the proper scaling factors and offsets to cause the
points to be plotted at the correct position within the
window.
Questions:
- How will you pass the array of points to the application?
(I'm not expecting you to prompt the user for the values;
just hard code them into the source.)
- Where will you place the code to determine the x and y
ranges for the points?
- Where will you place the code to do the scaling and
plotting?
- How will you draw each Point on the screen so that it is
reasonably visible, and not a tiny speck? (Hint: Look at
java.awt.Graphics.fillOval(); do not
use java.awt.Graphics.drawString().)
For information about how to perform the scaling calculations
see How to
do Graphical Scaling.
To make it even easier for you, here's a class that can do a
lot of the work for you: CoordinateSystem.
You'll have to fill in some of the details for yourself, but I've
given you the code that's difficult (take a look at its specification.).
Click on CoordinateSystem to get a skeleton Java
source for the class.
|