Table of Contents

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:

  1. Scan through the set of Points to determine the ranges (min and max values) of x and y values that it contains.
  2. Determine the Dimensions of the client area (remember its Insets).
  3. 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.)
  4. 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:

  1. 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.)
  2. Where will you place the code to determine the x and y ranges for the points?
  3. Where will you place the code to do the scaling and plotting?
  4. 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.).

Here’s a CoordinateSystem class to give you a skeleton Java source:

package shapes;

/**
 *  A class to represent a world coordinate system, and to provide
 *  translation between that world coordinate system and a viewport 
 *  using screen (pixel) coordinates.
 *  <p>
 *  @author Bryan J. Higgs, 26 August 1998
 *  <p>
 *  @version 1.0
 */

public class CoordinateSystem
{
    /**
    *   Creates an instance of class CoordinateSystem,
    *   with default world coordinates.
    */
    public CoordinateSystem()
    {}
    
    /**
    *   Creates an instance of class CoordinateSystem,
    *   with specified world coordinate x and y ranges.
    *   <br>Hint: Use setWorld()
    *
    *   @param xlow the lowest x value
    *   @param xhigh the highest x value
    *   @param ylow the lowest y value
    *   @param yhigh the highest y value
    */
    public CoordinateSystem(double xlow, double xhigh, 
                            double ylow, double yhigh)
    {
    }
    
    /**
    *   Sets the world coordinate x and y ranges.
    *
    *   @param xlow the lowest x value
    *   @param xhigh the highest x value
    *   @param ylow the lowest y value
    *   @param yhigh the highest y value
    */
    public void setWorld(double xlow, double xhigh, 
                         double ylow, double yhigh)
    {
    }

    /**
    *   Sets the world coordinate x and y ranges.
    *
    *   @param coords the coordinate values:
    *   <br>[0] the lowest x value
    *   <br>[1] the highest x value
    *   <br>[2] the lowest y value
    *   <br>[3] the highest y value
    */
    public void setWorld(double[] coords)
    {
    }
    
    /**
    *   Gets the world coordinate x and y ranges.
    *
    *   @return the range:
    *   <br>[0] the lowest x value
    *   <br>[1] the highest x value
    *   <br>[2] the lowest y value
    *   <br>[3] the highest y value
    */
    public double[] getWorld()
    {
    }
    
    /**
    *   Sets the width and height of the viewport (Window coordinates).
    *
    *   @param width the viewport width, in pixels
    *   @param height the viewport height, in pixels
    */
    public void setViewPortSize(int width, int height)
    {
    }

    /**
    *   Converts an x world coordinate value into a (viewport)
    *   pixel x window coordinate.
    *
    *   <br>Here's the code for this function:
    *   <pre>
    *   public int xToPixelPos(double x)
    *   {
    *       int value = (int)Math.round((m_xwidth - 2 * INSETS)
    *                           * (x - m_xlow) / (m_xhigh - m_xlow));
    *       return (INSETS + value);                  
    *   }
    *   </pre>
    *
    *   @param x the x world coordinate
    *   @return the pixel viewport x window coordinate
    */
    public int xToPixelPos(double x)
    {
        int value = (int)Math.round((m_xwidth - 2 * INSETS)
                            * (x - m_xlow) / (m_xhigh - m_xlow));
        return (INSETS + value);                  
    }

    /**
    *   Converts a y world coordinate value into a (viewport)
    *   pixel y window coordinate.
    *
    *   <br>Here's the code for this function:
    *   <pre>
    *   public int yToPixelPos(double y)
    *   {
    *       int value = (int)Math.round((m_yheight - 2 * INSETS)
    *                           * (m_yhigh - y) / (m_yhigh - m_ylow));
    *       return (INSETS + value);                  
    *   }
    *   </pre>
    *
    *   @param y the y world coordinate
    *   @return the pixel viewport y window coordinate
    */
    public int yToPixelPos(double y)
    {
        int value = (int)Math.round((m_yheight - 2 * INSETS)
                            * (m_yhigh - y) / (m_yhigh - m_ylow));
        return (INSETS + value);                  
    }

    /**
    *   Converts a viewport x window coordinate into an
    *   x world coordinate.
    *
    *   <br>Here's the code for this function:
    *   <pre>
    *   public double xFromPixelPos(int x)
    *   {
    *       return m_xlow + (m_xhigh - m_xlow)
    *                 * (x - INSETS) / (m_xwidth - 2 * INSETS);
    *   }
    *   </pre>
    *
    *   @param x the x window coordinate
    *   @return the x world coordinate
    */
    public double xFromPixelPos(int x)
    {
        return m_xlow + (m_xhigh - m_xlow)
                  * (x - INSETS) / (m_xwidth - 2 * INSETS);
    }

    /**
    *   Converts a viewport y window coordinate into a 
    *   y world coordinate.
    *
    *   <br>Here's the code for this function:
    *   <pre>
    *   public double yFromPixelPos(int y)
    *   {
    *       return m_yhigh + (m_ylow - m_yhigh)
    *                 * (y - INSETS) / (m_yheight - 2 * INSETS);
    *   }
    *   </pre>
    *
    *   @param y the y pixel coordinate
    *   @return the y world coordinate
    */
    public double yFromPixelPos(int y)
    {
        return m_yhigh + (m_ylow - m_yhigh)
                  * (y - INSETS) / (m_yheight - 2 * INSETS);
    }
    
    ////////////// Data /////////////////
    /**
    *   Inset value to use for the viewport.
    *   <br>
    *   This is the number of pixels to use as an 
    *   n-pixel-wide border around the viewport drawable area.
    *   <br>Note: I used 5 pixels.
    */
    private static final int INSETS = 5;

    /**
    *   Width of viewport in pixels (window coordinates).
    */
    private int     m_xwidth;
    
    /**
    *   Height of viewport in pixels (window coordinates).
    */
    private int     m_yheight;
    

    /**
    *   Lowest x world coordinate.
    *   <br>
    *   Defaults to -10.0
    */
    private double  m_xlow  = -10.0;
    /**
    *   Highest x world coordinate.
    *   <br>
    *   Defaults to 10.0
    */
    private double  m_xhigh =  10.0;
    /**
    *   Lowest y world coordinate.
    *   <br>
    *   Defaults to -10.0
    */
    private double  m_ylow  = -10.0;
    /**
    *   Highest y world coordinate.
    *   <br>
    *   Defaults to 10.0
    */
    private double  m_yhigh =  10.0;
}

Copy the above code, and paste it into your favorite IDE.